Python: Functions and Reusability Basics

Introduction

So far, you’ve learned how to write simple programs with variables, operations, and control flow. But as programs grow, repeating code becomes messy. Functions solve this problem by letting you bundle code into reusable blocks. In this tutorial, we’ll explore how to define functions, pass arguments, return values, and use them effectively.

Step 1: Defining a Function

A function is defined using the def keyword:

def greet():

    print("Hello, welcome to Python!")

 

·       def starts the function definition.

·       greet is the function’s name.

·       The code inside runs when you call the function:

 

greet()

 

Output:

Hello, welcome to Python!

 

 

Step 2: Parameters and Arguments

Functions can accept inputs called parameters. When you call the function, you provide arguments.

Code:

def greet(name):

print(f"Hello, {name}!")

 

#Calling the function:

greet("Deepak")

greet("Xorox")

 

Output:

Hello, Deepak!

Hello, Xorox!

 

Note: Parameters make functions flexible and reusable.

 

 Step 3: Returning Values

Functions can also return results using the return keyword.

Code:

def add(a, b):

    return a + b

 

result = add(5, 3)

print(result)  # Output: 8

Here, add() returns the sum, which we store in result.

  

Step 4: Default Parameters

You can set default values for parameters:

Code:

def greet(name="Guest"):

    print(f"Hello, {name}!")

 

greet()          # Output: Hello, Guest!

greet("Deepak")  # Output: Hello, Deepak!

This makes functions more user-friendly.

A screenshot of a computer program

AI-generated content may be incorrect.

 

Step 5: Scope of Variables

Variables inside a function are local — they exist only within that function.

Code:

def show_number():

    num = 10

    print(num)

 

show_number()

print(num)  # Error: num is not defined outside the function

Note: Use return if you want to pass values back to the main program.

 

 Step 6: Why Functions Matter

Functions help you:

·       Avoid repetition (DRY principle: Don’t Repeat Yourself)

·       Organize code into logical sections

·       Make programs easier to read and maintain

·       Reuse code across multiple projects

 

Mini Exercise

Try this challenge:

·       Write a function square(num) that returns the square of a number.

·       Write another function is_even(num) that returns True if the number is even, otherwise False.

·       Call both functions and print the results.

 

Hint:

def square(num):

    return num * num

 

def is_even(num):

    return num % 2 == 0

 

print(square(4))       # Output: 16

print(is_even(4))      # Output: True

print(is_even(7))      # Output: False

 

A screenshot of a computer program

 

Conclusion

In this tutorial, you learned how to:

·       Define and call functions

·       Use parameters and arguments

·       Return values from functions

·       Work with default parameters and variable scope

·       Functions are the building blocks of reusable code. They make your programs cleaner, more efficient, and easier to expand.

 

In the next tutorial, we’ll dive into data structures — lists, tuples, and dictionaries — which allow you to store and organize collections of data.