C++ Functions Practice Questions with Solutions

Functions are one of the most important building blocks in C++ programming. They help break large programs into smaller, reusable, and manageable pieces of code. Instead of writing the same logic multiple times, you can define it once inside a function and call it whenever needed.

Functions improve:

  • Code reusability
  • Readability
  • Maintainability
  • Modularity
  • Debugging efficiency

In C++, functions can:

  • Take no parameters
  • Take one or more parameters
  • Return a value
  • Return no value (void)
  • Be called multiple times from different parts of a program

Functions are widely used in software development, competitive programming, game development, web applications, and system programming.

In this chapter, you’ll solve practical C++ function-based problems that help you understand function declaration, function definition, function calls, parameters, return values, and reusable programming techniques. C++ Functions practice questions with solutions help to understand the concepts.

Each question includes:

  • Problem Statement
  • Complete C++ Solution
  • Sample Input
  • Sample Output
  • Explanation
  • Concepts Covered

Let’s begin with some beginner-friendly function practice questions.


1. C++ Program to Create and Call a Simple Function

Problem Statement

Write a C++ program to create a function named displayMessage() that prints “Welcome to C++ Functions”.

C++ Solution

#include <iostream>
using namespace std;

void displayMessage()
{
    cout << "Welcome to C++ Functions";
}

int main()
{
    displayMessage();

    return 0;
}

Sample Output

Welcome to C++ Functions

Explanation

The function displayMessage() is declared with the void return type because it does not return any value. It is called from the main() function.

Concepts Covered

  • Function Declaration
  • Function Definition
  • Function Call
  • Void Function

2. C++ Program to Print a Number Using a Function

Problem Statement

Write a C++ program to create a function that prints a given number.

C++ Solution

#include <iostream>
using namespace std;

void printNumber(int number)
{
    cout << "Number = " << number;
}

int main()
{
    int num;

    cout << "Enter a number: ";
    cin >> num;

    printNumber(num);

    return 0;
}

Sample Input

Enter a number: 45

Sample Output

Number = 45

Explanation

The value entered by the user is passed as an argument to the function.

Concepts Covered

  • Function Parameters
  • Function Arguments
  • User Input
  • Void Function

3. C++ Program to Add Two Numbers Using a Function

Problem Statement

Write a C++ program to create a function that returns the sum of two numbers.

C++ Solution

#include <iostream>
using namespace std;

int addNumbers(int a, int b)
{
    return a + b;
}

int main()
{
    int num1, num2;

    cout << "Enter first number: ";
    cin >> num1;

    cout << "Enter second number: ";
    cin >> num2;

    cout << "Sum = "
         << addNumbers(num1, num2);

    return 0;
}

Sample Input

Enter first number: 15
Enter second number: 25

Sample Output

Sum = 40

Explanation

The function returns the sum of the two numbers using the return statement.

Concepts Covered

  • Return Statement
  • Integer Return Type
  • Function Parameters

4. 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 <iostream>
using namespace std;

int square(int number)
{
    return number * number;
}

int main()
{
    int num;

    cout << "Enter a number: ";
    cin >> num;

    cout << "Square = "
         << square(num);

    return 0;
}

Sample Input

Enter a number: 9

Sample Output

Square = 81

Explanation

The function multiplies the number by itself and returns the result.

Concepts Covered

  • Function Return Value
  • Arithmetic Operations
  • Function Call

5. C++ Program to Find the Largest of Two Numbers Using a Function

Problem Statement

Write a C++ program to determine the larger of two numbers using a function.

C++ Solution

#include <iostream>
using namespace std;

int largest(int a, int b)
{
    if (a > b)
        return a;
    else
        return b;
}

int main()
{
    int num1, num2;

    cout << "Enter first number: ";
    cin >> num1;

    cout << "Enter second number: ";
    cin >> num2;

    cout << "Largest Number = "
         << largest(num1, num2);

    return 0;
}

Sample Input

Enter first number: 92
Enter second number: 75

Sample Output

Largest Number = 92

Explanation

The function compares both numbers and returns the larger value.

Concepts Covered

  • Function Returning Value
  • if…else
  • Comparison Operators
  • Decision Making

C++ Program to Check Whether a Number is Even or Odd Using a Function

Problem Statement

Write a C++ program to determine whether a number is even or odd using a user-defined function.

C++ Solution

#include <iostream>
using namespace std;

void checkEvenOdd(int number)
{
    if (number % 2 == 0)
        cout << "Even Number";
    else
        cout << "Odd Number";
}

int main()
{
    int num;

    cout << "Enter a number: ";
    cin >> num;

    checkEvenOdd(num);

    return 0;
}

Sample Input

Enter a number: 28

Sample Output

Even Number

Explanation

