Functions

Scope, parameters and arguments

A parameter, also called a formal argument, is a variable that is used within a function.

Scope

We looked at scope in Week 1 when learning about variables. Declare variables in the narrowest possible scope. For example, if a variable is used only inside a specific if statement, declare it inside that if statement rather than at the top of the function or the top of the Python file / class. 

Variables in functions only have local scope. This means they can only be accessed from inside that function.

Global variables can be accessed from any location of a program. Due to the nature of Global variables, they should be used with careful consideration. You should never use the global keyword, which allows functions to have access to global variables. Instead, pass parameters to the function and / or return a value from the function.

It is important to note that:

  • Variables declared outside a function (in your main block of code) cannot be accessed directly inside the function unless explicitly passed as arguments to the function.
  • Variables declared inside a function cannot be seen from or used outside the function.

Parameters and arguments

To pass values into a function, we use parameters / arguments.

To pass values out of a function, we use a return value.

Run the following code
# This function takes one parameter, a name. The variable name can now be used inside the function.

# The return value for this function is the string constructed by concatenating hello and the name that was passed in as a parameter.

def greet_person(name):
# Create a local variable for use inside the function.
greeting = 'hello ' + name # Another way to put a space between words is by adding a space to the end of a string.
return greeting

When we call a function with parameters, we can pass a value to it.

When we pass a value to a function, we call it an argument.

The argument we are passing to the function greet_person() is the string Leslie.

def greet_person(name):
greeting = 'hello ' + name
return greeting

print('Using greet_person() with a string passed in as an argument.')

print(greet_person("Leslie")) # This line tells the Python interpreter to print whatever was returned by the function greet_person() when it is passed the argument "Leslie". In this case it will output hello Leslie.

print('')

We have passed a value directly into a function. We can also pass the value contained in a variable as an argument to a function.

In the following example, we will set three variables. Then, we will call our function three times, passing a different variable as an argument each time.

It is important that you understand that it is the value contained in the variable that is passed, not the variable itself. However, in normal speech we just say we are passing the variable.

def greet_person(name):
greeting = 'hello ' + name
return greeting

# Set three variables.
name1 = 'Jess'
name2 = 'Tracy'
name3 = 'Haru'

print('Using greet_person() with a string value contained in a variable passed as an argument - three examples.')

print(greet_person(name1)) # Here we are passing the value stored in name1 as the argument to the function greet_person(). Jess is stored in name1, so inside the function the parameter name will contain Jess.

print('')
print(greet_person(name2))
print('')
print(greet_person(name3))
print('')

If a function has a parameter, and no argument is passed to the function when it is called, this will cause an error.

def greet_person(name):
greeting = 'hello ' + name
return greeting

print('Using greet_person() with no argument passed in. Parameter is required, it will cause an error')

print(greet_person()) # If no argument is passed to the function, it will cause an error.
print('')