A function is a reusable block of code that performs a specific task. Functions help us organize code, avoid repetition, and make programs easier to understand and maintain.
A function is a named block of code that can be executed whenever it is needed.
def greet():
print("Hello!")
greet()
Functions provide several benefits:
Use the def keyword followed by the function name and parentheses.
def welcome():
print("Welcome to Python")
The indented code below the function definition is called the function body.
def function_name():
# function body
statement
Example:
def hello():
print("Hello Python")
Defining a function does not execute it. You need to call the function.
def greet():
print("Good Morning")
greet()
A function can be called multiple times.
def greet():
print("Hello!")
greet()
greet()
greet()
A function does not always need parameters.
def show_message():
print("Learn Python")
show_message()
A parameter allows a function to receive data.
def greet(name):
print("Hello", name)
greet("Amit")
Here, name is the parameter and "Amit" is the argument passed to the function.
A function can have more than one parameter.
def add(a, b):
print(a + b)
add(10, 20)
A parameter is a variable defined inside the function definition. An argument is the actual value passed when calling the function.
def greet(name):
print("Hello", name)
greet("Rahul")
A function can send a result back to the calling code using the return statement.
def add(a, b):
return a + b
result = add(10, 20)
print(result)
print() displays a value on the screen, while return sends a value back from the function.
def add(a, b):
return a + b
result = add(5, 10)
print(result)
A Python function can return multiple values. Python packages these values into a tuple.
def calculate(a, b):
return a + b, a - b
result = calculate(20, 5)
print(result)
def calculate(a, b):
return a + b, a * b
sum_value, product = calculate(5, 4)
print("Sum:", sum_value)
print("Product:", product)
A parameter can have a default value. The default value is used when no argument is supplied for that parameter.
def greet(name="Student"):
print("Hello", name)
greet()
greet("Amit")
You can pass arguments using parameter names.
def student(name, age):
print("Name:", name)
print("Age:", age)
student(age=20, name="Rahul")
When arguments are passed according to their position, they are called positional arguments.
def student(name, age):
print(name)
print(age)
student("Amit", 21)
def square(number):
return number * number
print(square(5))
print(square(8))
def check_number(number):
if number % 2 == 0:
return "Even"
else:
return "Odd"
print(check_number(10))
print(check_number(7))
def check_age(age):
if age >= 18:
return "Eligible"
else:
return "Not Eligible"
print(check_age(20))
print(check_age(15))
A list can be passed to a function as an argument.
def display_items(items):
for item in items:
print(item)
fruits = ["Apple", "Banana", "Mango"]
display_items(fruits)
def show_student(student):
for key, value in student.items():
print(key, ":", value)
data = {
"name": "Amit",
"course": "Python"
}
show_student(data)
If a function does not explicitly return a value, Python returns None.
def greet():
print("Hello")
result = greet()
print(result)
A variable created inside a function is generally a local variable. It can normally be accessed only inside that function.
def show():
message = "Hello Python"
print(message)
show()
Variables created inside a function have local scope unless they refer to variables from an outer scope.
def calculate():
number = 100
print(number)
calculate()
The variable number is local to the function.
One function can call another function.
def message():
print("Welcome to Python")
def start():
message()
print("Let's learn programming")
start()
A function can be reused with different arguments.
def multiply(a, b):
return a * b
print(multiply(2, 5))
print(multiply(4, 10))
print(multiply(7, 3))
Function names should follow Python's naming rules:
Example:
def calculate_total():
print("Calculating total")
A function can contain a docstring that describes what the function does.
def add(a, b):
"""Return the sum of two numbers."""
return a + b
print(add(10, 20))
def calculate_result(marks):
total = sum(marks)
average = total / len(marks)
return total, average
marks = [80, 75, 90, 85]
total, average = calculate_result(marks)
print("Total:", total)
print("Average:", average)
Question: Which keyword is used to define a function in Python?