Functions are used for grouping statements that do specific task. Also they are used to make code reusable. They are not like mathematical function. Mathematicians uses declarative definitions (what is) while computer scientists are using imperative definitions (how to). Making functions in Python is done using following syntax:
Parameters are delimited using comma. There are functions without parameters and return statement. Later we will see an example. After theory it is time to write function as shown in following example:
EXAMPLE: Write function which takes as input 2 numbers and returns greatest number.
You can use name of function which you want but in this example I am going to use maxNumber.
def maxNumber (firstNumber, secondNumber): if firstNumber > secondNumber: return firstNumber return secondNumber
Now we have our function. To make it do something we must call it. Calling function is done using the following statement:
Numbers 2 and 3 are called arguments. Variables firstNumber and secondNumber are called parameters. When you are calling function parameters are substituted by values of arguments as shown on the picture:
As you can see from example we first write function and then call it. If you first call function and then write it you will get an error. Inside function code at every place firstNumber is substituted with 2 and secondNumber is substituted with value 3. The code looks like this:
When Python comes to return keyword then the flow of application returns to the caller.
It is useful to store execution of function in variable so we can use the result of function later in the code.
result=maxNumber(2,3) print “Largest number is ”, result
You could also write previous statement as:
print “Largest number is ” , maxNumber(2,3)
Maybe you have noticed that I used two return statements. As I said if condition inside if is true then first return statement is executed. If condition is false then first return is skipped and only second is executed. Remember: return statement leaves function immediately and if there is code after return statement it is not executed as shown in second example:
EXAMPLE: Rewrite max function to consider case when numbers are equal.
def maxNumber (firstNumber, secondNumber): if firstNumber > secondNumber: return firstNumber if firstNumber==secondNumber: return firstNumber print "This is never executed" return secondNumber
result=maxNumber(2,2) print "Largest number is ", result
Variable names inside function are called local variables. There are also global variables, I am going to explain them later. This is called scope of a variable. Check this example:
number=1 print "Outside function value of number is ", number def printNumber(): number=5 print "Inside function value of number is ",number printNumber() print "Value of variable number after calling function", number
At the beginning of tutorial I mentioned that functions can be used without parameters and without return statement. This is an example of such function. Function also demonstrates the scope of the variable. As you can see after function finish all variables declared inside are deleted. Variables declared outside function remains same. Maybe you haven't expected following output of function:
Outside function value of number is 1
Inside function value of number is 5
Value of variable number after calling function 1
Suppose you want for some reason to have variable which can be modified inside function and have the same value when leaves function. It is possible to accomplish this task using keyword global as in example:
number=1 print "Outside function value of number is ", number def printNumber(): global number number=number+1 print "Inside function value of number is ",number printNumber() print "Value of variable number after calling function", number
From example you can see that value of variable number is 1 before it enters function. Inside function variable is increased by 1 and after exiting function the value of number remains same. This is how output looks like after executing:
Outside function value of number is 1
Inside function value of number is 2
Value of variable number after calling function 2
Graphically local variables and global variables are shown in the picture.
There are also non-local variables. To read more about them check this url:
Python has ability to make documentation at same time while you are writing function. It is recommended to document every function you write. Following example shows how to do it:
def printText(howManyTimes,yourText): ''' Prints text given number of times. First parameter is number, second parameter is text''' print howManyTimes*yourText printText(2,"codecall.net ") print (printText.__doc__)
This feature is called documentation strings or DocStrings. Convention used in example is that first letter is capital and sentense ends with dot. Then one line is blank and then detailed explanation is at line three. Printing documentation is done using syntax:
as shown in example. Make sure you have two symbols __ not one.
Write composite function which will find maximum of three numbers. (HINT: Composite functions uses output of one function as input to other function)
Write function which takes three numbers as input and returns sum of squares of two largest numbers.
Source code is here: codecall.zip 1.7KB 218 downloads