Function

A piece of code that can be executed as part of a program, which may return a value.

define main()
  name = read("Enter your name")
  print("*=*=*=*=*=*=*=*=*")
  print(name)
  print("*=*=*=*=*=*=*=*=*")

nickname = read("Enter your nickname") print("*=*=*=*=*=*=*=*=*") print(nickname) print("*=*=*=*=*=*=*=*=*")
userID = read("Enter your user ID") print("*=*=*=*=*=*=*=*=*") print(userID) print("*=*=*=*=*=*=*=*=*")


Repetative Code

define main()
  name = read("Enter your name")
  print("*=*=*=*=*=*=*=*=*")
  print(name)
  print("*=*=*=*=*=*=*=*=*")

nickname = read("Enter your nickname") print("*=*=*=*=*=*=*=*=*") print(nickname) print("*=*=*=*=*=*=*=*=*")
userID = read("Enter your user ID") print("*=*=*=*=*=*=*=*=*") print(userID) print("*=*=*=*=*=*=*=*=*")



Use Loops

define main()
  for i from 0 to 2
    if i == 0
     input = read("Enter your name")
    if i == 1
      input = read("Enter your nickname")
    if i == 2
      input = read("Enter your user ID")

print("*=*=*=*=*=*=*=*=*") print(input) print("*=*=*=*=*=*=*=*=*")

Use Functions?

define main()
  getName()
  getNickname()
  getUserID()

define getName() name = read("Enter your name") print("*=*=*=*=*=*=*=*=*") print(name) print("*=*=*=*=*=*=*=*=*")
define getNickname() nickname = read("Enter your nickname") print("*=*=*=*=*=*=*=*=*") print(nickname) print("*=*=*=*=*=*=*=*=*")
define getUserID() userID = read("Enter your user ID") print("*=*=*=*=*=*=*=*=*") print(userID) print("*=*=*=*=*=*=*=*=*")

Use Function
with Parameter

define main()
  name = read("Enter your name")
  prettyPrint(name)
  nickname = read("Enter your nickname")
  prettyPrint(nickname)
  userID = read("Enter your user ID")
  prettyPrint(userID)

define prettyPrint(input) print("*=*=*=*=*=*=*=*=*") print(input) print("*=*=*=*=*=*=*=*=*")


Use Function
with Parameter

define main()
  getInputAndPrint("Enter your name")
  getInputAndPrint("Enter your nickname")
  getInputAndPrint("Enter your user ID")

define getInputAndPrint(prompt) input = read(prompt) print("*=*=*=*=*=*=*=*=*") print(input) print("*=*=*=*=*=*=*=*=*")

Use Function
with Parameter & Return

define main()
  name = getInputAndPrint("Enter your name")
  nickname = getInputAndPrint("Enter your nickname")
  userID = getInputAndPrint("Enter your user ID")

define getInputAndPrint(prompt) input = read(prompt) print("*=*=*=*=*=*=*=*=*") print(input) print("*=*=*=*=*=*=*=*=*") return input