- Declaring, defining, and calling functions with parameters and return values
- Pass-by-value semantics and how to return results from functions
- Writing recursive functions (Fibonacci, GCD, power, subsequences)
- Passing arrays and matrices to functions
- Working with strings inside functions (length, reverse, concatenation)
- Building reusable helper functions for math, number theory, and geometry
- Implementing classic algorithms — Kadane's, majority element, longest increasing subsequence
- Decomposing a large problem into smaller, testable functions
- Understanding scope, visibility, and lifetime of variables
- Structuring multi-file and modular programs
Functions are the building blocks of every serious C program. They let you break a large, intimidating problem into small, testable pieces — and reuse those pieces across programs. This page collects 33 function assignments, arranged from simple arithmetic to recursive algorithms, string manipulation, array processing, matrix operations, and advanced interview-style problems. Every assignment links to a full tutorial with the program code, sample output, and a line-by-line explanation.
🔹 Basic Math Functions
Start here — simple arithmetic and geometry functions to get comfortable with parameters and return values.
- 1. Write a C program to find the sum of two integers using a function.
- 5. Create a C program to calculate the area of a rectangle using a function.
- 6. Write a program to convert temperature from Celsius to Fahrenheit using a function.
- 10. Develop a program to calculate the power of a number using a function.
🔹 Number Checks
Functions that return a yes/no answer or a computed value — prime, even/odd, GCD, LCM, leap year, palindrome.
- 2. Write a program to check whether a given number is prime or not using a function.
- 4. Write a program to check whether a number is even or odd using a function.
- 7. Develop a program to find the GCD (Greatest Common Divisor) of two numbers using a function.
- 8. Write a program to find the LCM (Least Common Multiple) of two numbers using a function.
- 11. Write a program to check if a given year is a leap year using a function.
- 14. Write a program to check if a given number is a palindrome using a function.
🔹 Series & Tables
Generate sequences, swap values, and print tables from within functions.
- 3. Write a program in C to find the nth term of the Fibonacci series using a function.
- 9. Create a program to swap two numbers using a function.
- 12. Create a program to print all prime numbers between two given numbers using a function.
- 13. Develop a program to generate a multiplication table of a given number using a function.
🔹 String Functions
Pass strings to functions, process them character by character, and return results.
- 15. Create a program to reverse a string using a function.
- 16. Develop a program to find the length of a string using a function.
- 17. Write a program to concatenate two strings using a function.
🔹 Array Functions
Pass arrays to functions, find max/min, sum elements, search, and sort.
- 18. Create a program to find the maximum element in an array using a function.
- 19. Develop a program to find the minimum element in an array using a function.
- 20. Write a program to calculate the sum of elements in an array using a function.
- 21. Create a program to search for an element in an array using a function.
- 22. Develop a program to sort elements in an array in ascending order using a function.
🔹 Matrix Functions
2D arrays, matrix operations, and symmetric checks — all implemented with helper functions.
- 23. Write a program to perform matrix addition using a function.
- 24. Create a program to calculate the transpose of a matrix using a function.
- 25. Develop a program to check if a matrix is symmetric using a function.
- 26. Write a program to perform matrix multiplication using a function.
- 27. Create a program to find the sum of diagonal elements in a matrix using a function.
🔹 Advanced Algorithms
Interview favorites and algorithm classics implemented as reusable functions.
- 28. Develop a program to find the largest sum subarray in an array using a function.
- 29. Write a program to find the longest increasing subsequence in an array using a function.
- 30. Create a program to find the median of two sorted arrays using a function.
- 31. Develop a program to find the majority element in an array using a function.
- 32. Write a program to find the maximum product subarray in an array using a function.
- 33. Create a program to find the contiguous subarray with the largest sum (Kadane's algorithm) using a function.
How to Get the Most Out of These Assignments
Reading a solution is not the same as writing one. For each assignment, follow this three-step workflow:
- Attempt first. Open a blank editor and try to solve the problem using only the assignment description. Even if you get stuck, the act of trying cements the concept.
- Compare carefully. Open the solution page and compare your approach to the reference. Look for differences in parameter passing, return type, and edge-case handling.
- Re-implement from scratch. Close the solution page and write the program again without looking. This is the step most learners skip — and it's the one that actually builds fluency.
💡 Tip: Always compile with gcc -Wall -Wextra -std=c11 -g. The -Wall and -Wextra flags warn you about missing prototypes, unused parameters, and implicit conversions — all common mistakes when writing your first functions.
❓ Function Viva Questions & Quick Answers
Q1. What is the difference between a function declaration and a function definition?
A declaration (prototype) tells the compiler the function's name, return type, and parameter types. A definition provides the actual body of the function. You can declare a function many times, but define it only once.
Q2. What does "pass by value" mean in C?
When you pass an argument to a function, C copies its value into the parameter. Changes made to the parameter inside the function do not affect the original variable in the caller.
Q3. How can a function modify a variable in the caller?
By accepting a pointer to the variable. Dereferencing the pointer inside the function allows you to modify the original value. This is commonly called "pass by reference" — implemented in C using pointers.
Q4. What is recursion, and when should I use it?
Recursion is a function that calls itself. Use it when a problem naturally breaks into smaller instances of the same problem — e.g., Fibonacci, GCD, tree traversal. Always include a base case, or the recursion will run forever.
Q5. Can a function return an array?
Not directly — C functions cannot return arrays. You can return a pointer to an array (which must not be a local variable), or pass an output array as a parameter and fill it inside the function.
Q6. What is the difference between a function prototype and a function header?
The prototype ends with a semicolon and contains no body — e.g., int add(int, int);. The header is the first line of the definition — e.g., int add(int a, int b) — and is followed by the function body.