The function receives the number as a parameter and checks whether it is divisible by 2.

Concepts Covered

  • Function with Parameter
  • if…else
  • Modulus Operator
  • Void Function

7. 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 function.

C++ Solution

#include <iostream>
using namespace std;

bool isPrime(int number)
{
    if (number <= 1)
        return false;

    for (int i = 2; i <= number / 2; i++)
    {
        if (number % i == 0)
            return false;
    }

    return true;
}

int main()
{
    int num;

    cout << "Enter a number: ";
    cin >> num;

    if (isPrime(num))
        cout << "Prime Number";
    else
        cout << "Not a Prime Number";

    return 0;
}

Sample Input

Enter a number: 29

Sample Output

Prime Number

Explanation

The function checks whether the number has any divisor other than 1 and itself. If none exists, it returns true.

Concepts Covered

  • Boolean Function
  • for Loop
  • Prime Number Logic
  • Return Statement

8. C++ Program to Calculate Factorial Using a Function

Problem Statement

Write a C++ program to calculate the factorial of a number using a function.

C++ Solution

#include <iostream>
using namespace std;

long long factorial(int number)
{
    long long fact = 1;

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

    return fact;
}

int main()
{
    int num;

    cout << "Enter a number: ";
    cin >> num;

    cout << "Factorial = "
         << factorial(num);

    return 0;
}

Sample Input

Enter a number: 6

Sample Output

Factorial = 720

Explanation

The factorial is calculated inside the function using a loop and returned to the main() function.

Concepts Covered

  • Function Returning Value
  • for Loop
  • Accumulator Variable
  • Factorial Logic

9. C++ Program to Check Whether a Year is a Leap Year Using a Function

Problem Statement

Write a C++ program to determine whether a year is a leap year using a function.

C++ Solution

#include <iostream>
using namespace std;

bool isLeapYear(int year)
{
    if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
        return true;

    return false;
}

int main()
{
    int year;

    cout << "Enter a year: ";
    cin >> year;

    if (isLeapYear(year))
        cout << "Leap Year";
    else
        cout << "Not a Leap Year";

    return 0;
}

Sample Input

Enter a year: 2024

Sample Output

Leap Year

Explanation

The leap year calculation is performed inside the function, making the program modular and reusable.

Concepts Covered

  • Boolean Function
  • Decision Making
  • Leap Year Logic
  • Function Reusability

10. C++ Program to Print the Multiplication Table Using a Function

Problem Statement

Write a C++ program to print the multiplication table of a given number using a function.

C++ Solution

#include <iostream>
using namespace std;

void multiplicationTable(int number)
{
    for (int i = 1; i <= 10; i++)
    {
        cout << number
             << " x "
             << i
             << " = "
             << number * i
             << endl;
    }
}

int main()
{
    int num;

    cout << "Enter a number: ";
    cin >> num;

    multiplicationTable(num);

    return 0;
}

Sample Input

Enter a number: 8

Sample Output

8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80

Explanation

The multiplication table logic is written inside a reusable function that can be called with any number.

Concepts Covered

  • Void Function
  • Loop Inside Function
  • Function Parameter
  • Reusable Code

11. C++ Program to Find 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.

Formula:

Area = π × r × r

Take π = 3.14159

C++ Solution

#include <iostream>
using namespace std;

double areaOfCircle(double radius)
{
    return 3.14159 * radius * radius;
}

int main()
{
    double radius;

    cout << "Enter radius: ";
    cin >> radius;

    cout << "Area = "
         << areaOfCircle(radius);

    return 0;
}

Sample Input

Enter radius: 7

Sample Output

Area = 153.938

Explanation

The radius is passed to the function, which calculates and returns the area using the mathematical formula.

Concepts Covered

  • Function Returning double
  • Mathematical Formula
  • Function Parameter
  • Return Statement

12. C++ Program to Swap Two Numbers Using a Function (Call by Value)

Problem Statement

Write a C++ program to swap two numbers using a function with call by value.

C++ Solution

#include <iostream>
using namespace std;

void swapNumbers(int a, int b)
{
    int temp;

    temp = a;
    a = b;
    b = temp;

    cout << "\nInside Function" << endl;
    cout << "First Number  = " << a << endl;
    cout << "Second Number = " << b << endl;
}

int main()
{
    int num1, num2;

    cout << "Enter first number: ";
    cin >> num1;

    cout << "Enter second number: ";
    cin >> num2;

    swapNumbers(num1, num2);

    cout << "\nInside Main Function" << endl;
    cout << "First Number  = " << num1 << endl;
    cout << "Second Number = " << num2 << endl;

    return 0;
}

Sample Input

Enter first number: 10
Enter second number: 20

Sample Output

