Next

Introduction to Functions

  • This lesson introduces Python functions and explains how to create reusable blocks of code.

  • What is a Function?

    A function is a block of reusable code that performs a specific task.

    Instead of writing the same code again and again, we define it once and call it whenever needed.

    Why Do We Use Functions?

    Functions help to:

    • Reduce code repetition

    • Improve code readability

    • Make programs modular

    • Simplify debugging

    • Save time and effort

    • Organize large programs

    Real-Life Example:
    ATM withdrawal, mobile calculator operations, login verification – all use functions internally.

    Syntax of a Function

    def function_name(parameters):

        # function body

        return value

    Syntax Explanation

    Part

    Description

    def

    Keyword to define a function

    function_name

    Name of the function

    parameters

    Values passed to the function

    :

    Starts function block

    indentation

    Defines function body

    return

    Sends result back (optional)


    What is Function Definition?

    Defining a function means creating the function and writing its logic.
    The function does not execute until it is called.

Defining a Function

This function prints a welcome message. It is only defined, not executed yet.

def welcome():
    print("Welcome to Python Programming")

Calling a Function

This program calls the welcome() function to display the message.

def welcome():
    print("Welcome to Python Programming")
welcome()
  • What are Parameters?

    Parameters are variables used in function definition to receive values.

    def function_name(parameter):

    What are Arguments?

    Arguments are actual values passed to the function when calling it.

    function_name(value)


    Difference Between Parameters and Arguments

    Parameters

    Arguments

    Used in function definition

    Used in function call

    Act as placeholders

    Actual values

    Receive data

    Send data

Function with Parameter

This function receives a name as a parameter and prints a greeting message.

def greet(name):
    print("Hello", name)

greet("Amit")

Function with Multiple Parameters

This function takes two numbers as parameters and prints their sum.

def add(a, b):
    print("Sum is:", a + b)

add(10, 20)

Function with Return Statement

This function calculates the sum and returns the result.

def add(a, b):
    return a + b

result = add(5, 7)
print("Result:", result)

Understanding Parameters and Arguments

This example shows parameters in function definition and arguments in function call.

def multiply(x, y):   # x, y → parameters
    return x * y
print(multiply(4, 5))  # 4, 5 → arguments
Next