Functions in Python
Every programming language has the features to supports functions. The Functions gives our code more productive and efficient. It is a block of order which is used to perform single or many actions. There are in builds and customized functions in pythons and You can increase the reusability of codes using the features of functions.
In fact Python provides huge inbuilt function but you can be made your own functions accordingly which is knows as users- defined functions.
How to define the Functions?
a. Block for function starts with def followed with the name of function and parentheses ().
b. Arguments are dropped inside the parentheses and you can also define the parentheses.
c. The very first line of the function can be an optional statement.
d. The block of the function should start with the symbol ‘:’ colon.
e. The return statement at the end of the block which gets the return to the calling statement. The return statement with no argument will return none.
Example –
def machine( run ):
"this is a function with name machine and word run as argument "
print run
return
How to call functions?
Defining a function simply gives it a name and arguments, specify the parameters that are to be built-in in the function, and structures the blocks of code of the program.
Initially when the fundamental structure of a function is done, you can run it by calling it from another function or directly in python accordingly.
Example:
#This how you have to call Instruments functions
def Instruments():
print("I am Instrument function")
Instruments()
Arguments in python
Names that we pass inside () parentheses are called as arguments. We can add as long as arguments inside the function just we need to separate them with comma.
Example:
def Instruments(fn):
print(fn + " is a instrument")
Instruments("piano")
Instruments("dholak")
Instruments("octopad")
Variable number of arguments in python:
If you are hoping for many numbers of arguments so that can be possible using a variable number of arguments or variable-length arguments.
The * symbol is typed before the name of the variable that takes the values of all the non-keyword variable arguments in python.
Examples:
# info function definition is here
def info( g1, *var1 ):
"This prints a variable passed arguments"
print ("Output is: ")
print (g1)
for var in var1:
print (var)
return;
# calling of info function
info( 103 )
info( 703, 603, 530 )
Anonymous function in python
These types of functions are generally not declared in a preferred manner with the help of the def keyword, on the other hand, you can take the help of the lambda keyword to deals with these kinds of stuff.
- Lambda easily takes any number of arguments but what is imperative is that it returns only one value in the form of an expression respectively. Multiple expressions or commands can’t be contained there.
- Lambda requires an expression that is why an anonymous function can’t be called directly.
- Lambda function has their personal local namespace and therefore it can’t access the parameter from global namespaces respectively.
Examples:
# sumlambda Function definition is here
sumlambda = lambda argument1, argument2: argument1 + argument2;
# Here you can call sumlambda as a function
print ("Value of total : ", sumlambda( 10, 20 ))
print ("Value of total : ", sumlambda( 20, 20 ))
Return statements
The important return keyword exits the function taking the values from the function to the calling place. If there is no return value it seems that it doesn’t have any argument passed.
Examples:
def sum( argument1, argument2 ):
# Here add both the parameters and return the value."
totalarg = argument1 + argument2
print ("Inside the function : ", totalarg)
return totalarg;
# Now you can call sum function
totalarg = sum( 10, 20 );
print ("Outside the function : ", totalarg)
