Lets Start Python Programming For Beginners to advanced. Functions β Reusable Code Blocks, Lectures 8
π Lecture 8: Functions β Reusable Code Blocks
π― Goal
By the end of this lecture, you will:
- Understand what a function is and why it’s useful.
- Be able to define and call your own functions.
- Use parameters and return values to make functions more powerful.
- Practice writing simple functions like
greet()
oradd()
.
π§ What Is a Function?
A function is like a mini-program inside your code.
It helps you:
- Organize your code
- Reuse code without typing it again and again
- Make your programs easier to read and fix
Think of a function like a magic box that does one specific job.
π¦ Example:
def greet():
print("Hello!")
This defines a function called greet()
that says “Hello!” when used.
π§ How to Define a Function
To create a function, we use the keyword def
, followed by the name and parentheses ()
.
Syntax:
def function_name():
# Code goes here
Example:
def say_hi():
print("Hi there!")
say_hi()
π Output:
Hi there!
π§ Kids Corner: A function is like a recipe. Once you write it, you can use it anytime β just like making cookies over and over!
π£οΈ Giving Your Function Information β Parameters
You can give your function extra info using parameters (like telling it what to say or do).
Example:
def greet(name):
print(f"Hello, {name}!")
greet("Alice")
greet("Bob")
π Output:
Hello, Alice!
Hello, Bob!
Here, "Alice"
and "Bob"
are arguments β the actual values passed into the function.

π¦ Getting an Answer Back β Return Values
Sometimes, you want your function to give back a result.
Use return
to send a value back.
Example:
def add(a, b):
return a + b
result = add(3, 5)
print("The sum is:", result)
π Output:
The sum is: 8
Now you can store or use the result somewhere else in your program.
π Practice Time!
Write a function that:
- Takes a personβs name
- Says hello and tells them the square of 5
π‘ Sample Code:
def welcome(name):
print(f"Welcome, {name}!")
print("The square of 5 is", 5 * 5)
welcome("Emma")
π Output:
Welcome, Emma!
The square of 5 is 25
π Challenge (Optional)
Make a function that calculates the area of a rectangle.
def area(width, height):
return width * height
print("Area of rectangle:", area(4, 6))
π Output:
Area of rectangle: 24
Try adding another function for perimeter!
π§ Kids Corner
π§ Imagine you have a robot friend who knows how to tie shoes.
Every time someone needs help tying shoes, you say:
βRobot, tie shoes!β
Thatβs exactly what a function does β it lets you reuse a task easily!
π€ Robot says:
Shoes tied! Ready to go!
π Summary
- Use
def
to define a function. - Call the function with its name and parentheses.
- Pass information using parameters.
- Use
return
to get a result from a function. - Functions help organize code and avoid repetition.
Stay Updated
If you found this information useful, donβt forget to bookmark this page and Share and leave your feedback in the comment section below.
Python Programming For Beginners To Advanced
Python Programming For Beginners! Lecture 1
Python Programming! Variables and Data Types | Lectures 2
Python Programming! Input And Output | Lectures 3
Python Programming! Operators in Python Lectures 4
Python Programming! Conditional Statements Lectures 5
Python! Loops For and While | Lectures 6
Python! Building A Mini Game | Lectures 7
Python! Functions | Lectures 8
Python! Storing Multiple Values | Lectures 9Β