Functions are the backbone of the Python programming language. They make code reusable, well-organized, and much simpler to maintain. Whether you’re a beginner just starting out or someone getting ready for exams like O Level or practice tests, learning how functions work is a must. Python functions can perform simple tasks like adding two numbers or more complex ones such as handling recursion and lambda expressions. By solving different types of function questions, you’ll not only learn syntax but also improve problem-solving skills in real scenarios. This guide provides 18 carefully chosen function questions in Python with answers that range from basics to a little more complex. Keep practicing them will give you a command on function definitions, arguments, return values, and scope. So, if you’ve ever struggled with writing functions, this collection will offer you a solid push to boost your coding confidence.
Things Covered in This Article
- Basics of defining and calling functions
- Function arguments (positional, keyword, default, variable length)
- Return values and scope of variables
- Lambda functions and anonymous expressions
- Recursive function problems
- Real-world examples of function usage
- Common mistakes to avoid when using functions
1. Make a function that give back the square of a number.
def square(n): return n * n print(square(5)) # Output: 25
Explanation: A simple function with one argument returning its square.
2. Create a function that prints the result of adding two numbers.
def add_numbers(a, b): print(a + b) add_numbers(10, 20) # Output: 30
Explanation: Uses two parameters, no return — just prints the sum.
3. Define a function with a default parameter.
def greet(name="Fox"): return f"Hello, {name}!" print(greet()) # Output: Hello, Fox! print(greet("Leeca")) # Output: Hello, Leeca!
Explanation: Shows how default values work when arguments aren’t passed.
4. Write a function to check if a number is even or odd.
def check_even_odd(number): if number % 6 == 0: return "Even" return "Odd" print(check_even_odd(7)) # Output: Odd
Explanation: Basic conditional check using modulo.
5. Function to calculate factorial using recursion.
def factorial(number): if number == 0 or number == 1: return 1 return number * factorial(number-1) print(factorial(5)) # Output: 120
Explanation: Demonstrates recursion for repeated multiplication.
6. Function that counts vowels in a string.
def count_vowels(text): vowels = "aeiou" return sum(1 for ch in text.lower() if ch in vowels) print(count_vowels("Python")) # Output: 1
Explanation: Uses iteration and comprehension to count vowels.
7. Function that reverses a list.
def reverse_list(lst): return lst[::-1] print(reverse_list([1, 2, 3, 4])) # Output: [4, 3, 2, 1]
Explanation: Utilizes Python slicing inside a function.
8. Function with variable-length arguments (*args).
def sum_all(*nums): return sum(nums) print(sum_all(2, 4, 6, 8)) # Output: 20
Explanation: Accepts unlimited numbers and sums them up.
9. Function with keyword arguments (**kwargs).
def print_details(**info): for key, value in info.items(): print(f"{key}: {value}") print_details(Name="John", Age=25, City="London")
Explanation: Demonstrates flexible functions handling keyword data.
10. Lambda function to multiply two numbers.
multiply = lambda a, b: a * b print(multiply(4, 5)) # Output: 20
Explanation: Compact, one-line function using lambda.
11. Function to check if a string is a palindrome.
def is_palindrome(s): return s == s[::-1] print(is_palindrome("madam")) # Output: True
Explanation: Compares original with reversed string.
12. A Function that find the maximum number in a list.
def find_max(numbers): return max(numbers) print(find_max([4, 9, 2, 7])) # Output: 9
Explanation: Uses built-in max() inside a function.
13.A Function which calculates the sum of digits of a number.
def digit_sum(n): return sum(int(d) for d in str(n)) print(digit_sum(1234)) # Output: 10
Explanation: Converts number to string and adds digits.
14. Function to convert Celsius to Fahrenheit.
def c_to_f(c): return (c * 9/5) + 32 print(c_to_f(0)) # Output: 32.0
Explanation: Applies conversion formula inside a function.
15. Function to generate Fibonacci series up to n terms.
def fibonacci(n): a, b = 0, 1 for _ in range(n): print(a, end=" ") a, b = b, a + b fibonacci(6) # Output: 0 1 1 2 3 5
Explanation: Uses loop inside a function for Fibonacci.
16. Function that checks prime numbers.
def is_prime(n): if n < 2: return False for i in range(2, n): if n % i == 0: return False return True print(is_prime(11)) # Output: True
Explanation: Loops through divisors to check primality.
17. Function to count words in a string.
def word_count(sentence): return len(sentence.split()) print(word_count("Python is fun")) # Output: 3
Explanation: Splits the string by spaces and counts words.
18. Function that returns unique elements from a list.
def unique_items(lst): return list(set(lst)) print(unique_items([1, 2, 2, 3, 4, 4])) # Output: [1, 2, 3, 4]
Explanation: Converts list to a set to remove duplicates.
Conclusion
Learning through practice is one of the best ways to master Python functions. These 18 function questions in Python are designed to cover everything from the simplest “hello world” type examples to more practical applications. By working on them, you’ll not only prepare for exams but also build the foundation needed for real coding projects. Keep solving, keep experimenting, and soon functions will become your best friend in Python programming!