Skip to content

← Back to Python Course Foundations

User Defined Functions (Python)

Learning Objectives

  • Define and call functions with parameters.
  • Use default parameter values.
  • Return computed results.

Example

def area_rectangle(length, width=1):
    return length * width

print(area_rectangle(5, 2))
print(area_rectangle(7))

Dry Run

  • area_rectangle(5,2) -> 10
  • area_rectangle(7) uses default width 1 -> 7

Common Mistakes

  • Forgetting return when output is needed.
  • Mismatch in number of arguments.

Practice

  1. Write a function to find max of 3 numbers.
  2. Return both sum and average from one function.