Functions
Functions
Function
What is Function?
o Python function is a block of statements that return the specific task.
o The idea is to put some commonly done tasks together and make a
function so that instead of writing the same code again and again for
different inputs, we can do the function calls to reuse code contained
in it over and over again.
Benefits of using functions:
Increase code Readability
Increase code Reusability
Function
Types of function:
Built – in – function:
o These are standard functions in python that are available to use.
o For Example: append(), clear(), add() etc..
User – defined – function:
o We can create our own functions based on our requirements.
Function
Creating a Function:
o We can create a user defined function in python by using the def
keyword.
o After creating a function we can call it by using the name of the
function followed by parenthesis containing parameters of that
particular function.
Example: Output:
Function
Return statement:
o If we want our function to return some value to a function call we can
use return statement.
o The return statement also denotes that the function has ended. Any
code after return is not executed.
Example: Output:
Function
Standard functions:
o Standard library functions are the built-in functions that can be used
directly in our program.
o These library functions are defined inside a module and to use them,
we must include the module inside our program.
Function
Example: Output:
Thank You