Master Python Programming From Scratch

Clear, interactive, and structured coding lessons designed for absolute beginners.

Defining & Calling Functions in Python

Python function is a reusable block of code that performs a specific task. Instead of writing the same code again and again, we can create a function once and call it whenever required.

CIIT Tip: Think of a function as a small reusable machine β€” provide input, process it, and return the required output.

1. What is a Function?

A function is a block of statements that is created to perform a particular task. In Python, the def keyword is used to define a function.

def greet():
    print("Welcome to CIIT Institute πŸ“©πŸ”“..!")

Above, we have greet() named function. Defining a function alone does not execute the code. The function must be called to execute the code.

2. Function Syntax

def function_name():
    # function body
    statement
Part Meaning
def Keyword used to define a function
function_name Function name
() Parentheses for parameters
: Starts the function block
Function Body Code executed inside the function

3. Defining a Function

When defining a function, we specify the function name and the code that will be executed inside it.

def welcome():
    print("Have a nice day ..😁😍")
    print("Welcome to CIIT Training Institute πŸ“©πŸ”“..!")

The function has only been defined so far. The function must be called to execute the code.

4. Calling a Function

To call a function, use parentheses after the function name. parentheses () use karte hain.

def welcome():
    print("Welcome to CIIT Training Institute πŸ“©πŸ”“..!")

welcome()
Output:
Welcome to CIIT Training Institute πŸ“©πŸ”“..!

5. Calling a Function Multiple Times

A function can be called multiple times.

def message():
    print("Learn Python with CIIT πŸ‘¨β€πŸ«")

message()
message()
message()
Output:
Learn Python with CIITπŸ‘¨β€πŸ«
Learn Python with CIIT πŸ‘¨β€πŸ«
Learn Python with CIIT πŸ‘¨β€πŸ«

6. Function with Parameters

Parameters are used to pass data to a function.

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

greet("Rahul")
greet("Priya")
Output:
Hello Rahul
Hello Priya

7. Function with Multiple Parameters

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

add(10, 20)
add(50, 30)
Output:
30
80

8. Returning a Value

The return statement is used to send a calculated result from the function to the outside. return statement use karte hain.

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

result = add(10, 20)

print(result)
Output:
30

9. print() vs return

print() return
Screen par output display karta hai Value function ke bahar bhejta hai
Further calculation ke liye directly use nahi hota Returned value ko variable mein store kar sakte hain
def square(number):
    return number * number

result = square(5)

print(result)

10. Default Parameter

If the caller does not provide a value, a default value can be used.

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

greet()
greet("Amit")
Output:
Hello Student
Hello Amit

11. Keyword Arguments

With keyword arguments, you can pass a value by specifying the parameter name.

def student_info(name, course):
    print("Name:", name)
    print("Course:", course)

student_info(
    course="Python Full Stack",
    name="Rahul"
)
Output:
Name: Rahul
Course: Python Full Stack

12. CIIT Example πŸ§‘β€πŸŽ“πŸŒ

Suppose we want to display a student’s course and fee at CIIT. We can create a reusable function for this.

def student_details(name, course, fee):
    print("Student:", name)
    print("Course:", course)
    print("Fee:", fee)

student_details(
    "Amit",
    "Python Full Stack",
    35000
)
Output:
Student: Amit
Course: Python Full Stack
Fee: 35000

13. Function Execution Flow

Step 1: Define the function
↓
Step 2: Call the function
↓
Step 3: Pass the arguments
↓
Step 4: The function body is executed
↓
Step 5: The result is returned

14. Function Best Practices

  • Use a meaningful name for the function.
  • Preferably give a function one clear responsibility.
  • Convert repeated code into a function.
  • Pass required data through parameters.
  • Use return when you need a result.
  • Generally use lowercase and underscore style for function names.

15. Common Mistakes

# Wrong
def greet()
    print("Hello")

# Correct
def greet():
    print("Hello")
# Function defined but not called

def greet():
    print("Hello")

# Call the function
greet()
Important: Indentation is very important in Python. Code inside a function must be written with proper indentation.

16. Interview Points

  • In Python, the kaunsa keyword use hota hai?
  • How do you execute a function?
  • What is the difference between a parameter and an argument?
  • What is the difference between print() and return?
  • What is a default parameter?
  • What is a keyword argument?

CIIT Learning Point

Functions are a core concept in Python programming. If you want to write reusable, clean, and maintainable code, it is very important to use functions properly.

CIIT Practice Tasks

  1. Greeting Function:
    Create a greet() function that "Welcome to CIIT Training Institute" prints it.
  2. Addition Function:
    add(a, b) create a function that returns the sum of two numbers.
  3. Square Function:
    square(number) function create karo that returns the square of a number.
  4. Student Details:
    Create a function that accepts the student name, course, and city and displays the details.
  5. Even Number Function:
    is_even(number) create a function that return True if the number is even; otherwise return False.

Summary :

Python functions are reusable blocks of code. A function is defined using the def keyword and called using parentheses after the function name. You can pass data through parameters and return statement ke through the result outside the function using the statement.