Chapter 8: Functions

      

   

   Until now, our code was only a few lines long and remained easily readable. However, we will soon develop more elaborate programs of several hundred or even thousands of lines and you will easily understand that writing everything in a suucession will quickly become unmanageable... 😱

   This is where functions come in. 

   A function is a set of instructions created to perform a specific task under a unique name, which we can choose, preferably using a representative name. So, for example, we will create an addition() function whose goal will be... to add 2 numbers! 😱

   A function is defined as follows, with the term def or definition, followed by the name of the function and then its arguments in parentheses, followed by a colon :. Then comes the function code. If it ends with return + value, it returns a result, otherwise not.

def function_name (arguments) :
   #Function's code
   return result

   

   If you think about it, you've actually used functions without even knowing it. 😁 Indeed, the print() or type() functions for example are predefined functions in Python . 

   

   So let's create our addition() function:

def addition (a,b) :
    return a + b

   

   As you can see, it's very simple. The function is called addition(). It takes two arguments : the numbers a and b and returnsthe addition of both.

   The advantage of functions in a program is that instead of retyping the same code over and over again or copying / pasting, you just have to call the function for that. 🙃

   Now, if you run the previous program, you will notice that nothing happens. This is normal, because we have just created a function but there is no code that calls it. 😱

    So we'll add it below. 😎

   Be careful, as with variables, functions must have been defined before calling them, otherwise it won't work! It's like asking you to go get someone without telling you who it is... 😝

   

#Definition of the function
def addition (a,b) :
    return a + b

#Beginning of our main program
#We ask the user for 2 numbers
a = int(input("Enter a number for a: "))
b = int(input("Enter a number for b: "))

#We call our function addition() into 
#print() to show the result
print(addition(a, b))

    

   Yes, like conditions or loops, we can nest functions inside other functions, which makes everything extremely fast and practical! 😄

   That said, you will notice that it is not very clean, because it is difficult to know where our program begins, especially if we add other functions... That is why, in general, when programming, we start with the main() function.

   We can do this in Python, by writing it like this:

   

#Function that tests if a number is less than or not
def lesser(a,b):
   if a < b :
      return True
   else :
      return False

#Function that tests if a number is even or not
def parity (number) :
   if number % 2 == 0 :
      print("even")
   else :
      print("odd")

#Function that adds 2 numbers
def addition (a,b) :
   return a + b

#Main function
def main():
   a = int(input("Enter a number for a :"))
   b = int(input("Enter a number for b :"))

   #We call the lesser() function to test the numbers entered
   if lesser(a, b):
      print("a = " + str(a) + " is less than b = " + str(b))
   else :
      print("a = " + str(a) + " is greater than b = " + str(b))

   #We call the parity() function
   parity (a)
   parity (b)

   #We call the addition() function
   print(addition(a, b))

#Call that indicates to start the program
#by the main() when it is launched (Run)
if __name__ == "__main__":
   main()

   

   There we are, this program is a little longer but commented enough to allow you to understand how it works. 😉

   Now let's move on to some training! 🤓

        

   

Exercise 11

Take the code above and add the test for equality between the two numbers, in the function lesser() .

In the parity() function, add the number, to display something like: "24 is an even number".

In the call to the addition() function : print(addition(a, b)) , add some text so that it displays something like: "24 + 12 = 36".

   

   

Exercise 12

Take the code from exercise 11.

Create a function hello(), which prints "Hello!" and call it at the beginning of the program. Same with the bye() function at the end.

   

   

Exercise 13

Take the code from exercise 12 .

Create the subtraction()multiplication(), and division() functions and display their results as for the addition() function .

      

   That's it for this chapter! 😀

        See you soon! ☺️

   

This site uses cookies to enable you to log in. We do not store or sell any personal data. By continuing to use this website, you agree to their use. Thanks!