This is a text field

Python Function- bundle the task!

Image credit: GangBoard

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
• Arguments
• Parameters vs
arguments
• Number of arguments
• Arbitrary Arguments,
*args
• keyword arguments
• Arbitrary keyword
arguments, **kwarg
• Default parameter
value
• passing list as an
argument
• Return value
• The pass statement
• Recursion

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.

For example,

Output:-

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.

For example,

Output:-

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.


3. Arguments

The arguments in python are variable of information which we passing through function.

This variable holds the information.

For example,

Output:-


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,     
* args
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.

For example,

Output:-

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.

For example,

Output:-

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.

For example,

Output:-

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.

For example,

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.

For example,

Output:-

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?

For example,

Output:-

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.

For example,

Output:-

Here, you can see function returning function till the condition satisfy.

# num = 4 matches
4*factorial(3)

# num = 3 match
4*3*factorial(2)

# num = 2 match
4*3*2*factorial(1)

# num = 1 match if condition applied
4*3*2*1 = 24

Function recursion is a great way to do the things which can return.


Conclusion:-

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:-

python🐍-loop
python🐍 -if-else
python🐍-dict-5 method
python🐍-dict
python🐍-sets

"Quality brings clarity"

Avatar for Vipul kunwar
Follow me

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Back to top