Video Lesson:
Lesson Transcript:
So far, the functions we've created execute a fixed task without context / inputs. But most often we want our functions to produce different outcomes depending on the context behind the function call.
For example, it would be nice if say_hello() would print a personalized message such as "Hello humans" or "Hello cats" or "Hello dogs", depending on the audience instead of "Hello world" no matter what.
Function parameters:
Function parameters allow functions to accept input(s) and perform a task using the input(s). We use parameters as placeholders for information that will be passed to the function when it is called.
When a function is defined, its parameters are specified between the parentheses that follow the function name.
Here is our function with one parameter, audience:
def say_hello(audience):
print("Hello " + audience)
Calling with arguments:
Then we can call say_hello and specify the values in the parentheses that follow the function name. The values that are passed to the function when it is called are called arguments.
# Function declaration:
def say_hello(audience):
print("Hello " + audience)
# Function call:
say_hello("humans") # Output: Hello humans
We have passed one string argument: "humans". Inside the function audience will equal string "humans" and acts just like a regular variable.
Multiple parameters:
A function can take as many parameters as it needs. Here is a function with two parameters:
def printPerson(name, age):
print(name + ", aged " + str(age))
# Let's call it:
printPerson("John", 30) # Output: John, aged 30
printPerson("Mary", 26) # Output: Mary, aged 26
Notice that the order in which arguments are passed and assigned follows the order that the parameters are declared.
Variables as arguments:
Variables can also be passed as arguments for function calls:
def printPerson(name, age):
print(name + ", aged " + str(age))
name = "Andy"
age = 28
printPerson(name, age) # Output: Andy, aged 28
printPerson("Mary", age) # Output: Mary, aged 28
Assignment
Follow the Coding Tutorial and let's write some functions.
Hint
Look at the examples above if you get stuck.
Free plan: 30 tutor messages. PRO is unlimited.
