In this article you will learn about Python def function.
Python def function- Before moving ahead, let’s know a little bit about Python for loop
Function – A block of code or data passes as a parameter into a function, it’s known as function. In other words, a set of blocks of code or some codes is passed to the function as an argument to perform an action over function. A function runs when it is called.
Parameter – It is data or variable defined inside parenthesis – ().
Argument – It is the value of parameter.
Creating a Function – In Python, a function is created by using def keyword.
def – It is Python’s built-in function. It used to create a function in Python.
Example 1- Creation of function.
def my_func(): print('Hello, Python World')
Calling a Function – A function is run when it gets called. To call a function, use the function name followed by parenthesis:
Example 2- Calling a function.
def my_func(): print('Hello, Python World') my_func()
Parameter or Argument – While creating a function in Python, the term parameter or argument can be treated as same.
From a function’s perspective-
A parameter is passed to a function inside parentheses, it consists list of variables like x, y, and z etc.
An argument is a value of parameter passed to a function when it is called.
Creating Argument – A argument is passed after the function name and inside the parentheses. An argument can be passed as many as want to pass. A function treated argument as value of parameter or an information into function.
Example 3- Creating a function with string value argument.
def my_func(i): print(i) my_func('Hello, Python World')
Explanation – In the above example, a parameter is defined name (i) inside parentheses and it stored a value as an argument to a function. As function is called, it executed argument value as function value and returned ‘Hello, Python World’.
Example 4- Creating a function with argument.
def my_func(i): print(i + 5) my_func(10) my_func(12)
Explanation – In the above example, a parameter is defined name (i) inside parentheses and stored two different values in print() function as an argument. Inside function, print() function that stored two value as an argument, i.e., ‘i’ and 5. When function will be called, value 5 would be added to ‘i’ and will returned added value.
Note: Often in Python ‘arguments’ are written in shorthand as ‘args’.
Number of Arguments – A function normally called by default number of arguments, means the number of argument which want to pass but if a function doesn’t call to its equal number of argument then Python will give us an error.
Example 5- Calling a function with not all arguments.
def my_func(x , y): print(x+y) my_func(5)
Explanation – In the above example, the function needs the two-argument values to call but function got only one argument value, therefore Python gave us an error.
Example 6- Calling a function with its equal number of arguments.
def my_func(x , y): print(x+y) my_func(5,10)
Explanation – In the above example, function needs two argument values to call as function got two parameters. As function is called it got two values therefore, it returned a sum of both values.
Arbitrary Arguments, *args – When it is not defined how many parameters should be passed, then add an asterisk (*) before the name of the parameter. It will make sure function to receive the number of tuple argument according to its requirement.
Example 7- Check if function runs successfully when number of arguments is not defined.
def my_func(x): print(x) my_func(5,10,15)
Explanation – In the above example, it is shown clearly that the function got 3 arguments but has defined only one parameter, therefore, the function did not get an equal number of parameters as to argument, and finally it returned an error.
Example 8- If the number of arguments is not defined, add an asterisk (*) before the parameter name.
def my_func(*x): print(x) my_func(5,10,15)
Explanation – In the above example, it is not defined how many parameters should be passed but as an asterisk (*) was added before parameter name that will let know function how many numbers of parameter is needed and will avoid getting an error. By referring index number inside square brackets , we can select argument value as per choice.
Keyword Argument – An argument can be sent as key-value pair. In this way argument order doesn’t matter. In other words, a keyword is a value defined on the left-hand side and an argument is the value defined on the right-hand side followed by assignment statement (=).
Example 9- Passing an argument to a function in key-value pair.
def my_func(x,y): print('The sum of all argument is:' , x+y) my_func(y=5, x=10)
Explanation – In above example, it is shown clearly that function contains equal number of parameters and arguments therefore, it did not find any error and as per print() function it performed an action of adding both parameter values.
Arbitrary Keyword Arguments, **kwargs – If it is not defined that how many keyword arguments (kwargs) should be passed to a function then add two asterisks (**) before the parameter name. It will make sure function to receive the dictionary of arguments according to its need.
Example 10- If the number of keyword arguments (kwargs) is not defined, add two asterisks (**) before the parameter name.
def my_func(**x): print('The number is', x['z']) my_func(x = 5, y = 10, z = 15)
Explanation – In this example, as two asterisks was added before parameter that will help def function to take equal number of parameters as to argument therefore, function will not return any error and perform an action of returning parameter value of z.
Default Parameter Value – When a function called without parameter then it takes default parameter.
Example 11- Calling a function with its default parameter.
def my_func(x = 'Alex'): print('My name is ' + x) my_func()
Explanation – In this example, despite calling a function without parameter, function did not return any error because function got its default parameter therefore, function performed an action of print() function.
Example 12- Calling a function with both default parameter and additional key value pair.
def my_func(x = 'Alex'): print('My name is ' + x) my_func('Elisa') my_func() my_func('Kily')
Explanation – In above example, when function was called first and second time it took its defined parameter value but when function was called second time it did not call parameter value because it used its default value.
Passing data types as an Argument – An argument can receive any kind of data types such as list, tuple, set and dictionary etc. it will be treated as the same data type inside the function.
Example 13- Passed an argument of elements as tuple to function.
def my_func(n): for x in n: print(x) z = (1,2,3,4) my_func(z)
Explanation – In above example, a tuple that stored a number of elements passed to a function as parameter value. That later, perform an action of print() function by looping each element once in tuple.
In other words, ‘for loop’ will loop throughout each element in tuple and will return elements one by one.
Return values – To let return a function its value use ‘return’ keyword.
Example 14- Use return keyword to return function’s value.
def my_func(x): return 3 + x print(my_func(4)) print(my_func(6))
Explanation – In above example, a parameter is defined inside parentheses that holds two different values in print() function. As function is called it takes value from print() function and goes to return keyword to perform an action of adding value 3 to parameter. Finally, after adding value 3 to parameter it returns a sum of both number.
pass Statement – A function statement can not be empty, but in case it is necessary to leave empty then use pass keyword to leave a function statement empty.
Example 15- Use pass keyword to leave a function statement empty.
def my_func(): pass
Explanation – In above example, pass keyword is used to pass value of function. If there would be any action to perform then it would never be performed as pass keyword would pass action.
Recursion – It means when a defined function calls itself.
Example 16 – Calling a function to itself.
def my_func(x): if x > 0: r = x + my_func(x-1) print(r) else: r = 0 return r print( "\nRecursion Example Results" ) my_func(8)
Explanation – In the above example, first of all, Python will execute print() function statement and then call def function. After that it will check if ‘if condition’ is True or False, as it is True, therefore, it will enter in ‘‘if condition’ and every time it will deduct number 1 one from current value of x starting from 8 until the condition goes falls. After that Python will go to ‘else condition’ and perform an action of adding number to the x’s current value.
If you find anything incorrect in the above-discussed topic and have any further questions, please comment down below.
Like us on