Parameters and Arguments

A function becomes genuinely useful when it works on different input each time.

A parameter is the name in the definition, the placeholder. An argument is the actual value you pass when calling. The parameter is the empty seat; the argument is who sits in it.

Parameters

PYTHON

def greet(name):
    print("Welcome, " + name)

greet("Ama")
greet("Kofi")

JAVASCRIPT

function greet(name) {
  console.log("Welcome, " + name);
}

greet("Ama");
greet("Kofi"); 
Compare → Identical structure. `name` is the parameter; "Ama" and "Kofi" are arguments. Inside the function, name holds whatever was passed on that particular call.
EXAMPLE
loading editor…
YOUR TURN
Your turn → Multiple parameters are matched by POSITION, first argument to first parameter. Swap the arguments and see the sentence break.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

Default arguments let a parameter fall back to a value when no argument is given.

EXAMPLE
loading editor…
YOUR TURN
Your turn → Identical syntax in both languages. Add a third parameter with its own default.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: return: Handing a Value Back →
← Back