Theoretical recap.
A function is a reusable block of code that takes zero or more inputs, called parameters or arguments, performs a task, and often returns a result.
The mental model to keep in mind is that of a machine.
You feed it raw material, the arguments, it processes it according to its internal logic, and it outputs a finished product, the return value.
A function that computes a sum is one of the simplest yet most fundamental examples.
It receives numbers, adds them together, and returns the total.
Key definitions.
A parameter is the named variable declared in the function signature.
An argument is the concrete value passed when calling the function.
The return value is what the function hands back to the caller.
In most languages, a function that does not explicitly return anything gives back an undefined or none value.
Common mistakes include forgetting the return statement.
The function computes but gives nothing back.
Confusing printing a value with returning it.
Initializing an accumulator incorrectly.
Starting a running total at something other than zero.
And off-by-one errors when iterating with a loop.
Mini example, pseudocode.
Function add_two a, b result equals a plus b.
Return result.
add_two three, five, slash, slash returns eight.
Because you have already studied loops and the accumulator pattern, you can now generalize this to summing an arbitrary list of numbers by combining a function with a loop.
Read the full transcript.
Create an account to read the whole episode, search across every transcript, and follow the shows you care about.
Theoretical recap.
A function is a reusable block of code that takes zero or more inputs, called parameters or arguments, performs a task, and often returns a result.
The mental model to keep in mind is that of a machine.
You feed it raw material, the arguments, it processes it according to its internal logic, and it outputs a finished product, the return value.
A function that computes a sum is one of the simplest yet most fundamental examples.
It receives numbers, adds them together, and returns the total.
Key definitions.
A parameter is the named variable declared in the function signature.
An argument is the concrete value passed when calling the function.
The return value is what the function hands back to the caller.
In most languages, a function that does not explicitly return anything gives back an undefined or none value.
Common mistakes include forgetting the return statement.
The function computes but gives nothing back.
Confusing printing a value with returning it.
Initializing an accumulator incorrectly.
Starting a running total at something other than zero.
And off-by-one errors when iterating with a loop.
Mini example, pseudocode.
Function add_two a, b result equals a plus b.
Return result.
add_two three, five, slash, slash returns eight.
Because you have already studied loops and the accumulator pattern, you can now generalize this to summing an arbitrary list of numbers by combining a function with a loop.