Functions in Python

Greetings! Some links on this site are affiliate links. That means that, if you choose to make a purchase, The Click Reader may earn a small commission at no extra cost to you. We greatly appreciate your support!

A function in Python is a block of code packaged together to perform a single related action.

The name ‘function’ comes from the fact that a block of code is trying to achieve a single functionality in the program. The main advantage of writing and using functions in Python is that it allows programmers to break their tasks into a group of reusable code.

There are two kinds of functions in Python: built-in functions and user-defined functions.

Built-in functions in Python

Built-in functions in Python are functions that come pre-written when you install Python and are readily available to use when coding.

You’ve already come across some built-in functions like print() and type() earlier on in this course. Here is a list of some commonly used built-in functions in Python.

Function NameFunctionality
printPrints the specified message
typeReturns the class type
lenCalculates the length of a string
absReturns the absolute value of a number
formatConverts a value to a formatted representation

User-defined functions in Python

User-defined functions in Python are functions that are defined by the user to perform a custom task. The general syntax for writing a user-defined function in Python is as follows:

def function_name(parameters):
   return [expression]

Let us go over all of the lines mentioned in the above syntax and understand clearly about what they mean:

  1. Functions in Python are created using the def keyword alongside a function_name which marks the name of the function.
  2. The function_name is followed by a set of parenthesis inside which we can pass various parameters. These parameters are the input to the function and are optional while creating a function that doesn’t need any input.
  3. A colon (:) marks the end of the function header and any line of code that is part of the function is written with an indentation.
  4. The docstring is a multi-line comment that describes what the function does. It is optional but writing docstrings helps in setting proper documentation for user-defined functions.
  5. The actual lines of code are denoted by statement(s) and they build up the functionality of the function.
  6. The return keyword is used to return back any output when the function is used.

Once a function is written, it is very easy to use it. The syntax for using a function or ‘calling’ the function is as follows:


As you can see, we can simply write the function name followed by a set of parenthesis to call a function. Any input to be supplied to the function is passed in the form of parameters which is written in between the set of parenthesis.

Example of a user-defined function in Python

Now, that we know the general syntax of defining and calling a function, let us learn more about it using a concrete example.

The function below calculates the sum of two numbers and returns back the output.

def sum_func(a, b):
    This function takes in two parameters and returns their sum.
    total = a + b
    return total

Now, to call this function, we can simply call the function and pass two values we would like to add through it.

output = sum_func(4, 6)

The following things are happening when the sum_func is called:

  1. The value of a variable is set as 4 and the value of b variable is set as 6.
  2. The total variable is calculated as 4 + 6 = 10.
  3. The total variable is returned back from the function and the output variable gets assigned with the value of the total variable, i.e. 10.

Local and Global Variables

Whenever we create a variable inside of a function, the scope of the variable gets limited to that function. This means that the variable can only be used within the function it is defined at. Such kinds of variables are known as local variables.

In the above sum_func function, the variable a and b can only be used within the function itself. If we try to use these variables outside of the function, we will receive an error stating that the variables are not defined. This is because the scope of the two variables is limited to the sum_func function.

On the other hand, if we declare a variable outside of a function, the scope of the variable is the entire program. This means that we can use it inside of user-defined functions as well. However, we cannot change the value of such variable from inside of a function.

To change a variable declared outside of a function from within a function, we must use the global keyword when declaring the variable. Such kinds of variables are known as global variables. Here is an example of a global variable:

global a = 4

def sum_func(b):
    This function takes in two parameters and returns their sum.
    total = a + b
    return total

output = sum_func(6)

That is it for this lesson on Python functions.

Leave a Comment