๐ฏ Learning Objectives
By the end of this lesson, learners will be able to:
-
Define a function using
def
-
Pass data using parameters and arguments
-
Use
return
to send back values -
Call a function multiple times with different inputs
๐ Lesson Content
๐น What is a Function in Python?
A function is a block of reusable code that performs a specific task.
Functions make your code:
-
Organized
-
Reusable
-
Easier to test and debug
๐ธ Defining a Function
Syntax:
Example:
โ
greet()
is a function call
๐ธ Functions with Parameters
You can pass values to a function using parameters:
Parameters are variables inside the function
Arguments are values you pass when calling the function
๐ธ Functions with Return Values
Use return
to send data back to where the function was called:
๐ธ Multiple Parameters and Dynamic Behavior
โ ๏ธ Common Mistakes
Mistake | Why Itโs Wrong |
---|---|
Calling a function without parentheses (greet vs greet() ) |
Wonโt execute the function |
Missing return keyword |
Function wonโt give back any result |
Incorrect number of arguments | Will cause a TypeError |
๐ง Mini Practice
๐ Assignment
Create the following functions:
-
square(num)
โ returns the square of a number -
is_even(num)
โ returnsTrue
if the number is even, elseFalse
-
full_name(first, last)
โ returns the full name as a single string
Call each function at least twice with different inputs.