How to create a Function in Python

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.

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. It also specifies whether the function should have some input parameters. 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 explains what the function does.

Function Python
  • 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

How to create a Function in Python? – Syntax

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:

 

Syntax:

 

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

Let’s take a closer look at the code above. At first, we define our function, set the name we want it to have, and choose the input parameters

 

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.

How to create a function in Python – Example

Let’s take a short and 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)
print(result)

We will get the result

 

15

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

 

help(my_function)

Will print the result

 

Help on function my_function in module __main__:

my_function(x)
    Multiplies the input parameter by 3