Python Basics — Functions
In this lesson, we are going to learn one of the most fundamental concepts in programming, functions. Functions help us to write a piece of code once and call it several times. Understanding the concept of functions and their usage will improve your programming skill dramatically and make your life easier as a programmer. So, Let’s dive in and learn how to implement functions in Python.
What are functions?
Generally, a function is a group of related statements that perform a specific task. If we want to write clean, organized, and manageable code, especially in a large program, we have to break our program into smaller chunks. In computer programming terminology, these chunks are called functions. Functions make our code reusable and prevent repeating ourselves.
Functions are self-contained modules of code that accomplish a specific task. Functions usually “take in” data, process it, and “return” a result. Once a function is written, it can be used over and over and over again. Functions can be called from the inside of other functions.
School of Computing, University of Utah
Function Syntax in Python
functions begin with the keyword def which marks the function header. A function name is used to identify the function uniquely. Function naming follows the same rules of naming variables in Python. Then a pair of parenthesis, and within those are parameters which we pass values to a function through them. They are optional. Lastly, we need an ending colon to mark the end of the function header. The following handwritten sketch shows the syntax of functions in Python.
How to Write a Simple Function
So far, you have learned the structure of a function in Python. Now, Let’s write our first function.
Go ahead and run the cell. We define a function called, which prints 'Hello World!'
each time it is called. In the last four lines, we call the function four times. So, running the code print out 'Hello World!'
four times.
You can say this function is totally useless, but in the next lessons, you will find how useful functions can be for writing concise and efficient programs.
Function Life Cycle
In Python, the life cycle of functions has two stages. The first stage is the function definition, and the second stage is the function call, Let’s take a look at the function life cycle in the following sketch:
When you call a function, it runs the function body with the function definition.
There are two types of functions, built-in, and user-defined functions. Functions such as
print()
,len()
,sum()
, etc are built-in functions. However, functions we define as programmers, called user-defined functions or UDFs.
Parameters
We need to use parameters when calling a function with different values. Technically, the parameter name that should be stated within the pair of parentheses is an arbitrary variable name that we use to reference the value within the function body. When we call the function, we pass in the necessary value to run the function block.
Let’s see how we can write a function that accepts a parameter.
The printHelloName
function is defined with a parameter, name
, within the parenthesis. When executed, the function block will use the parameter's value within the print
statement. The function call on line 3 passes the value into the function, known as an argument. This piece of code prints out "Hello Mehdi"
. This function can be called with any string value that we would like.
Sometimes, we need to pass multiple values to a function and process them into the function body. Let’s check out the following example:
Go ahead and run the cell. The roomArea
function is expecting two numbers to be passed into the parameters width
and height
. Within the function block, we reference these values passed in by their argument names and calculate the area of a room.
In Python, passing lists to functions is easy and straightforward. Let’s see an example:
Go ahead and run the cell. You can see that it outputs the sum of the numbers in num_list
.
Alright, I hope you enjoyed this lesson. In the next lesson, you will learn more about functions in Python.