Function fundamentals
Homework
- Functions
- Read: Functions - reusable blocks of code
- Read: Build your own function
- Read: Function return values
- Read: Function expression
- Watch (by Steve Griffith):
- Events
- Read: Introduction to events
- Watch: Introduction to JavaScript Event Listeners by Steve Griffith
- Reference: MDN Event Reference
- Scope
- Watch: Variable Scope
Morning reflection
Housekeeping
- Returned marks: CPNT 260 Assignment 4, CPNT 201 Assignment 2
- Week 7 is function week.
- Dailies Shoutouts
- Shivani: Hamburger menu
- 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 aname
and optionallang
parameter.
Materials
- Sample Code: Simple
greet
function
Open lab-time
- Create a function that accepts an array as a parameter and returns a random item from that array.
- 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.