Functions are one of the most important concepts in C programming. A function is a reusable block of code designed to perform a specific task. Instead of writing the same code multiple times, you can create a function once and call it whenever needed.
Functions improve code readability, reduce duplication, simplify debugging, and make programs modular. They are widely used in real-world software development, from simple calculators to large enterprise applications. C Functions practice questions with solutions help to understand the concepts.
In this chapter, you’ll practice beginner-friendly function programs with complete solutions, sample outputs, explanations, and concepts covered.
1. C Program to Create and Call a Simple Function
Problem Statement
Write a C program to create a simple function that displays “Welcome to C Programming!” and call it from the main() function.
C Solution
#include <stdio.h>
void displayMessage()
{
printf("Welcome to C Programming!");
}
int main()
{
displayMessage();
return 0;
}
Sample Output
Welcome to C Programming!
Explanation
displayMessage()is a user-defined function.- It does not accept any parameters and does not return any value.
- The function is called from the
main()function.
Concepts Covered
- User-Defined Function
- Function Call
- void Function
- Code Reusability
2. C Program to Find the Sum of Two Numbers Using a Function
Problem Statement
Write a C program to calculate the sum of two numbers using a user-defined function.
C Solution
#include <stdio.h>
int addNumbers(int a, int b)
{
return a + b;
}
int main()
{
int first, second, sum;
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);
sum = addNumbers(first, second);
printf("Sum = %d", sum);
return 0;
}
Sample Output
Enter first number: 20
Enter second number: 15
Sum = 35
Explanation
The function addNumbers() accepts two integers as parameters, calculates their sum, and returns the result to the main() function.
Concepts Covered
- Function Parameters
- Function Return Value
- Function Call
- Integer Return Type
3. C Program to Find the Square of a Number Using a Function
Problem Statement
Write a C program to calculate the square of a number using a function.
C Solution
#include <stdio.h>
int square(int number)
{
return number * number;
}
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("Square = %d", square(number));
return 0;
}
Sample Output
Enter a number: 9
Square = 81
Explanation
The function receives one integer parameter and returns its square.
Example:
9 × 9 = 81
Concepts Covered
- Function Arguments
- Return Statement
- Arithmetic Operations
- User-Defined Functions
4. C Program to Find the Cube of a Number Using a Function
Problem Statement
Write a C program to calculate the cube of a number using a user-defined function.
C Solution
#include <stdio.h>
int cube(int number)
{
return number * number * number;
}
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("Cube = %d", cube(number));
return 0;
}
Sample Output
Enter a number: 4
Cube = 64
Explanation
The function cube() accepts one integer as a parameter and returns the cube of the number.
Formula:
Cube = Number × Number × Number
Concepts Covered
- User-Defined Function
- Function Parameter
- Return Statement
- Arithmetic Operations
5. C Program to Find the Maximum of Two Numbers Using a Function
Problem Statement
Write a C program to find the larger of two numbers using a function.
C Solution
#include <stdio.h>
int maximum(int first, int second)
{
if(first > second)
return first;
else
return second;
}
int main()
{
int first, second;
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);
printf("Largest Number = %d", maximum(first, second));
return 0;
}
Sample Output
Enter first number: 45
Enter second number: 32
Largest Number = 45
Explanation
The function compares the two numbers and returns the greater value to the main() function.
Concepts Covered
- Function with Parameters
- if-else Statement
- Return Value
- Comparison Logic
6. C Program to Check Whether a Number is Even or Odd Using a Function
Problem Statement
Write a C program to check whether a given number is even or odd using a user-defined function.
C Solution
#include <stdio.h>
void checkEvenOdd(int number)
{
if(number % 2 == 0)
{
printf("%d is an Even Number.", number);
}
else
{
printf("%d is an Odd Number.", number);
}
}
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
checkEvenOdd(number);
return 0;
}
Sample Output
Enter a number: 28
28 is an Even Number.
Explanation
The function receives a number as a parameter and checks whether it is divisible by 2.
- If the remainder is 0, it is an even number.
- Otherwise, it is an odd number.
Concepts Covered
- Function with Parameters
- void Function
- if-else Statement
- Modulus Operator
7. C Program to Find the Factorial of a Number Using a Function
Problem Statement
Write a C program to calculate the factorial of a number using a function.
C Solution
#include <stdio.h>
long long factorial(int number)
{
long long fact = 1;
int i;
for(i = 1; i <= number; i++)
{
fact = fact * i;
}
return fact;
}
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("Factorial = %lld", factorial(number));
return 0;
}
Sample Output
Enter a number: 6
Factorial = 720
Explanation
The function calculates the factorial using a for loop and returns the result to the main() function.
Example:
6! = 6 × 5 × 4 × 3 × 2 × 1 = 720
Concepts Covered
- Function Returning Value
- for Loop
- Factorial Logic
8. C Program to Check Whether a Number is Prime Using a Function
Problem Statement
Write a C program to determine whether a number is prime using a user-defined function.
C Solution
#include <stdio.h>
int isPrime(int number)
{
int i;
if(number <= 1)
return 0;
for(i = 2; i < number; i++)
{
if(number % i == 0)
return 0;
}
return 1;
}
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if(isPrime(number))
printf("Prime Number");
else
printf("Not a Prime Number");
return 0;
}
Sample Output
Enter a number: 17
Prime Number
Explanation
A prime number is divisible only by 1 and itself.
The function checks whether any number between 2 and number – 1 divides the entered number.
Concepts Covered
- Function Returning Integer
- Prime Number Logic
- Loop
- Conditional Statements
9. C Program to Find the Power of a Number Using a Function
Problem Statement
Write a C program to calculate the power of a number using a function.
C Solution
#include <stdio.h>
long long power(int base, int exponent)
{
long long result = 1;
int i;
for(i = 1; i <= exponent; i++)
{
result = result * base;
}
return result;
}
int main()
{
int base, exponent;
printf("Enter base: ");
scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &exponent);
printf("Result = %lld", power(base, exponent));
return 0;
}
Sample Output
Enter base: 3
Enter exponent: 4
Result = 81
Explanation
The function repeatedly multiplies the base by itself until the specified exponent is reached.
Example:
3 × 3 × 3 × 3 = 81
Concepts Covered
- Function Parameters
- Return Statement
- for Loop
- Repeated Multiplication
10. C Program to Calculate the Area of a Circle Using a Function
Problem Statement
Write a C program to calculate the area of a circle using a user-defined function.
C Solution
#include <stdio.h>
float areaOfCircle(float radius)
{
return 3.14159 * radius * radius;
}
int main()
{
float radius;
printf("Enter radius: ");
scanf("%f", &radius);
printf("Area = %.2f", areaOfCircle(radius));
return 0;
}
Sample Output
Enter radius: 7
Area = 153.94
Explanation
The function calculates the area using the formula:
Area = π × r²
where π ≈ 3.14159.
Concepts Covered
- Function Returning Float
- Mathematical Formula
- Function Call
- Float Data Type
11. C Program to Swap Two Numbers Using a Function
Problem Statement
Write a C program to swap two numbers using a user-defined function.
C Solution
#include <stdio.h>
void swapNumbers(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int first, second;
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);
swapNumbers(&first, &second);
printf("After Swapping:\n");
printf("First Number = %d\n", first);
printf("Second Number = %d", second);
return 0;
}
Sample Output
Enter first number: 15
Enter second number: 30
After Swapping:
First Number = 30
Second Number = 15
Explanation
The function receives the memory addresses of both variables and swaps their values using a temporary variable.
Concepts Covered
- Function with Parameters
- Pointer Parameters
- Swapping Values
- Call by Reference
12. C Program to Find the Minimum of Two Numbers Using a Function
Problem Statement
Write a C program to find the smaller of two numbers using a function.
C Solution
#include <stdio.h>
int minimum(int first, int second)
{
if(first < second)
return first;
else
return second;
}
int main()
{
int first, second;
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);
printf("Smallest Number = %d", minimum(first, second));
return 0;
}
Sample Output
Enter first number: 18
Enter second number: 42
Smallest Number = 18
Explanation
The function compares the two values and returns the smaller number.
Concepts Covered
- User-Defined Function
- Return Value
- Comparison
- if-else Statement
13. C Program to Find the Length of a String Using a Function
Problem Statement
Write a C program to calculate the length of a string using a user-defined function.
C Solution
#include <stdio.h>
int stringLength(char str[])
{
int i = 0;
while(str[i] != '\0')
{
i++;
}
return i;
}
int main()
{
char text[100];
printf("Enter a string: ");
scanf("%s", text);
printf("Length = %d", stringLength(text));
return 0;
}
Sample Output
Enter a string: Programming
Length = 11
Explanation
The function traverses the string character by character until it reaches the null character ('\0').
The total count of characters is returned.
Concepts Covered
- Functions
- Strings
- Character Arrays
- while Loop
14. C Program to Calculate the Average of Three Numbers Using a Function
Problem Statement
Write a C program to calculate the average of three numbers using a function.
C Solution
#include <stdio.h>
float average(float a, float b, float c)
{
return (a + b + c) / 3;
}
int main()
{
float first, second, third;
printf("Enter first number: ");
scanf("%f", &first);
printf("Enter second number: ");
scanf("%f", &second);
printf("Enter third number: ");
scanf("%f", &third);
printf("Average = %.2f", average(first, second, third));
return 0;
}
Sample Output
Enter first number: 20
Enter second number: 40
Enter third number: 60
Average = 40.00
Explanation
The function receives three floating-point numbers and returns their average.
Formula:
Average = (a + b + c) / 3
Concepts Covered
- Float Functions
- Function Parameters
- Return Statement
15. C Program to Display a Menu Using Functions
Problem Statement
Write a C program that displays a simple menu using a user-defined function.
C Solution
#include <stdio.h>
void displayMenu()
{
printf("===== MENU =====\n");
printf("1. Add Numbers\n");
printf("2. Subtract Numbers\n");
printf("3. Multiply Numbers\n");
printf("4. Exit\n");
}
int main()
{
displayMenu();
return 0;
}
Sample Output
===== MENU =====
1. Add Numbers
2. Subtract Numbers
3. Multiply Numbers
4. Exit
Explanation
The menu is placed inside a separate function so that it can be reused multiple times without rewriting the same code.
Concepts Covered
- User-Defined Function
- Function Reusability
- Modular Programming
Chapter Summary
In this chapter, you learned how functions help organize C programs into reusable and modular blocks of code. You practiced creating functions with and without parameters, functions that return values, and functions that work with numbers, strings, and pointers. You also solved practical problems such as finding factorials, prime numbers, averages, powers, string lengths, and swapping values using functions.
Key Takeaways
- Functions improve code reusability.
- Functions make programs easier to read and maintain.
- A function can accept parameters.
- A function can return a value.
voidfunctions do not return any value.- Functions reduce duplicate code.
- Pointer parameters allow functions to modify original variables.
- Functions are an important part of modular programming.
- Large programs are divided into smaller functions.
- Functions are used extensively in real-world software development.
Frequently Asked Questions (FAQs)
1. What is a function in C?
A function is a reusable block of code designed to perform a specific task.
2. Why are functions used in C programming?
Functions reduce code duplication, improve readability, simplify debugging, and make programs modular.
3. What is the difference between a function declaration and a function definition?
- A function declaration tells the compiler about the function.
- A function definition contains the actual implementation of the function.
4. What is a function call?
A function call executes the code inside a function whenever it is needed.
5. What is the difference between void and int functions?
- A
voidfunction does not return any value. - An
intfunction returns an integer value.
6. What are function parameters?
Function parameters are variables that receive values from the calling function.
7. What is call by reference?
Call by reference passes the memory address of variables to a function, allowing the function to modify the original values.
8. Why are functions important in real-world programming?
Functions make software easier to maintain, test, debug, and expand. They are used in operating systems, banking software, web applications, embedded systems, games, and enterprise applications, making them one of the most essential concepts in C programming.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
