JavaScript Functions - Guided Notes

CSCI 3403 - Web Programming | Week 11, Day 21

Name: _________________________________ Date: _____________

📚 Today's Goal: Learn to write reusable code blocks with functions!
⏰ Homework: Complete function exercises and add them to your one-page website

⚡ What Are Functions?

Functions are _______________ blocks of code that can be _______________ multiple times.

Why Use Functions?

💡 Think of functions like: A recipe you can follow over and over to get the same result!

🔬 Anatomy of a Function

function _________(_________) { // Function body return _________; }
Component Purpose
function _________________________
Function Name _________________________
Parameters _________________________
Function Body { } _________________________
return _________________________

📥 Parameters vs Arguments

// Define function with PARAMETER function favoriteAnimal(_________) { return animal + " is my favorite animal!"; } // Call function with ARGUMENT favoriteAnimal(_________);

Parameter: _________________________________

Argument: _________________________________

Memory Trick:
📌 Parameter = Placeholder (in definition)
📌 Argument = Actual value (when calling)

📤 Return Values

The return statement:

  1. Sends a value _______________ from the function
  2. _______________ the function execution
  3. Makes the function result _______________

With vs Without Return

Without Return With Return
Function performs action Function produces value
Result is undefined Result can be stored/used
Side effects only Can be chained/composed

🔒 Function Scope

Scope determines where _______________ can be accessed.

Scope Rules:

let globalVar = "I'm global!"; function myFunction() { let localVar = _______________; console.log(globalVar); // Works? _____ console.log(localVar); // Works? _____ } console.log(globalVar); // Works? _____ console.log(localVar); // Works? _____

📝 Types of Functions

Type Syntax Key Feature
Function Declaration function name() {} _______________
Function Expression const name = function() {} _______________
Arrow Function const name = () => {} _______________

💪 Practice Exercises

Exercise 1: add7

Write a function that takes one number and returns that number + 7

function add7(_________) { _________________________________ } console.log(add7(5)); // Should output: _____

Exercise 2: multiply

Write a function that takes 2 numbers and returns their product

function multiply(_____, _____) { _________________________________ } console.log(multiply(3, 4)); // Should output: _____

Exercise 3: capitalize

Write a function that capitalizes only the first letter of a string

function capitalize(str) { return str[0].___________() + str.slice(1).___________(); }

Exercise 4: lastLetter

Write a function that returns the last letter of a string

function lastLetter(str) { return str[_______________]; }

🌍 Real World Functions

Complete these common real-world function patterns:

Email Validation

function isValidEmail(email) { return email.includes('_____') && email.includes('_____'); }

Format Currency

function formatPrice(price) { return '_____' + price.toFixed(_____); }

Calculate Age

function calculateAge(birthYear) { const currentYear = new Date()._______________(); return _________________________________; }

⭐ Best Practices Checklist

✅ DO:

❌ DON'T:

🎯 Key Concepts Review

  1. What are functions useful for?
    _________________________________________
  2. How do you invoke (call) a function?
    _________________________________________
  3. What's the difference between parameters and arguments?
    _________________________________________
    _________________________________________
  4. What is function scope?
    _________________________________________
  5. What happens after a return statement?
    _________________________________________
  6. When should you use arrow functions?
    _________________________________________

📚 The Call Stack

When functions call other functions, JavaScript uses a _______________ to keep track of execution order.

function first() { console.log("First start"); second(); console.log("First end"); } function second() { console.log("Second"); } first();

Output order:

  1. _______________
  2. _______________
  3. _______________

📝 Additional Notes

Use this space for any additional notes during the lecture: