Skip to content
Ryan James
Menu

← Python10 min read

Defining Functions

Functions are probably one of the best things that you can include in a program and the amount that they can simplify a program can never be overstated. In this section, creating and using functions in Python is going to be explored. Functions are really useful when there is a block of code that needs to be referred to multiple times throughout the program.

Defining Functions

Functions are probably one of the best things that you can include in a program and the amount that they can simplify a program can never be overstated. In this section, creating and using functions in Python is going to be explored. Functions are really useful when there is a block of code that needs to be referred to multiple times throughout the program. It is one of the main ingredients when implementing a top-down design approach because the names of the functions are one of the first steps in planning the design.

Creating Functions

def hello():
   print("Hello")

hello()

In the example shown above is a very simple function. When this function is called, which is the last line, the word ‘Hello’ is printed to the screen. Notice how the function is set up and how the function is called. These features go for every function in Python.

In the following example, the ‘hello’ example has been extended to include a parameter. In Python, there is no limit to how many parameters or what type of parameters a function can have. If the word parameter is new, think of it as the input for a function.

def hello(name):
   print("Hello {}!".format(name))

myName = "Ryan"
hello(myName)

Using the code in the segment shown above, the String “Ryan” will be passed on to the hello() function. By doing so the function will print the String “Hello Ryan!” to the screen.

Functions can also do more than just print to the screen. They can also return values.

def sum(a, b, c):
   return a + b + c

num1 = 2
num2 = 5
num3 = 9
total = sum(num1, num2, num3)
print("The sum of these numbers is {}.".format(total))

In the example shown above, the function sum() is called and the values that are passed into the sum() function are added together and using the return statement, the output of the function is assigned to the variable total. Then using a print statement, the state of total is printed to the screen. This is a good example of when a function can have multiple parameters and how it uses return statements to get data out of the function. It’s also worth noting that a function can return any data type, there are no limits to what kind of output the function can have. If a function ever doesn’t return properly, then its output will be of type, ‘None’. You will know when this happens because you will generally get an error message at run time.

So far, you’ve seen examples of creating functions with and without parameters as well as how to return data types out of a function. But, one thing that may not be totally clear is how the variables inside a function work.

def local():
   localVar = 13
   return localVar

temp = local()

In the code segment shown above, the function local() is called and it returns the number 13 and assigns it to the variable temp. But one thing that you may not be aware of is that when the function returns the value stored in localVar, this variable ceases to exist. If you try to print the variable localVar, then you’ll get an error at run time stating that this variable was referenced before assignment, meaning that the program can’t find the variable. Whenever you define a variable for the first time inside of a function, then that variable is called a local variable and is local only to that function. It doesn’t exist outside of that function. A way to solve this problem is to use global variables.

temp = 0

def local():
   global temp
   temp = 13

local()
print(temp)

When the code segment above runs, first the variable temp is assigned a value of 0. Then, the local() function is called. When this is called, the first line in that function uses the keyword global to import the variable temp and its state. At this point it is still equal to 0. Then the next line in the function reassigns it to be 13. When the last print statement runs it will output the final value of temp being 13. This is the correct way to use variables inside a function that you want to carry over when the function is no longer running.

Functions are really useful and are common in just about every program that you’ll write. They will save you a lot of time if you plan them appropriately and understand how to implement them.

Keep coding!
Ryan