Functions in C Practice Questions with Solutions

Introduction

Functions in C are used to divide a large program into smaller, reusable blocks of code. Instead of writing the same logic repeatedly, you can create a function once and call it whenever needed. In this chapter, you will practice functions through 10 beginner-friendly examples, starting with simple functions and gradually moving to functions with parameters, calculations, conditions, and repeated use. These examples build the foundation for function arguments and return values covered in later chapters. Functions in C Practice questions with solutions to help you understand the concepts.

Q1. Create a Simple Function to Print a Message

Problem Statement

Write a C program that creates a function named greet() and uses it to print a welcome message.

C Program

#include <stdio.h>

void greet()
{
    printf("Welcome to C Programming!");
}

int main()
{
    greet();

    return 0;
}

Sample Output

Welcome to C Programming!

Explanation

The function is created using:

void greet()
{
    printf("Welcome to C Programming!");
}

Here:

  • void means the function does not return a value.
  • greet is the function name.
  • () means this function currently takes no arguments.

The function is called inside main():

greet();

Concepts Covered

  • Function definition
  • void
  • Function calling
  • main()

Q2. Create a Function to Print Your Name

Problem Statement

Write a C program that creates a function named displayName() to print a name.

C Program

#include <stdio.h>

void displayName()
{
    printf("My name is Rahul.");
}

int main()
{
    displayName();

    return 0;
}

Sample Output

My name is Rahul.

Explanation

The function contains the code responsible for displaying the name.

void displayName()
{
    printf("My name is Rahul.");
}

Calling:

displayName();

runs the statements inside the function.

A function can be called more than once.

For example:

displayName();
displayName();

would execute the function twice.

Concepts Covered

  • Function creation
  • Function call
  • Code reuse
  • void function

Q3. Create a Function to Print Numbers from 1 to 10

Problem Statement

Create a function named printNumbers() that prints numbers from 1 to 10.

C Program

#include <stdio.h>

void printNumbers()
{
    int i;

    for (i = 1; i <= 10; i++)
    {
        printf("%d ", i);
    }
}

int main()
{
    printNumbers();

    return 0;
}

Sample Output

1 2 3 4 5 6 7 8 9 10

Explanation

The loop is placed inside the function:

void printNumbers()
{
    int i;

    for (i = 1; i <= 10; i++)
    {
        printf("%d ", i);
    }
}

When main() calls:

printNumbers();

the entire loop executes.

Functions are useful because the same block can be reused whenever required.

Concepts Covered

  • Function
  • for loop
  • Function calling
  • Code reuse

Q4. Create a Function to Find the Square of a Number

Problem Statement

Create a function named square() that accepts a number and prints its square.

C Program

#include <stdio.h>

void square(int number)
{
    int result;

    result = number * number;

    printf("Square = %d", result);
}

int main()
{
    square(6);

    return 0;
}

Sample Output

Square = 36

Explanation

The function accepts a value through:

void square(int number)

Here, number is called a parameter.

When we write:

square(6);

the value 6 is passed to number.

The calculation becomes:

6 × 6 = 36

Concepts Covered

  • Function parameter
  • Function argument
  • Multiplication
  • Function call

Q5. Create a Function to Add Two Numbers

Problem Statement

Create a function named add() that accepts two numbers and prints their sum.

C Program

#include <stdio.h>

void add(int a, int b)
{
    int sum;

    sum = a + b;

    printf("Sum = %d", sum);
}

int main()
{
    add(10, 20);

    return 0;
}

Sample Output

Sum = 30

Explanation

The function has two parameters:

int a, int b

When we call:

add(10, 20);

the values are assigned like this:

a = 10
b = 20

Then:

sum = a + b;

calculates:

10 + 20 = 30

Concepts Covered

  • Multiple parameters
  • Function arguments
  • Addition
  • Function call

Q6. Create a Function to Check Even or Odd

Problem Statement

Create a function named checkEvenOdd() that accepts a number and checks whether it is even or odd.

C Program

#include <stdio.h>

void checkEvenOdd(int number)
{
    if (number % 2 == 0)
    {
        printf("%d is even.", number);
    }
    else
    {
        printf("%d is odd.", number);
    }
}

int main()
{
    checkEvenOdd(15);

    return 0;
}

Sample Output

15 is odd.

Explanation

The function receives a number:

checkEvenOdd(15);

The % operator finds the remainder.

For an even number:

number % 2 = 0

For an odd number:

number % 2 != 0

The function then uses if-else to display the result.

Concepts Covered

  • Function parameter
  • if-else
  • Modulus operator
  • Even and odd numbers

Q7. Create a Function to Find the Largest of Two Numbers

Problem Statement

Create a function named findLargest() that accepts two numbers and prints the larger number.

C Program

#include <stdio.h>

