Introduction to Python Programming/Python Programming - Functions
5. Functions
[edit | edit source]A function within Python is a set of code that performs a single utility and may or may not return a result. Functions provide methods of breaking up complex code into reusable blocks of code which can be used as building blocks of different programs. Python already provides for a lot of inbuilt functions such as print, input, etc. You can write your own Python functions called user defined functions.
5.1. Defining a function
[edit | edit source]Like stated earlier, one can write a set of code to perform certain utility. The following rules provide a guideline for writing your own function. Functions are executed when they are called by their user defined function name.
Function blocks start with the keyword “def” followed by the user provided function name and parentheses (). The inputs that user provides to the function are placed within the parentheses, further extrapolating the parentheses can be used to define new parameters to be used within the user defined function. Python provides for documentation that be used to describe the function, author, date of creation, and the purpose of the user defined function, etc. The documentation of the user defined function can be accessed by using the __doc__ keyword after the user defined function name. The keyword return is used to exit the function, optionally passing an expression to the caller of the function.
5.2. Syntax
[edit | edit source]def functionname(parameters): ‘’’function docstring’’’
Code block
Return(expression)
Example
>>> def squareofnum(n): This function returns the square of the number passed as the parameter return n*n >>> print squareofnum(3) 9 >>> print squareofnum.__doc__ This function returns the square of the number passed as the parameter >>> >>> def myappendlist(mylist,a): this function appends a list to the already existing list mylist.append(a) return >>> a=[1,2,3,4] >>> b=[6] >>> >>> a [1, 2, 3, 4] >>> b [6] >>> print myappendlist(a,b) None >>> a [1, 2, 3, 4, [6]] >>>
5.3. Function Arguments
[edit | edit source]A function can be called by using passing the arguments in the right order, mentioning the keyword arguments, default arguments, variable length arguments.
#function is defined here >>> def newfunction(n): print "inside function" answer= n*n*n return answer >>> print newfunction(2) inside function 8 >>> print newfunction() Traceback (most recent call last): File "<pyshell#203>", line 1, in <module> print newfunction() TypeError: newfunction() takes exactly 1 argument (0 given) >>> >>> def vistingcard(name, age="22"): print "the name is %s" %name print "the age is %d" %age return >>> vistingcard("new", 33) the name is new the age is 33 >>> vistingcard(age=34, name="no name") the name is no name the age is 34 >>> vistingcard(36, "newname") the name is 36
Traceback (most recent call last): File "<pyshell#221>", line 1, in <module> vistingcard(36, "newname") File "<pyshell#218>", line 3, in vistingcard print "the age is %d" %age TypeError: %d format: a number is required, not str >>>
Variable length arguments can be passed to the function using the *. Example is provided below.
>>> def variableargfunc(*argu): print "inside function" for a in argu: print a return >>> variableargfunc(10) inside function 10 >>> >>> variableargfunc(10,20) inside function 10 20 >>> >>> variableargfunc([1,2,3,4,5]) inside function [1, 2, 3, 4, 5]
5.4. Scope of variables
[edit | edit source]There are two types of variables that are defined within the scope of the function that they are used. Variables within a function are local to the function and are called local variables with respect to the function and the global variables are defined outside the scope of the variables.
>>> globalvariable=0 >>> def scopefunc(n,m): globalvariable=n+m print "the local variable is %d " %globalvariable return globalvariable >>> >>> print "outside the function scope %d " %globalvariable outside the function scope 0 >>> scopefunc(5,6) the local variable is 11 11 >>> print "outside the function scope %d " %globalvariable outside the function scope 0 >>>