How to create a Function in Python

Keywords: function, def, input parameters, top-down programming

Python Programming

A function is defined (created) easily in Python by specifying which name the function should have and if the function should have some inparameters. A function is always terminated with the return command (return a result), or if all operations in the function have been performed.

  • A method is a sequence of operations that is performed when called
  • Methods can get input parameters and return values
  • The method is independent of the other code in the program

Create a function in Python

Creating a function in Python is very simple

  • When you define (create) a function, you use the reserved word def followed by the function name, and also specifies whether the function should have some input parameters that you put within parentheses.
  • Everything that is supposed to be performed when the function is called is coded with indentation.
  • The function ends with the reserved word return which terminates the function and returns a final value.
  • If return is not used, the function runs until the last operation is completed. Additionally, you can also use return without returning any value.
  • Finally, can also add a function description “function documentation” that, for example, explains what the function does and why

Syntax: Declare a function in Python

When creating a function, you need to specify the input parameters, and whether the function will return any values. We can design our function in several ways:

def function_name(input parameter):
    “Function Documentation”
    what the function should perform
    return value

Let’s take a closer look at the code above.

  • At first, we define our function by using the command def
  • set the name we want it to have, in our case, function_name
  • Finally, select the input parameters that is within the parentheses
  • End it with a colon (:)
def function_name (input parameters):

Then, on line 2, we added a Function Documentation where we can explain what the function actually does/purpose

   “Function Documentation”

Furthermore, the third line of code in our syntax above is where we write what the function should do, in other words, the operations it should perform.

   what the funct should perform

Finally, we end the function by using the reserved word return and the value that the function should return.

   return value

Note: all the code that is “inside” the function is written indented.

Encapsulation: Keep your code separate

An essential principle in all programming is encapsulation. This means that each part of the program should “take care of its own”. Instead of writing large functions that handle different things, you encapsulate every small part of the program.

Imagine a function as a piece of the puzzle, where each piece of the puzzle makes the “smallest possible” calculation. Through this important principle, you can combine the logic from several different functions that together build large programs. Programming is thus like a jigsaw puzzle of functions combined to create large and intelligent programs.

encapsulation python functions

Example: How to create a function in Python

Let’s take a simple example just to illustrate how we can use a function.

def my_function(x):                       
    "multiplies input parameter with 3"    
    return 3 * x

So the only thing  the function, that we named my_function, is to take the input parameter and multiply that value by 3. Additionally, the function should after that then return the value

If we try our function by sending the value 5 as input parameter

result = my_function(5)

We will get the result


If you are not sure what the function actually does, you can call the function description with help () that returns the function documentation:


Will print the result

Help on function my_function in module __main__:
    Multiplies the input parameter by 3

Summary: Create functions in Python

Functions in Python is a powerful tool for reusing and structuring our code. We can use functions to simplify and break down our programs into smaller parts, and then solve the smaller pieces that together build the complete solution. Moreover, using a function is denominated as calling it (as we will look closer at in the coming article), and the result of that call is named what the function returns. When you declare a function, you need to specify the input parameters the function should receive and use and what the function should return. 

Syntax: Declare a function in Python

def function_name(input parameter):
    “Function Documentation”
    what the function should perform
    return value