LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.
Lesson 27: Functions in Python
A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.
There Are Four Types of Functions in Python
1. Python Function with no argument and no return value.
2. Function with no argument and with a Return value.
3. Python Function with argument and No Return value.
4. Function with argument and return value.
Note: 1 and 3 types of functions in Python do not return any value when the function called.
So, while defining the function, we can avoid the return keyword.
When we call the 2 and 4 types of functions in Python, they return some value. So, we have to
use the return keyword.
Type1. Python Function with no argument and no return value.
While defining, declaring, or calling the function, We won’t pass any arguments to the function.
This type of Python function won’t return any value when we call the function.
Whenever we are not expecting any return value, we might need some print statements as output. In such a
scenario, we can use this type of functions in Python.
Example Code1: Python Function with no argument and no return value.
output will be
After Calling the Function: 80
Type2. Python Function with no argument and with a Return value.
In this type of function in Python, We won’t pass any arguments to the function while defining, declaring, or calling the function. When we call the Python function, this type of function returns some value. In this type of function in the Python program, we are going to calculate the multiplication of 2 integer values using the user defined function without arguments and return keyword.
Example Code2: Python Function with no argument and with a Return value.
# Python Function with No Arguments, and with Return Value
output will be
After Calling the Multiplication Function: 300
Type3.Python Function with argument and No Return value.
This type of function in Python allows us to pass the arguments to the function while calling the function. But, This type of function in Python won’t return any value when we call the function.
Example Code3: Python Function with argument and No Return value.
output will be
After Calling the Function: 400
Note: We called the Multiplication function with different values, and it is giving the output as per the values.
Type4. Python Function with argument and Return value. This type of python function allows us to pass the arguments to the function while calling the function. This type of functions in Python returns some value when we call the function. This type of user defined function called a fully dynamic function means it provides maximum control to the end-user.
This type of function in the Python program allows the user to enter 2 integer values. Then, we pass those values to the user-defined function to add those values and return the value using the return keyword.
Example Code4: Python Function with argument and Return value.
output will be
After Calling the Function: 75
More Example of Python function()
Creating a Function: How to Creat a Function in Python?
Example Code5: In Python a function is defined using the def keyword.
Calling a Function: How to Call a Function in Python?
output will be
>
Example Code6: To call a function, use the function name followed by parenthesis.
output will be
Hello from a function
Arguments: How to pass arguments into functions?
Information can be passed into functions as arguments.
Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.
The example below has a function with one argument (fname). When the function is called, we pass along a first name, which is used inside the function to print the full name.
Example Code7: How to pass arguments into functions?
output will be
Peter John
Paul John
Harry John
Note: Arguments are often shortened to args in Python documentations.
Parameters or Arguments?
A parameter is the variable listed inside the parentheses in the function definition
An argument is the value that is sent to the function when it is called.
The terms parameter and argument can be used for the same thing: information that are passed into a function.
Number of Arguments
By default, a function must be called with the correct number of arguments. Meaning that if your function expects 2 arguments, you have to call the function with 2 arguments, not more, and not less.
Example Code7: A function must be called with the correct number of arguments.
This function expects 2 arguments, and gets 2 arguments.
output will be
Peter John
Note: If you try to call the function with 1 or 3 arguments, you will get an error see example below.
Example Code8: This function expects 2 arguments, but gets only 1.
output will be
TypeError: my_function() missing 1 required positional argument: 'lname'
Arbitrary Arguments, *args
If you do not know how many arguments that will be passed into your function, add a * before
the parameter name in the function definition.
This way the function will receive a tuple of arguments, and can access the items accordingly.
Example Code9: If the number of arguments is unknown, add a * before the parameter name.
output will be
The youngest child is Harry
Note: Arbitrary Arguments are often shortened to *args in Python documentations.
Keyword Arguments
You can also send arguments with the key = value syntax. This way the order of the arguments does not matter.
Example Code10
output will be
The youngest child is Harry
Note: The phrase Keyword Arguments are often shortened to kwargs in Python documentations.
Arbitrary Keyword Arguments, **kwargs
If the nubers of keyword arguments that will be passed into your function is not known, you can add two asterisk: ** before the parameter name in the function definition. This way the function will receive a dictionary of arguments, and can access the items accordingly.
Example Code11: In case the number of keyword arguments is unknown, add a double ** before the parameter name.
output will be
His last name is John
Note: Arbitrary Kword Arguments are often shortened to **kwargs in Python documentations.
Default Parameter Value: How to use a default parameter value?
If we call the function without argument, it uses the default value The following example shows how to use a default parameter value.
Example Code12-a: Calling the function with arguments.
output will be
I am from india
I am from Sweden
I am from Norway
Example Code12-b: Calling the function without arguments.
output will be
I am from india
Passing a List as an Argument
You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.
Example Code13: if you send a List as an argument, it will still be a List when it reaches the function.
output will be
apple
grapes
orange
dates
Return Values
Example Code13: To let a function return a value, use the return statement.
output will be
21
35
63
The pass Statement
A function definitions cannot be empty, but if you for some reason have a function definition with no content, put in the pass statement to avoid getting an error.
Example Code14: how to put in the pass statement to avoid getting an error..
output will be
>
You will get an empty screen without any error.
Recursion: What is Recursion Recursion is a common mathematical and programming concept. It means that a function calls itself. This has the benefit of meaning that you can loop through data to reach a result.
The developer should be very careful with recursion as it can be quite easy to slip into
writing a function which never terminates, or one that uses excess amounts of memory or
processor power. However, when written correctly recursion can be a very efficient and
mathematically-elegant approach to programming.