This module is now over, see Blackboard for 2018 autumn repeat info.

Modular Programming - Karl Grabe . goo.gl/MjCgx1

(Jan 2018 cit.karlgrabe.com/psp2)

Group Cxy TimeTable

Lecture Support

Some Dates


Lecture WhiteBoard

# defining a function called greetUser

# calling greetUser from the main code

# Define our own function called greetUser

def greetUser ():

  print("=" * 23)

  print("Welcome to our 'greetUser' function")

  print("=" * 23)

print ("hi from the main program")

greetUser() # call our function defined above

Output:

hi from the main program

=======================

Welcome to our 'greetUser' function

=======================


2nd Version passing in a parameter:

First without a function:

#print happy birthday

print("=" * 23)

print ("Happy birthday to you")

print("Happy birthday to you")

print("Happy birtday dear karl")

print("Happy birthday to you")

print("=" * 23)

print("=" * 23)

print ("Happy birthday to you")

print("Happy birthday to you")

print("Happy birtday dear fred")

print("Happy birthday to you")

print("=" * 23)

Etc.. but adding more people is tedious

With a functions:

# function declaration

# function to print happy birthday

#

# parameters:

# input - String: userName

def wishPersonHappyBirthday (userName):

  print("=" * 23)

  print ("Happy birthday to you")

  print("Happy birthday to you")

  print("Happy birtday dear " + userName)

  print("Happy birthday to you")

  print("=" * 23)

wishPersonHappyBirthday("karl")   # call the function

wishPersonHappyBirthday("fred")   # call the function

#.. Adding more people is easy

Output for both programs

=======================

Happy birthday to you

Happy birthday to you

Happy birtday dear karl

Happy birthday to you

=======================

=======================

Happy birthday to you

Happy birthday to you

Happy birtday dear fred

Happy birthday to you

=======================

Using constants

NUMBER_OF_EQUALS =23

#print happy birthday

print("=" * NUMBER_OF_EQUALS)

print ("Happy birthday to you")

print("Happy birthday to you")

print("Happy birtday dear karl")

print("Happy birthday to you")

print("=" * NUMBER_OF_EQUALS)