Python Function- bundle the task!
To start this blog, first, you have to understand why are we using function?
The function in python carry out the tasks and make a subprogram of function.
It decreases the complexity of the program. It makes large code into small code.
Ok, you had understood why we using function? And what is the use of function?
Second, thing how to use it?
You can use a function with the following method:-
• Creating a function
• Calling a function
• Parameters vs
• Number of arguments
• Arbitrary Arguments,
• keyword arguments
• Arbitrary keyword
• Default parameter
• passing list as an
• Return value
• The pass statement
1. Creating a function
First, the question is how to create a function?
You can create a function with the help of the def keyword.
def keyword defines the function.
Here, you can see that how we had created a function to do repeated tasks.
2. Calling a function
You can call the function with the function name with parenthesis().
The parenthesis passes an argument to the function.
Here, you can see that how we had called the function using parenthesis and arguments.
Here, you had observed we had passed something from parenthesis. You are next going to see it.
The arguments in python are variable of information which we passing through function.
This variable holds the information.
4. Number of arguments
Suppose, you have to pass as many arguments from function. How will you pass it?
You can pass as many arguments from function using :-
1. Arbitrary arguments,
2. Keyword arguments
1) Arbitrary arguments, * args
You can use arbitrary arguments in python to pass as many arguments as you want.
You can do it with an asterisk(*) sign.
The benefit of this is that it doesn’t see the order of the arguments.
2) keyword arguments
You can use keyword arguments to pass key-value as one argument.
With the help of this, you can place an order of argument.
You had seen keyword arguments that we can assign value in any order.
But suppose you need the default value in the python function.
5.Default parameters value
You can put the default value to some arguments and can change the default value.
In the case of the default value, you have not necessary to pass a value for that argument.
Only when you have to change the default value then you can pass the value to argument.
Here, you can see that we had only passed the name “steve” through the function name.
The parameter age has the default value.
6.Passing list as an arguments
Suppose, you have to pass the list of all names. How would you do it?
You can do it with the help of the passing list as an argument.
Here, you can see that we had passed a list of names as an argument.
You can pass as many arguments making list.
7.Return value in function
The return value in the function gives the value which will return when the function is called.
It is different from the print method. the print method only displays the output.
After, return no statement executed.
The return statement ends the execution of the statement.
Here, you can see that how the function took two numbers and add them with the help of a return statement.
8.The pass statement
Suppose you have not executed the function this time. Maybe you have to execute in future. What will you do?
Here, you can see that nothing has displayed on the screen. Because we had used the pass statement which prevents the execution to stop.
9.Recursion in function
The recursive function call itself. In the python, the function calls itself again and again until the result doesn’t gain.
Recursive Function is very useful in mathematical problems to solve factorial.
You can go in-depth of solutions and problems with the help of recursion function.
Here, you can see function returning function till the condition satisfy.
# num = 4 matches
# num = 3 match
# num = 2 match
# num = 1 match if condition applied
4*3*2*1 = 24
Function recursion is a great way to do the things which can return.
Python function do the repeated task. The difference between function and loop is that loop doesn’t need calling. Function needs to be called.
That means the function can be control but the loop is hard to control.
You can pass as much information through the python function. Python function takes an argument as a keyword or positional argument.
Python recursion function quality return the function repeatedly until the condition satisfied.
The recursive technique is very helpful in factorial in maths to return a number until the condition satisfy.
I hope you love this blog. If there is any suggestion you want to give I’m happy to take it!
Thank you for reading this. To read more you can visit:-
"Quality brings clarity"
- A new way to format the string in Python 3.6: fstring - October 11, 2021
- Ultimate new guide for python beginners in 2021 - October 3, 2021
- How to download famous thonny easily for python? - September 20, 2021