Python Functions
What is a Function?
Function is one of basic concepts in programming language. Functions are "self contained" modules of code that performs a distinct service. It is a mathematical concept that groups a collection of operations into a useful identity, which simplifies other math expressions. A function is used to segregate different functionalities of a program and often combined to perform more complicated tasks. Most programming languages come with a pre-written set of functions that are kept in a library. These functions used to remove repetitiveness thereby resulting in lesser lines of code, ease of use and better maintenance. All programming functions have input and output. The function contains instructions used to create the output from its input. You can also write your own functions to perform specialized tasks .Python Functions
A function is defined by using the block keyword "def" , followed by the function's name, followed by a set of parentheses which hold any parameters the function will take and ending with a colon. Next follows the block of statements that are part of this function. Functions may return a value to the caller, using the keyword- 'return' .Here function is now fully defined, but if we run the program at this point, nothing will happen since we didn't call the function. So, outside of the defined function block, let's call the function with sayHello()
examplePython Function Parameters
A parameter is the variable which is part of the method’s signature (method declaration). Parameters are specified within the pair of parentheses in the function definition, separated by commas. When we call the function, we supply the values in the same way.
exampleDefault Argument Values
We can also provide default values for function parameters in Python in case the user does not want to provide values for them. This is done with the help of default argument values. The default value is assigned by using assignment (=) operator.
exampleThe main advantage of default argument is that we can give values to only those parameters to which we want to, provided that the other parameters have default argument values.
exampleVariable number of arguments
Sometimes the programs might want to define a function that can take any number of parameters, i.e. variable number of arguments, this can be achieved by using the stars (*). This is very useful when we do not know the exact number of arguments that will be passed to a function.
exampleReturning a Value
Not only can you pass a parameter value into a function, a function can also produce a value. The return statement is used to return from a function. The return statement is followed by an expression which is evaluated.
exampleInner Functions
The main advantage of inner functions is that it protect them from anything happening outside of the function, meaning that they are hidden from the global scope.
exampleIf you try calling the findSum(10.20) function:
outputAssign functions to variables
When you assign a function to a variable you don't use the () but simply the name of the function.
exampleFunctions as parameters
There are situations we have to pass Functions as parameters to other functions.
example