Functions Basic Question


#1



I am confussed about why there is a (n) inside one_good_turn and a (m) inside deserves_another what are the points of these letters?


def one_good_turn(n):
    return n + 1
    
def deserves_another(m):
    return one_good_turn(m) + 2


#2

They only indicate parameters.

Let's say that you want to calculate the perimeter of the rectangle. We know that perimeter of the rectangle depends on two values - length of the shorter side and length of the longer side, right? So we need to define the function with two parameters.

def rectangle_perimeter(x, y):
    return 2 * (x + y)

I have used x and y as names of the parameters, but you can use any valid names:

def rectangle_perimeter(firstSide, secondSide):
    return 2 * (firstSide + secondSide)

or:

def rectangle_perimeter(a, b):
    return 2 * (a + b)

It does not really matter.


My rectangle has dimensions 3 x 6. So to calculate the perimeter I need to call my function:

def rectangle_perimeter(a, b):
    return 2 * (a + b)

print rectangle_perimeter(3, 6)

Inside the body of the function a will be now equal to 3 and b will be equal to 6 and value 2 * (3 + 6) = 2 * 9 = 18 will be returned.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.