Login

Back

Happy Birthday - an introduction to functions

Q1. Enter this code and get it working:

1   #Happy birthday
2
3   def happyBirthdayJack(): 
4        print("Happy Birthday to you!") 
5        print("Happy Birthday to you!") 
6        print("Happy Birthday, dear Jack.") 
7        print("Happy Birthday to you!")
8
9   def happyBirthdayJohn(): 
10      print("Happy Birthday to you!") 
11      print("Happy Birthday to you!") 
12      print("Happy Birthday, dear John.") 
13      print("Happy Birthday to you!")
14
15  happyBirthdayJack() 
16  happyBirthdayJohn()
17  input("Press <ENTER> to quit >>>")

Q2. Lines 3 - 7 defines a function called happy BirthdayJack. We use the keyword def to define functions, as on lines 3 and 9. All functions need a pair of brackets after them. Sometimes, we will put values in the brackets, but that is for later! A function doesn't do anything until we 'call' it. That's what is happening in lines 15 and 16. When the program starts:

     - our computer takes note of the definitions, but doesn't actually run them
     - when we reach line 15, this says 'call happyBirthdayJack'
     - so we jump back to line 3 and run the code in that function from line 3 to line 7
     - then jump back to the line after the one that called the function.

We then run 16, jump to the function starting at line 9 and finishing on line 13, then jump back to line 17 and then finish.

Add another function for Mary's birthday and test it.

Q3. Put the function calls on lines 15 and 16 on lines 2 and 3, just before the function declarations. What happens? Why?
Q4.
Once you have declared a function, you can call it in other functions.

1   #Happy birthday
2
3   def happyBirthdayJack(): 
4        print("Happy Birthday to you!") 
5        print("Happy Birthday to you!") 
6        print("Happy Birthday, dear Jack.") 
7        print("Happy Birthday to you!")
8
9   def happyBirthdayJohn(): 
10      print("Happy Birthday to you!") 
11      print("Happy Birthday to you!") 
12      print("Happy Birthday, dear John.") 
13      print("Happy Birthday to you!")
14
15  def main():
16     happyBirthdayJack() 
17     happyBirthdayJohn()
18
19  main()
20
21  input("Press <ENTER> to quit >>>")

We have now made a new function called main(). This function calls the other two functions we wrote earlier. You can see that we call main() on line 19. The program then jumps to line 15, then 16, then it jumps to line 3. When it reaches line 7, it jumps back to line 17, then jumps off again to line 9. When it reaches line 13, it jumps back to line 20, then 21 and waits for us to press ENTER. 

Write a program that prints the horoscope for three different zodiac signs. Each sign should be written in its own function. The three functions should then be called from a function called 'stars'. Test it works.

Q5. Change the names of the functions so they begin with capital letters e.g. def HappyBirthdayJack() but leave the calls as they are e.g. happyBirthdayJack(). What happens when you try to run the program. Explain your answer.
Q6. Why are programs nearly always written using functions?

Back