Functions In Python Language

Watch video presentation and remember to subscribe to our channel;

Watch now!
In this tutorial we will be discussing use of functions in python. There are three types of functions in python that provide a functional programming style they include; the map(), filter(), and reduce() functions.


Function – function in python refers to the block of codes that are organized and reused to perform either single or related task, they only run when they are called.

How to create function in Python?

Functions in python are defined using def keyword, whereby you can also include parameters into a function to return a certain result. For instance we may have example here;

def my_function():
print("Hello, i am learning functions")

How do you call a function?

In order for one to call a function you need to write the function followed by parenthesis, as shown in the following block;

def my_function():
print("Hello, i am learning functions")

Arguments and Parameters

Parameters -a parameter refers to the variable that is listed between the parentheses when your are defining a function.

Arguments -a argument refers to the value that is sent when a function is called.

-Generally arguments and parameters as used in function are the information that is passed into a function.

-You can include many arguments inside a function as many as you wish, whereby they are only separated by comma, let’s have an example here for better understanding;

def my_function(school):
 print(school + "University")

In the above example we have used only one argument which is (school), when the function is called then we pass the name of the university then it prints the full name for the university. For instance, maseno university.

How many arguments should I write?

The number of the arguments passed into your function should be the same as the number of the arguments called inside the function. For instance, if you pass 3 arguments into a function then it means you will write 3 arguments when calling the function, if you write 2 or either 4 then it will bring an error. Look at this example here;

def my_function(name, age, country):
 print(name + " " + age + " " + country)
my_function("Philloh", "22", "Kenya")

How do I set a default parameter value?

When you are passing your arguments you can include a default parameter. Therefore, if it happens you call a function but you don’t include the argument then the default parameter is printed out. Look at this example here;

def my_function(name = "Philloh"):
 print("My name is " + name)

In the above example two people will have the name philloh since there are no arguments included when calling the function.

How do I pass a list as an argument?

A list may include a number, set, tuple, string or any other data type. If you pass any of these data types into a function, then they will be same as the data type in the function. Let’s have an example here for better understanding;

#ths is a string
def my_function(name):
 for y in name:

#this is a list
persons = ["philloh", "John", "Mary", "Simon"]

How to write return values?

We uses return statement for the function to return a value especially when you are doing arithmetic calculations. For instance, look at this example here;

def my_function(y):
return 10 + y

Suppose you do not know the number of arguments should be passed?

We use * before the parameter name when you are defining the function, if you don’t know the number of arguments that are passed into a function as shown in the following example;

def my_function(*cars):
 print("The expensive car is " + cars[3])
 my_function("BMW", "Toyota", "Ford", "Mercedes")

In the above example it will produce Mercedes as the most expensive car, since when we use the concept of arrays the counting starts from zero and Mercedes is in number 3 in the list.

How to pass keyword arguments?

We can use key=value to send arguments, as shown in the following example and not must to be in order check out;

def my_function(country1, country2, country3, country4):
 print(I am from " + country4)
 my_function(country3 = "Tanzania", country1 = "Zimbabwe", country4 = "Kenya", country1 = "Uganda")

In that case above it will produce I am from Kenya

Use Pass Statement in functions

We uses pass statement to avoid error which may be as result of function being left empty when defining it. Look at this example here;

def my-function()

Thanks for reading through our tutorial, as we continue learning python hoping to meet you in our next tutorial.