Skip to content

Python! Functions | Lectures 8

Lecture 8 Functions

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() or add().

🧠 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.

Lecture 8 Functions
Lecture 8 Functions

πŸ“¦ 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Β 

Najeeb Alam

Najeeb Alam

Technical writer specializes in developer, Blogging and Online Journalism. I have been working in this field for the last 20 years.

Leave a Reply

Your email address will not be published. Required fields are marked *