Inside Function
First Number  = 20
Second Number = 10

Inside Main Function
First Number  = 10
Second Number = 20

Explanation

The values are swapped only inside the function because call by value creates copies of the original variables.

Concepts Covered

  • Call by Value
  • Function Parameters
  • Local Variables

13. C++ Program to Find the Maximum of Three Numbers Using a Function

Problem Statement

Write a C++ program to find the largest among three numbers using a function.

C++ Solution

#include <iostream>
using namespace std;

int maximum(int a, int b, int c)
{
    if (a >= b && a >= c)
        return a;
    else if (b >= a && b >= c)
        return b;
    else
        return c;
}

int main()
{
    int num1, num2, num3;

    cout << "Enter three numbers: ";
    cin >> num1 >> num2 >> num3;

    cout << "Largest Number = "
         << maximum(num1, num2, num3);

    return 0;
}

Sample Input

Enter three numbers: 65 98 42

Sample Output

Largest Number = 98

Explanation

The function compares all three numbers and returns the largest value.

Concepts Covered

  • Multiple Parameters
  • else-if Ladder
  • Return Value
  • Logical Operators

14. C++ Program to Find the Power of a Number Using a Function

Problem Statement

Write a C++ program to calculate base<sup>exponent</sup> using a user-defined function.

C++ Solution

#include <iostream>
using namespace std;

int power(int base, int exponent)
{
    int result = 1;

    for (int i = 1; i <= exponent; i++)
    {
        result *= base;
    }

    return result;
}

int main()
{
    int base, exponent;

    cout << "Enter base: ";
    cin >> base;

    cout << "Enter exponent: ";
    cin >> exponent;

    cout << "Result = "
         << power(base, exponent);

    return 0;
}

Sample Input

Enter base: 3
Enter exponent: 4

Sample Output

Result = 81

Explanation

The function repeatedly multiplies the base by itself until the required exponent is reached.

Concepts Covered

  • Loop Inside Function
  • Mathematical Calculation
  • Return Value

15. C++ Program to Find the Sum of an Array Using a Function

Problem Statement

Write a C++ program to calculate the sum of all elements in an array using a function.

C++ Solution

#include <iostream>
using namespace std;

int arraySum(int arr[], int size)
{
    int sum = 0;

    for (int i = 0; i < size; i++)
    {
        sum += arr[i];
    }

    return sum;
}

int main()
{
    int numbers[5];

    cout << "Enter 5 numbers:\n";

    for (int i = 0; i < 5; i++)
    {
        cin >> numbers[i];
    }

    cout << "Sum = "
         << arraySum(numbers, 5);

    return 0;
}

Sample Input

Enter 5 numbers:
10
20
30
40
50

Sample Output

Sum = 150

Explanation

The array and its size are passed to the function. The function iterates through each element, adds it to the accumulator variable sum, and returns the final result.

Concepts Covered

  • Arrays as Function Parameters
  • Loop Inside Function
  • Accumulator Variable
  • Function Returning Value

Chapter Summary

In this chapter, you learned how functions improve the structure, readability, and reusability of C++ programs. You practiced creating functions with and without parameters, returning values from functions, using Boolean functions, performing mathematical calculations, checking prime numbers, calculating factorials, generating multiplication tables, and working with arrays inside functions. Functions are one of the core concepts of C++ and are essential for writing modular and scalable programs.


Key Takeaways

  • Functions help divide large programs into smaller reusable blocks.
  • void functions perform tasks without returning values.
  • Functions can accept one or more parameters.
  • Functions can return different data types such as int, double, and bool.
  • Boolean functions are useful for decision-making.
  • Functions improve code readability and maintenance.
  • Arrays can be passed as function parameters.
  • Functions reduce code duplication.
  • Functions are widely used in software development and competitive programming.
  • Mastering functions is essential before learning recursion and object-oriented programming.

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 important?

Functions improve code organization, reduce duplication, simplify debugging, and make programs easier to maintain.


3. What is the difference between a function declaration and a function definition?

  • Declaration specifies the function name, return type, and parameters.
  • Definition contains the actual implementation of the function.

4. What is a void function?

A void function performs a task but does not return any value.


5. What is the purpose of the return statement?

The return statement sends a value back to the calling function and ends the execution of the current function.


6. Can a function return different data types?

Yes. Functions can return int, float, double, char, bool, or user-defined types depending on their return type.


7. What is the difference between call by value and call by reference?

  • Call by value passes a copy of the variable.
  • Call by reference passes the original variable, allowing the function to modify its value.

8. Why should beginners practice function-based programs?

Functions are a fundamental concept in C++. They are used in almost every real-world application and are essential for learning recursion, classes, object-oriented programming, and advanced algorithms.

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

Scroll to Top