Python - Functions
Functions
Function in python it's same as the functions in any other programming language. Concept is same the syntax is differ.
As we all know function is a block of code that perform some task.
As we all know function is a block of code that perform some task.
There are 3 types of function in python.
- In-built function - for e.g:- min(), print(), add().
- User defined function - That will be defined or created by the user.
- Anonymous function - is also known as lambda function because they are not declared with the def keyword.
How to define a function in python
- use def statement.
- followed by function name.
- Add parameter to the function.
- All the parameter should enclosed with the parenthesis or bracket and at the end of the function used 'colon' :
When to use a function.
- Function should use when there is duplication of a task.
- It increase readability of the program.
Return Statement
A return statement is used to return something from the function. It's only work for the function. If any other want to work with it. It's said I can't work with you.
The statement after the return statement is not executed.
Without the return statement your function will return an object None.
Types of arguments in python
- Position
.png)
- Keyword : - When you pass the variable name itself.
- Default
- Variable length Arguments
The *arg variable will return output in a tuple format.
Keyword variable length arguments.
If you want to pass multiple data that to be help of the keyword than we have to use keyword variable length argument.
Example
Creating a function which will take two parameter and do subtraction.
Now, when calling the function min(), it's giving us output as 1. But what happened if you try to store the value of min in a variable.
When we print value of a, get the output as None. Because we are not using return statement inside the function.
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment