Functions

Functions with more than one parameter

Functions can take more than one parameter. When you call the function, you must give the arguments in the same order as the parameters. Any optional parameters should be listed last.

In the following example, when we call the function, we are passing it two arguments, which are the values stored in the variables named greet1 and name1. Inside the function, the parameter greet_type will contain the value that was stored in greet1 and the parameter name will contain the value that was stored in name1.

Note that it is the value stored in the variable that is passed to the function, not the variable itself. You cannot use the variable names greet1 and name1 inside the function; the function cannot see them, as they are outside the scope of the function.

Run the following code
# Define the function.
def create_greeting(greet_type, name):
greeting = greet_type + ' ' + name # Note that the + operator is used for concatenation, and a space has been inserted.
return greeting

# Set the variables.
greet1 = 'Hello'
greet2 = "G'day"
greet3 = 'Ciao'
name1 = 'Jess'
name2 = 'Tracy'
name3 = 'Haru'

# Call the function.
print('Using create_greeting() with two parameters, greet_type and name')
print(create_greeting(greet1, name1)) # This line will print the value returned by the function create_greeting().
print('')
print(create_greeting(greet2, name2))
print('')
print(create_greeting(greet3, name3))
print('')

The call site of a function or subroutine is the location (line of code) where the function is called. A call site is where zero or more arguments are passed to the function, and zero or more return values are received.

In the lines of code above, create_greeting() is called in the print() statement, and is passed two arguments each time. create_greeting() returns a value, which is a string in this case, and that is what the print() statement outputs to the screen.