void findLargest(int a, int b)
{
    if (a > b)
    {
        printf("Largest number = %d", a);
    }
    else
    {
        printf("Largest number = %d", b);
    }
}

int main()
{
    findLargest(45, 32);

    return 0;
}

Sample Output

Largest number = 45

Explanation

The function receives two values:

a = 45
b = 32

Then it checks:

if (a > b)

Since 45 > 32, the program prints 45.

Concepts Covered

  • Functions
  • Parameters
  • if-else
  • Relational operator
  • Comparison

Q8. Create a Function to Calculate the Factorial

Problem Statement

Create a function named factorial() that accepts a number and prints its factorial.

For example:

5! = 5 × 4 × 3 × 2 × 1 = 120

C Program

#include <stdio.h>

void factorial(int number)
{
    int i;
    int fact = 1;

    for (i = 1; i <= number; i++)
    {
        fact = fact * i;
    }

    printf("Factorial = %d", fact);
}

int main()
{
    factorial(5);

    return 0;
}

Sample Output

Factorial = 120

Explanation

The function starts with:

fact = 1;

The loop multiplies each number from 1 to number.

For 5:

1 × 2 × 3 × 4 × 5 = 120

The function then displays the result.

Concepts Covered

  • Function parameter
  • for loop
  • Factorial
  • Multiplication
  • Accumulator

Q9. Create a Function to Print a Multiplication Table

Problem Statement

Create a function named table() that accepts a number and prints its multiplication table from 1 to 10.

C Program

#include <stdio.h>

void table(int number)
{
    int i;

    for (i = 1; i <= 10; i++)
    {
        printf("%d x %d = %d\n", number, i, number * i);
    }
}

int main()
{
    table(7);

    return 0;
}

Sample Output

7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

Explanation

The function receives 7:

table(7);

Inside the function, the loop runs from 1 to 10.

The expression:

number * i

calculates each multiplication result.

Concepts Covered

  • Function parameter
  • for loop
  • Multiplication
  • Reusable function

Q10. Create Multiple Functions in One Program

Problem Statement

Create separate functions to:

  • Add two numbers
  • Subtract two numbers
  • Multiply two numbers
  • Divide two numbers

Call all four functions from main().

C Program

#include <stdio.h>

void add(int a, int b)
{
    printf("Addition = %d\n", a + b);
}

void subtract(int a, int b)
{
    printf("Subtraction = %d\n", a - b);
}

void multiply(int a, int b)
{
    printf("Multiplication = %d\n", a * b);
}

void divide(int a, int b)
{
    printf("Division = %d\n", a / b);
}

int main()
{
    int a = 20;
    int b = 5;

    add(a, b);
    subtract(a, b);
    multiply(a, b);
    divide(a, b);

    return 0;
}

Sample Output

Addition = 25
Subtraction = 15
Multiplication = 100
Division = 4

Explanation

Instead of putting every calculation directly inside main(), we create separate functions:

add()
subtract()
multiply()
divide()

Then main() calls each function:

add(a, b);
subtract(a, b);
multiply(a, b);
divide(a, b);

This makes the program easier to organize and reuse.

Concepts Covered

  • Multiple functions
  • Function parameters
  • Function calls
  • Arithmetic operations
  • Program organization

Key Takeaways

  • A function is a reusable block of C code designed to perform a specific task.
  • main() is the function where execution of a C program begins.
  • A function can be created once and called multiple times.
  • void means the function does not return a value.
  • Parameters allow a function to receive data.
  • Arguments are the actual values passed when calling a function.
  • A function definition contains the statements that perform the task.
  • A function call executes the function.
  • A function prototype tells the compiler about a function before its definition is encountered.
  • Functions make large programs easier to organize and maintain.
  • A C program can contain multiple user-defined functions.
  • Functions can work together to divide a program into smaller tasks.
  • Function arguments and return values will make functions even more powerful in the next chapters.

FAQs

1. What is a function in C?

A function is a reusable block of code that performs a specific task.

For example:

void greet()
{
    printf("Hello");
}

2. Why are functions used in C?

Functions help divide a large program into smaller, manageable parts. They also allow the same logic to be reused without writing it repeatedly.

3. What is a function call?

A function call is the statement used to execute a function.

For example:

greet();

4. What is a parameter in C?

A parameter is a variable listed in a function definition that receives a value when the function is called.

Example:

void square(int number)

Here, number is a parameter.

5. What is an argument in C?

An argument is the actual value passed to a function when calling it.

For example:

square(5);

Here, 5 is the argument.

6. What does void mean in a function?

void means that the function does not return a value.

Example:

void greet()
{
    printf("Hello");
}

7. Can a C program have multiple functions?

Yes. A C program can contain many functions.

For example:

void add()
{
    // code
}

void subtract()
{
    // code
}

int main()
{
    // code
}

Each function can perform a different task.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top