Python Functions
A function is a block of organized, reusable code that is used to perform a single, related action.
- 1. Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
- 2. Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.
- 3. The first statement of a function can be an optional statement - the documentation string of the function or docstring.
- 4. The code block within every function starts with a colon (:) and is indented.
- 5. The statement return [expression] exits a function.
- Example
- The following function takes a string as input parameter and prints it on standard screen.
def printme( str ): "This prints a passed string into this function" print str return
Calling a Function
Defining a function only gives it a name, specifies the parameters that are to be included in the function and structures the blocks of code.Once the basic structure of a function is finalized, you can execute it by calling it from another function or directly from the Python prompt. Following is the example to call printme() function − #!/usr/bin/python # Function definition is here def printme( str ): "This prints a passed string into this function" print str return; # Now you can call printme function printme("I'm first call to user defined function!") printme("Again second call to the same function")
When the above code is executed, it produces the following result −I'm first call to user defined function! Again second call to the same function
Pass by reference vs value
- All parameters in the Python language are passed by reference.
def changeme( mylist ): "This changes a passed list into this function" mylist.append([1,2,3,4]); print "Values inside the function: ", mylist return # Now you can call changeme function mylist = [10,20,30]; changeme( mylist ); print "Values outside the function: ", mylist
Function Arguments
You can call a function by using the following types of formal arguments:- Required arguments
- Keyword arguments
- Default arguments
- Variable-length arguments
Required arguments
- Required arguments are the arguments passed to a function in correct positional order.
Keyword arguments
Keyword arguments are related to the function calls. When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name.Default arguments
A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument.Variable-length arguments
You may need to process a function for more arguments than you specified while defining the function. These arguments are called variable-lengtharguments and are not named in the function definition- An asterisk (*) is placed before the variable name that holds the values of all nonkeyword variable arguments.
The Anonymous Functions
These functions are called anonymous because they are not declared in the standard manner by using the def keyword. You can use the lambda keyword to create small anonymous functions.The return Statement
The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.- You can return a value from a function as follows −
#!/usr/bin/python # Function definition is here def sum( arg1, arg2 ): # Add both the parameters and return them." total = arg1 + arg2 print "Inside the function : ", total return total; # Now you can call sum function total = sum( 10, 20 ); print "Outside the function : ", total
When the above code is executed, it produces the following result −Inside the function : 30 Outside the function : 30
Scope of Variables
- The scope of a variable determines the portion of the program where you can access a particular identifier. There are two basic scopes of variables in Python −Global and Local Variable.
Global vs. Local variables
Variables that are defined inside a function body have a local scope, and those defined outside have a global scope.
No comments:
Post a Comment