Core programming fundamentals · Lesson 11 of 60 · Code lesson, JavaScript or Python · Optional
Functions and procedures
A function is a named piece of code you can run whenever you need it.
A parameter is a name for a value the function receives, an argument is the value you pass in, and return sends a result back.
A function that only does something, without returning a value, is often called a procedure.
Worked example
In JavaScript, function add(first, second) { ... } defines the function, and add(2, 3) calls it with two arguments.
function add(first, second) {
return first + second;
}
console.log(add(2, 3));Output: 5
In Python, def add(first, second): defines the function, and add(2, 3) calls it with two arguments.
def add(first, second):
return first + second
print(add(2, 3))Output: 5
Your turn
Make greet use its name parameter, so greet("Ada") returns "Hello, Ada!" and greet("Mia") returns "Hello, Mia!".
Expected output:
Hello, Ada! Hello, Mia!
Loading your practice…
Preparing your workspace…