Python book of magic/08.Functions

Contents | Previous | Next

Functions edit

You have learnt about loops which make work more easy for you. But wait, there are other staff's in python that can make coding more easy for you. One of those are functions. Functions have names used to identify them much like variable.

Functions are the same as box with a name(label). The name of the box can be anything. But this box is not just a box, it could have bombs, food or anything useful or useless. Things inside the box can be used for different purpose such as bomb, can be used in war or in mining.

Functions have a name followed by parenthesis() used to identify them. The importance and role of the functions depends on the information and variables inside them and how they work together. Importance of functions is to prevent us from repeating doing the same thing. So functions allow us to reuse, extend, recycle the code we have written. You don't need to type the same thing whole day. So let take short example.

Example 01

def area(x,y):
 result= x*y
 return result




I know you're asking yourself what def mean, if it is a function or not. The word is a keyword(they tell python what your code represent and starts with small letter). The word mean define which in full I can say 'define a function', every time you create a function you need to type def at the beginning followed by function name. The function name is followed by parenthesis with optional parameters. The colons are used for indentation(you are forced to put a colon)

In the example we created a function which calculate the area of rectangle. I can tell you that its dam easy to create a function. Where are things inside the function? if you look at second and third lines you will realise that they have moved to the left by few spaces(this is called indentation). All code indented after the function belong to that function. result= x*y, return result belongs to area() function but a=29 does not. This is because it is not indented/ found in the right-side of the function.

The values in parentheses are parameters that the function accepts. Any value put in position of x in the function will be assigned on x while the same occurs on y.

The statement result= x*y takes the product of x and y and assign it to variable called result. The second line contain keyword return which shows values stored like how print() works. a=29 is just assigning 29 to variable a (note that the statement is not part of the function).

The last line calls the area() functions. Calling means letting interpreter read your instructions and following them.

Two values are passed to x and y. x is assigned 5 and y assigned 4. Then x and y are multiplied and their products are stored inside result. result is passed to return which would show the value of result to main program(remember return work like printt()). Try putting different values for x and y and see the output. This will save you a lot of time.

Contents | Previous | Next