Skip to content

Instantly share code, notes, and snippets.

@Shurlow
Forked from bwreid/01 Functions.md
Last active September 14, 2018 17:48
Show Gist options
  • Save Shurlow/f655c7a2f69d8d3b41296a3bec0cdbdc to your computer and use it in GitHub Desktop.
Save Shurlow/f655c7a2f69d8d3b41296a3bec0cdbdc to your computer and use it in GitHub Desktop.
Functions Lesson Notes

Functions

Objectives

  • Define what an abstractions is
  • Explain why abstractions are useful
  • Define what a function is and why they are useful
  • Describe why functions are abstractions
  • Explain the syntax of functions

Terms

  • Function Declaration
  • Function Expression
  • Anonymous Function
  • Paramater
  • Argument
  • Invoke

Guiding Questions

  • What are abstractions? With your table, create a definition of what an abstraction is. Also, describe one abstraction that you have worked with.

    Your answer...

  • Why are abstraction useful? With your table, synthesize an explanation of why abstractions are useful. Add an example of when an abstraction helped you be more productive.

    Your answer...

  • Define what a function is and why they are useful.

    Your answer...

  • Why are functions abstractions?

    Your answer...

  • Explain the syntax of a function.

    Label the following code with all the function termanology that you know!

    1) keyword
    2) name
    3) body
    4) parameters
    5) arguments
    6) invokation
    
    function sayHi(name) {
      return `Hello, ${name}`
    }
    
    sayHi('Mossy')

    Your answer...

  • How are function declarations different from function expressions?

    Your answer...

  • What is the difference between these two functions?

    function boom () {
      console.log('BOOM!')
    }
    
    function boom () {
      return 'BOOM!'
    }

    Your answer...

Practicing Functions

In a scratch javascript file write the code for the following functions. Compare your work with your neighbor.

  • Update the following code to use a function.

    var names = [ 'Erik', 'Sarah', 'Nancy', 'Matt' ]
    if (names.length > 0) {
      var selected = names.shift()
      console.log(selected)
    }
    if (names.length > 0) {
      var selected = names.shift()
      console.log(selected)
    }
    if (names.length > 0) {
      var selected = names.shift()
      console.log(selected)
    }

    Your answer...

  • Write a function called printNums that prints out the numbers 0 through 9.

    Your answer...

  • Update your printNums function to take a num paramater, then update your code to only print the numbers 0 through num.

    Your answer...

  • Update your printNums function to take 2 paramaters. The first paramater is the starting number and second will be the ending number (inclusive).

    Your answer...

  • Write a function named sumAll that will take 0 or more arguments, and return the sum of all arguments. Hint: lookup the arguments keyword.

    Your answer...

  • Write a function called times2, that takes a number as input and returns this number multiplied by 2.

    Your answer...

  • What would the result of the following code be? times2(times2(3))

    Your answer...

  • Looking at the code bellow, what order will we see the the logs?

    function boom() {
      console.log('BOOM!')
    }
    
    function bang() {
      console.log('BANG!')
    }
    
    
    function pow() {
      console.log('POW!')
    }
    
    
    boom(bang(pow()))

    Your answer...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment