Learning outcomes
By the end of this lecture, you should be able to:- define a function using
def - call functions with arguments
- use
returncorrectly - explain code reuse and modularity
Why functions matter
Functions help you:- reuse logic
- reduce repetition
- organize code
- test smaller pieces independently
Basic syntax
Function with arguments
Function with return value
print vs return
Scope idea (basic)
- variables inside function are local by default
- they are separate from variables outside
Exam hints and traps
- function definition alone does not execute body
returnends function immediately- if no explicit return, function returns
None
Quick practice
- Write function to square a number.
- Write function to greet a user by name.
- Predict:
Answer key
- prints
x, then printsNone
