PencilCoder

Custom Functions!

Goals:

  • Write functions to facilitate code reuse, simplify code maintenance, and improve readability of your code

Coding Snippets:

  • coding snippets
  • Coffeescript uses the syntax ()-> to define custom functions.
  • Save a function by assigning it a variable name, using the assignment operator, =.
  • The function genericHello doesn't have any arguments. However specificHello has the argument name, which is a variable that can be referenced anywhere within the body of the specificHello definition.

Math and Computer Concepts:

  • Functions facilitate code reuse: using functions, a block of code can be used repeatedly by simply calling the function, rather than having to write all the individual lines of code over and over again.
  • Variables defined outside of a custom function can be referenced in the body of that function. This includes variables that reference other functions, such as label, as illustrated above.
  • Arguments to functions are special variables that can only be referenced within the body of that particular function. For example, name cannot be referenced anywhere outside of the definition of specificHello.

Activities:

Save each of the following in a folder called CustomFunctions.