Section 3: Functions

Welcome to Section 3! In this section, we will cover the basics of functions. Let's get started!

3.1 Functions

In the previous sections, we have learned about many different in-built functions that Python has that makes our jobs as coders much easier. However, Python only has a certain amount of in-built functions, so sometimes, we want to create our own functions. We create functions if we want to preform a multi-step action many times. Here is an example of a function ...

def print_stuff(a, b, c, d):

print(a)

print(b)

print(c)

print(d)

print_stuff(1, 2, 3, 4)

print_stuff(5, 6, 7, 8)

print_stuff(9, 0, 1, 2)

What this code will do is run lines 2-5 3 times with the inputs in the last 3 lines. If we were to do that without a function, we would need 12 lines, but this code does that in only 8 lines.

The function is between lines 1 and 5. The syntax of a function is a little complicated. The first line will start with "def" and end with "():". before the parentheses, you will put the name of the function. The name will follow the same rules as a variable name. If you want to, you can add in variables between the parentheses and separate them with commas. These variables have to be inputted by the user and are called parameters. Every line after the first line of a function must be indented by the same amount.

The last three lines of the code above are calling the function. To call a function you need to first type the function's name and then type "()". If the function has any parameters, you must either enter pre-existing variables or any data between the parentheses in the correct order.

Sometimes, we want a function to return something instead of printing something. This way, we can save the output to a variable for later use. Here is an example of that ...

def addition(a, b):

return a + b

c = addition(1, 2)

This function will take to variables and return the sum of them. The variable c is now created with a value of 3. The code doesn't output anything.

Now that you have functions down, let's move on to the next part!

3.2 Importing Modules

We can't always make our own functions, and Python doesn't always have what we need. This is when we can use modules. Modules are databases which have many functions inside them that you can use. There are hundreds of modules in Python that combined have thousands of functions. Here is an example ...

import random

print(random.randint(1, 10))

The first line imports the module random, and randint is one of its functions. You just have to type "import" and type the module's name. To call a function from a module, you have to type the name of the module, add a period, and type the functions. You will need to put in the required parameters inside the parentheses if the function requires that.

Now that you have importing modules down, let's move on to the next part!

3.3 Variable Scope

When creating a variable inside a function, you have to remember that the variable created cannot be used outside the function. Here is an example ...

def variable():

test_variable = 5

test_variable = 3

variable()

print(test_variable)

Before we call the function, we set test_variable is set to 3. Then, we call the function which sets test_variable to 3. We would think that when we print test_variable it would return 3. However, it would actually return 5. This is because variables defined in a function are called local variables, and they do not affect anything outside of the functions. Variables defined outside a function are called global variables.

Here is one way to make a variable defined inside a function a global variable ...

def variable():

global test_variable

test_variable = 5

test_variable = 3

variable()

print(test_variable)

This code is almost identical to the previous code, but this code has an extra line inside the function which makes test_variable a global variable. The global function makes this code output 5 instead of 3.

Now that you have variable scope, let's move on to the next section!

Sample Problems