Python: An introduction to functions, part 2

We last saw how to write a function in Python. Let’s take a step back to consider how a function works with variables provided to it.

Functions can accept input values and return new values. In Python, input values are passed into the function using local variables inserted in the parentheses (). For example, the fahr_to_kelvin() function below receives the input variable temp, performs a computation using this variable, and returns the result:

1
2
3
4
5
6
def fahr_to_kelvin(temp):
    """Convert temperature in Fahrenheit to Kelvin """
    return ((temp - 32) * (5/9)) + 273.15

print(fahr_to_kelvin(5))
print(temp)

We say that the variable temp is local to the function because it only ever exists in the scope of the function. That is, outside of the function the variable temp is not defined. Calling the function for the value 5 produces the output 258.15, but trying to print the variable temp produces a NameError:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
258.15

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-e3951874915c> in <module>()
      3     return ((temp - 32) * (5/9)) + 273.15
      4 print(fahr_to_kelvin(5))
----> 5 print(temp)

NameError: name 'temp' is not defined

This means that a function works with information (i.e. local variables) defined in its scope. This idea is important to understand because a variable defined outside the scope of a function is a totally different variable to a local variable defined within the scope of a function, even if both these variables have the same name.

Consider the following example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
temp_f = 0
temp_k = 0

def fahr_to_kelvin(temp_f):
    temp_k = ((temp_f-32)*(5/9)) + 273.15
    return temp_k

print(fahr_to_kelvin(5))
print(fahr_to_kelvin(44))
print(fahr_to_kelvin(32), '\n') # where '\n' prints an empty line

print(temp_k)

# which produces the following output:
258.15
279.82
273.15 

0

Why do print statements calling the function fahr_to_kelvin() print out big values, but the print statement of the variable temp_k at the end prints out 0? This is because the variable temp_k in line 2 is defined outside the scope of the function, whereas the local variable temp_k in line 5 is defined within the scope of the function. The print statements in lines 8-10 print the values assigned to the local variable temp_k from computations within the function, but the print statement in line 12 prints the value of the variable temp_k that was defined outside the function. So, the variable temp_k in line 2 is not the same variable as temp_k in line 5, even though both these variables have the same name.

Global variables. A variable defined outside a function is a global variable and can be accessed everywhere in a program, including inside the function. In the example, temp_k in line 2 is a global variable, whereas temp_k in line 5 is a local variable.

The concept of function scoping is important to understand because we need to differentiate between variables defined outside and inside functions.

Summary

A function works with variables defined in its scope or in the global space. A variable defined outside a function is not the same variable as one defined within the function, even if both these variables share the same name. A variable defined within a function cannot be accessed outside the function.

Next, we will write a function to analyse some data.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s