Functions help you organize your code into reusable blocks. Instead of writing the same code multiple times, you can create a function once and call it whenever needed. In this practice set, you’ll learn how to create functions, pass arguments, return values, use default parameters, and solve beginner-friendly Python programs. Python functions practice questions with solutions help to understand the concept
1. Python Program to Create and Call a Function
Difficulty: Foundation
Problem Statement
Write a Python function that prints “Welcome to CodeMantra!” and call it.
Python Solution
def welcome():
print("Welcome to CodeMantra!")
welcome()
Sample Output
Welcome to CodeMantra!
Explanation
A function is created using the def keyword and executed by calling its name.
Concepts Covered
- Function
- def keyword
- Function Call
2. Python Program to Add Two Numbers Using a Function
Difficulty: Foundation
Problem Statement
Write a Python function that accepts two numbers and prints their sum.
Python Solution
def add_numbers(num1, num2):
print("Sum:", num1 + num2)
add_numbers(15, 25)
Sample Output
Sum: 40
Explanation
Function parameters receive values passed during the function call.
Concepts Covered
- Function Parameters
- Function Arguments
3. Python Program to Find the Square of a Number Using a Function
Difficulty: Foundation
Problem Statement
Write a Python function that returns the square of a number.
Python Solution
def square(number):
return number * number
result = square(8)
print(result)
Sample Output
64
Explanation
The return statement sends a value back to the function call.
Concepts Covered
- return Statement
4. Python Program to Check Whether a Number is Even or Odd Using a Function
Difficulty: Foundation
Problem Statement
Write a Python function that checks whether a number is even or odd.
Python Solution
def check_even_odd(number):
if number % 2 == 0:
return "Even"
else:
return "Odd"
print(check_even_odd(27))
Sample Output
Odd
Explanation
Functions can return different values based on conditions.
Concepts Covered
- if…else
- return
5. Python Program to Find the Largest of Three Numbers Using a Function
Difficulty: Practice
Problem Statement
Write a Python function that returns the largest among three numbers.
Python Solution
def largest(a, b, c):
return max(a, b, c)
print(largest(25, 80, 45))
Sample Output
80
Explanation
The built-in max() function returns the largest value.
Concepts Covered
- max()
- Function Return Value
6. Python Program to Calculate the Factorial of a Number Using a Function
Difficulty: Practice
Problem Statement
Write a Python function to calculate the factorial of a number.
Python Solution
def factorial(number):
result = 1
for i in range(1, number + 1):
result *= i
return result
print(factorial(5))
Sample Output
120
Explanation
The function multiplies numbers from 1 to the given number.
Concepts Covered
- for Loop
- Functions
7. Python Program to Use Default Function Arguments
Difficulty: Practice
Problem Statement
Write a Python function that greets a user. If no name is provided, print “Guest”.
Python Solution
def greet(name="Guest"):
print("Welcome", name)
greet()
greet("Rahul")
Sample Output
Welcome Guest
Welcome Rahul
Explanation
Default arguments are used when no value is passed during the function call.
Concepts Covered
- Default Arguments
8. Python Program to Return Multiple Values from a Function
Difficulty: Challenge
Problem Statement
Write a Python function that returns both the sum and product of two numbers.
Python Solution
def calculate(a, b):
return a + b, a * b
sum_value, product = calculate(5, 10)
print("Sum:", sum_value)
print("Product:", product)
Sample Output
Sum: 15
Product: 50
Explanation
Python functions can return multiple values as a tuple.
Concepts Covered
- Multiple Return Values
9. Python Program to Calculate the Average of Numbers Using a Function
Difficulty: Challenge
Problem Statement
Write a Python function that accepts three numbers and returns their average.
Python Solution
def average(a, b, c):
return (a + b + c) / 3
print(average(20, 30, 40))
Sample Output
30.0
Explanation
The function calculates the average and returns the result.
Concepts Covered
- Function Parameters
- Arithmetic Operations
10. Python Program to Find Whether a Number is Positive or Negative Using a Function
Difficulty: Challenge
Problem Statement
Write a Python function that returns whether a number is positive, negative, or zero.
Python Solution
def check_number(number):
if number > 0:
return "Positive"
elif number < 0:
return "Negative"
else:
return "Zero"
print(check_number(-15))
Sample Output
Negative
Explanation
The function uses conditional statements to determine the type of number.
Concepts Covered
- Functions
- if…elif…else
- return Statement
Chapter Summary
After completing this chapter, you have learned:
- Creating functions
- Calling functions
- Function parameters
- Function arguments
- Return statement
- Default arguments
- Multiple return values
- Using functions with loops
- Using functions with conditions
- Writing reusable Python code
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
