Writing Functions with Parameters

Let’s write and call our own functions now, with parameters!

The basic format is:

def function_name(parameters):
    # code here
    # notice that it is indented
    # everything indented here is inside this function

We can give the function as many parameters as we want to in a comma separated list.

Before we go on, let’s refine “parameter” a little more. (Adding to our definition from the previous section on using functions.)

parameter: information passed to the function when it is called. All parameters are given a name in the function definition. The name is how we will refer to the value that was passed to the function when it was called.

Notice that parameters are similar to, but not the same as, variables.

# TODO: run this code

# this is a function definition that takes one parameter
# when the function is called, the parameter x takes the 
# value given to the function
def times_two(x):
    ans = 2 * x
    print(ans)

# Parameters let our functions perform the same operations, 
# but on different values (making them more flexible!)
# TODO: run this code, add more examples of your own.

times_two(7)
times_two(100)
times_two(-1.2)
print()

# we can use variables as parameters when we are calling a function
a = 13
# this calls the function times_two, passing in the value of a (13)
times_two(a)
# this calls the function times_two, 
# passing in the value of a + 5.4 (18.4)
# Notice that if we have a mathematical expression,
# this math will be resolved (evaluated) before the
# value is given to the function
times_two(a + 5.4)

Parameters and Ordering

Let’s look at an example of a function that takes two parameters so that we can talk about how ordering affects parameters.

def subtract(x, y):
    s = x - y # notice that we subtract y from x and never x from y
    print(str(x) + " - " + str(y) + " = " + str(s))

When we call this function, the first value that we pass to it will be given to the parameter x and the second value that we pass to it will be given to the parameter y.

Let’s play around with some examples.

# TODO: run me to get the definition of subtract read 
# into your python kernel
def subtract(x, y):
    s = x - y # notice that we subtract y from x and never x from y
    print(str(x) + " - " + str(y) + " = " + str(s))

# TODO: run this code.
# Are the results of these two function calls the same?
# Answer:

subtract(10, 7)
subtract(7, 10)
print()

# What about if we pass in values via variables instead of directly?
a = 4
b = 99
subtract(a, b)
subtract(b, a)

To reiterate: no matter what, the value that you pass in first will be assigned to the first parameter, second to the second parameter, and so on and so forth.

Scope

Let’s talk about scope, but first, an informal definition:

scope: where you have access to which variables and parameters. This also refers to where you can change the values of which variables and parameters.

An example without functions:

x = 1
y = 2
print(x)
print(y)
# print(z)  # ERROR because z doesn't exist here
z = 3  # lines after this one are where z is in scope
print(z)
x = x + 1 # x is still in scope, we can still access it

Of course, the picture becomes more complicated when we start involving functions. Let’s use our subtract function from above.

def subtract(x, y):
    s = x - y # notice that we subtract y from x and never x from y
    print(str(x) + " - " + str(y) + " = " + str(s))
    
a = 10
b = 7
subtract(a, b)

What would happen if we tried to access the variables x and y outside of subtract?

# TODO: Run this code. What happens?
# Answer:
def subtract(x, y):
    s = x - y
    print(str(x) + " - " + str(y) + " = " + str(s))
    
a = 10
b = 7
subtract(a, b)
print(x) # notice that we added these lines!
print(y)

This causes an error because x and y are only in scope inside of the subtract function.

In general, inside functions, you should only be accessing parameters or variables that you defined inside the function.

Now let’s take a look at what happens if you also have variables named x and y outside of the subtract function.

# TODO: run this code. 
# What happens when you pass the variable x in as the 
# second parameter rather than the first?
# Answer:
def subtract(x, y):
    s = x - y
    print(str(x) + " - " + str(y) + " = " + str(s))
    
x = 10
y = 7
subtract(x, y)
subtract(y, x)  # What happens here?

# TODO: try some other combinations out
subtract(x, x) # for example

What’s happening here is that rather than passing in the variable x, we are passing the value of the variable x. The same thing goes for y.