Functions

Objectives

  • Call functions
  • Create functions
  • Explain global and local variable scope
  • Write anonymous functions
  • Explain the difference between passing arguments by value and by reference
  • Create a function which handles a variable number of arguments
  • Create an immediately invoked function
  • Use nested functions
  • Create recursive functions

Overview

  • Functions are great for encapsulating code for reuse.
  • Functions help modularize code, which makes applications easier to develop, write, read, and maintain.
  • Functions are objects in JavaScript.
  • General purpose functions can be packaged into libraries which can then be easily reused.

Defining a function

  • A sample template for a function is given below.
  • The function name is not required if you are assigning the function to a variable or passing it as an argument.
  • Functions without a name are called anonymous functions.
  • The parameter list is a comma separated list of valid identifier names.
  • The parameter variables are only visible within the function.
  • The parameter list is optional, has no type checking, and doesn't even have to match the number of arguments that are used when the function is called.
  • You may choose to return one item of data or not return anything. Since JavaScript deals with objects easily, the one item of data returned could be an object or array containing many individual items.
  • Any variables used only within the function should be declared within the function using the "var" keyword to make sure they aren't confused with global variables.
  • Even better than "var" is the keyword "let", which lets you declare a variable with block level scope (like c-type languages).
  • The keyword "const" can be used in place of "let" if the variable is immediately initialized and is a constant.

Calling a function

  • template: functionName(argumentList);
  • The argument list is a comma separated list of variable names or values. You may skip an argument in the list by using the value "undefined" for it. The argument list is optional and the number of arguments sent does not have to match the number of parameters specified in the function definition.
  • Arguments which are basic variable types (string, number, Boolean) are sent to the function by-value. This means that copies of the value are sent and the original variable can not be changed by the function.
  • Arguments which are not basic variable types (such as objects and arrays) are sent to the function by-reference. This means that the original variable can be changed by the function.
  • The names of the arguments sent and the paramters received does not have to match.
  • Since the number of parameters doesn't have to match the number of arguments sent, every function has a list of the arguments sent to it stored in an array-like object named "arguments". This allows functions to easily handle variable length argument lists.

Anonymous, nested, and immediately invoked functions

  • Anonymous functions have no names. If they have no names, how can they be used? It can be used by:
    • assigning it to a variable
    • assigning it as the value of an object property
    • passing it as an argument to a function
    • immediately invoking it
  • A nested function is simply one function defined inside another function. This makes the nested function invisible to code outside of the function it is nested inside.
  • An immediately invoked function is a function which is run as soon as it is defined. Everything in the immediately invoked function is hidden from other code.

Recursive functions

  • Recursive functions are those that call themselves either directly or indirectly.
  • You can always replace recursion with iteration and often save resources (time, memory).
  • Recursion is often easier to read, write, and understand (once you get used to the idea of how recursion works).
  • Recursive functions must alwasy lead eventually to a simpler base case so the recursion ends.

Exploring recursion

Try to write recursive JavaScript functions for these other commonly used icons of recursion:

  • Fibonacci numbers
  • Reversing a string
  • Checking to see if a string is a palindrome
  • Towers of Hanoi