Skip to content

Sep 13: Defining Functions, return

Learning Objectives

After today's class, you should be able to:

  • Explain the flow of execution from one function to another.
  • Describe the syntax of function definitions and function calls.
  • Write short functions that have parameters and return values.

Reminders

  • Read Ch 4 – ideally:
    • 1st half before Friday
    • 2nd half before Monday
  • Submit HW 4 – ideally:
    • 1 and 2 before Friday
    • 3 and 4 before Monday

POGIL Activity

  • Defining Functions
    • If you are absent today, complete this activity at home
    • Bring your completed activity to class or office hours
  • Python Tutor
    • Visualize the flow of execution and memory contents

Example Code

Model 1

def model_one():
    word = input("Enter a word: ")
    L = len(word)
    ans = word * L
    print(ans)

def main():
    print("Starting main...")
    model_one()
    print("All done!")

main()

Model 2

def model_two(word):
    ans = word * len(word)
    print(ans)

def main():
    print("Starting main...")
    w = input("Enter a word: ")
    model_two(w)
    print("All done!")

main()

Model 3

def model_three(word):
    ans = word * len(word)
    return ans

def main():
    print("Starting main...")
    w = input("Enter a word: ")
    result = model_three(w)
    print(result)
    print("All done!")

main()