A Function is a set of organized codes that perform a certain task.
Python functions give a particularity to your programming and a high-level of code reusing (you can use a code more than once in a program).
Defining Your Own Functions
You can define your own function to give the required functionality and re-use them throughout the program.
Syntax
def functionname (input parameters or arguments ):
“docstring”Block of code or indented statementsreturn [expression]
The two keywords are def (lets the program know that the indented code from the next line onward is part of the function) and return (return an answer from the function)
Example
def printstring( strg ):
"This is user defined python function to print a string"return
Calling a function
Defining a function just gives it a name, indicates the parameters that are to be included in the function and designs the blocks of code. Immediately the basic structure is finished, you can run it
Function arguments
Here are some of the arguments which can be used to call a function
Required argument: argument placed in correct fixed order. The number of arguments in the function definition should match exactly in the function call. If the function is not matched corrected then there will be an error as the results
def printstring( strg ):
"This is user defined python function to print a string"print (strg)return;we can call this function with the name as followprintstring()printstring (“calling second time”)printstring (“calling third time”)
output
Keyword argument:
In python, when we use keyword argument in a function call then the caller identifies the arguments by its name. Its provides the flexibility to place the arguments out of order as Python interpreter uses the keywords to match the values with parameters
Default argument: assumes a default value if a value is not given in the function call for that argument
Variable length argument: used to a process a function for more arguments than in the specified functions and they are not usually named name in the function definition. Below is the syntax for a variable length argument.
def functionname([formal_args,] *var_args_tuple ):
“docstring”Block of code or statementsreturn [expression]
Anonymous functions.
These are functions which do not use the standard keyword def. instead they are based on the lambda keyword to create small functions.
Lambda can take a numerous number of arguments but return just one as an expression. They are not direct call to print because anonymous function requires an expression.
Syntax
lambda [arg1 [,arg2,…..argn]]:expression
Example
# Anonymous function definition is heresum = lambda arg1, arg2: arg2 - arg2;call subtract as a function print "Value of total : ", subtract( 70, 70 )print "Value of total : ", subtract( 50, 70 )
Output
The return expression return exist a function, optionally given back an expression to the caller. A return expression with no arguments is the same as return none. Below is an example of how you can return a value from a function
def mutiply( arg1, arg2 ):
total = arg1*arg2print "Inside the function : ", totalreturn total;total = multiply( 30, 20 );print "Outside the function : ", total
output
It identifies the portion of the program where you can enter a particular identifier. They are two main types of scope of variables in python which are global and local variables.
Local variables can be accessed only in the inside body of the functions in which they are found and global variables can be accessed throughout the whole function.
total = 0; # This is global variable.
# Function definition is heredef subtract( arg1, arg2 ):# subtract both the parameters and return them."total = arg2 - arg1; # Here total is local variable.print "Inside the function local total : ", totalreturn total;# Now you can call subtract functionsubtract( 40, 70 );print "Outside the function global total : ", total
Output
Comments
Post a Comment
Please do not enter any spam link in the comment box.