Function fundamentals

Homework

  1. Functions
  2. Events
  3. Scope

Morning reflection

Housekeeping

  • Returned marks: CPNT 260 Assignment 4, CPNT 201 Assignment 2
  • Week 7 is function week.
  • Dailies Shoutouts
  • Cool app for visualizing Javascript that Erica found.
  • Aidan made a Discord server for WBDV! Check the Slack channel (from Monday the 22nd) for details.

1. Function fundamentals

Learning Objectives

  • Define a function.
    • Understand the difference between function statements and function expressions.
    • Define a function with a function statement.
    • Define a function with a function expression.
  • Understand the difference between function parameters and function arguments.
    • Define a function parameter with a default value.
  • Recognize when a function is being invoked.
    • Pass a function as a value.
    • Invoke a function.
  • Understand what a function return value is.
    • Define a function that returns a value.

Terminology

Function
A reusable set of statements that take an (optional) input and return an (optional) output.
Function Definition
The creation of a function, either as a function statement or a function expression.
Function Invocation
Calling or running a function, using trailing parentheses ().
Function Parameter
A named variable that a function accepts as (eventual) input. Parameters are defined when the function is created.
Function Argument
The actual variable passed to a function when it is invoked.
First-class Function
When a function is treated like any other value. All functions in Javascript are considered first-class and can be passed as arguments to other functions.

Materials

Key Takeaways

See relevant Gists above.


2. Function examples

Learning Objectives

  • Define a greet function that accepts a name and optional lang parameter.

Materials


Open lab-time

  1. Create a function that accepts an array as a parameter and returns a random item from that array.
  2. Review the code you’ve created in CPNT 262 thus far. Are there any other useful snippets you can turn into functions?

Tony’s goals for Lab-Time

  • Convert old function sample code into Gists for Week 7.

Dailies

  • Submit today’s Codepen/repo/gist to the Dailies section (in Assessments) in Brightspace.