Mastering Functions in C++ with Easy Examples

Evilox
Written by Evilox on
Mastering Functions in C++ with Easy Examples

Hey Guys, Welcome to this blog.
Today we’re going to talk about functions in C++:

  1. What are functions?
  2. How do they work?
  3. How do you use them to solve problems in C++?

What is a Function in C++?

A function is a term that comes from mathematics.

For example:

f(x) = x^2 + 2

Here x = 2;

f(2) = 2^2 + 2 = 4 + 2 = 6

Here, x is a parameter. You get the value of x from the user, and the function computes the result based on the value.
In programming, it’s similar—but not all functions need to compute a value. Some just perform actions without returning anything.


Example: Print a Message Using a Function

#include <iostream>
using namespace std;

void printhappybirthday(void)
{
    cout << "Happy birthday evilox" << endl;  // just prints without returning anything
}

int main(void)
{
    printhappybirthday(); // calling the function
}

The above function doesn’t compute a value, it just prints a birthday message.
In C++, a function is a series of statements grouped together and given a name.

C++ example showing a simple function called printhappybirthday that prints a message without returning a value.


Why Use Functions in C++?

  • To reuse code, so you don’t write the same code again and again.
  • To break big programs into smaller, manageable parts.

Types of Functions

There are two types of functions:

  1. Standard Library Functions:
    • Built-in and pre-defined functions provided by C++.
    • Examples:
      • sqrt()
      • strlen()
      • sizeof()
      • tolower()
      • toupper()
  2. User-Defined Functions:
    • Custom functions created by programmers to solve specific problems.
    • Help organize code and make it reusable.

Components to build a custom function:

  • return type
  • function name
  • parameters (optional)
  • function body
  • return statement

How to Write a Function in C++

return-type function_name(parameters)
{
    // function body
}
  1. return-type: The type of data the function will return at the end.
    • Common return types:
      • Integer types: short, int, long
      • Floating types: float, double
      • Character: char
      • Boolean: bool
      • String: string
  2. function_name: The name given to the function. Should be unique and descriptive.
  3. parameters: Dummy variables that help take input (optional).
  4. function body: Series of statements and declarations used inside the function.

Rules for Function Return Type

  • You cannot return an array from a function, but there are no other restrictions.
  • Specifying the return type as void means you cannot return anything.

Writing a Function in C++

#include <iostream>
using namespace std;

int add(int a, int b)  // accepting a and b as parameters
{
    return a + b; // returning the integer
}

int main(void)
{
    int addnum = add(12, 24); // calling the function and storing the result
    cout << addnum << endl;
}

C++ code showing a function named add that takes two integers, adds them, and returns the result.

In the above example:

  • return type: int
  • function name: add
  • parameters: a and b with their types
  • returns the integer a + b as the result

Solving a Real Problem: Factorial Using a Function

Find the Factorial of a Given Number

First, understand what a factorial is.
In mathematics, a factorial is the product of all integers less than or equal to a non-negative integer (the symbol is !).

For example:
5! = 5 × 4 × 3 × 2 × 1 = 120

So, we can clearly see that numbers loop from 1 to n.

Algorithm

  1. Get the input from the user (i.e. n > 1).
  2. Create a variable to store the product (i.e. factorial = 1).
  3. Loop from 1 to n and multiply the number, storing the result in factorial.
  4. Return the value at the end (i.e. return factorial).
#include <iostream>
using namespace std;

int find_factorial(int n)
{
    if (n < 1)
    {
        cout << "Invalid number" << endl;
        return -1;
    }

    int factorial = 1;
    for (int i = 1; i <= n; i++)
    {
        factorial *= i;
    }
    return factorial;
}

int main(void)
{
    int num = 0;
    cout << "Enter a number: ";
    cin >> num;
    cout << find_factorial(num) << endl;
    return 0;
}

C++ code defining a function to calculate the factorial of a number using a for loop, with input validation and return value.


How Does This Function Work?

In the main function, you get input from the user using cin and store the value in the num variable. Then you pass num to the function named find_factorial.

Inside that function:

  • It first checks whether n > 1. If not, it prints invalid number and stops the function.
  • Then it calculates the factorial using a for loop and stores the result in the factorial variable.
  • Finally, it returns the factorial at the end of the function.

C++ Functions Consist of Three Parts

  1. Function Declaration:
    Define the function before using it in your program. This tells the compiler about the function name, return type, and parameters. (Required if writing the function below main). This is called a function prototype.

     #include <iostream>
     using namespace std;
    
     int find_factorial(int n); // Function Declaration
    
     int main(void)
     {
         // main function
         return 0;
     }
    
     int find_factorial(int n)
     {
         // write the program for factorial
     }
    
  2. Function Definition:
    The body of the function or your actual code.

     int find_factorial(int n)
     {
         // Function Definition
     }
    
  3. Function Call:
    This is when you use or invoke the function in your main program.

     #include <iostream>
     using namespace std;
    
     int find_factorial(int n);
    
     int main(void)
     {
         int num = 0;
         cout << "Enter a number: " << endl;
         cin >> num;
         find_factorial(num); // Function Call
         return 0;
     }
    
     int find_factorial(int n)
     {
         // write the program for factorial
     }
    

Function Parameters and Arguments in C++

  • Parameters are dummy variables that hold the value passed by the arguments.
  • Arguments are the actual values you give when calling the function.
  • Basically, parameters receive the data and arguments send the data.

Default Parameters

Default parameters are used when no value is passed during the function call.
If you don’t provide an argument while calling the function, the default parameter is used automatically.

#include <iostream>
using namespace std;

int find_factorial(int n = 2); // default parameter

int main(void)
{
    int num = 0;
    cout << "Enter a number: ";
    cin >> num;
    cout << find_factorial() << endl;
    return 0;
}

int find_factorial(int n)
{
    // factorial program
}

Rules for Arguments

  1. Implicit Conversion:
    If you define the parameter as one type (e.g., integer) but pass a different type (e.g., floating point), C++ automatically converts one type to another (e.g., float to int, losing the decimal part).

     #include <iostream>
     using namespace std;
    
     int add(int a, int b);
    
     int main(void)
     {
         float i = 12.3;
         float j = 13.2;
         cout << add(i, j) << endl;
         return 0;
     }
    
     int add(int a, int b)
     {
         return a + b;
     }
    

    C++ program demonstrating implicit type conversion where float values are passed to a function expecting int parameters

  2. Default Argument Promotion:
    When passing small datatype values, they get automatically converted:

    • float is promoted to double
    • char and short are promoted to int
     int getNum(int a) // implicit type conversion (default promotion)
     {
         return a; 
     }
    
     int main(void)
     {
         char a = 'A';
         cout << getNum(a) << endl; // implicit type conversion (default promotion)
         return 0;
     }
    

Function Overloading in C++

Function Overloading is a language feature provided by C++.
It allows you to define multiple functions with the same name but different parameter sizes or types.

To overload a function, you need to follow two steps:

  1. Set different parameters for the function (multiple dummy variables).

     int add(int a, int b) // 2 parameters
     {
         return a + b;
     }
    
     int add(int a, int b, int c) // 3 parameters
     {
         return a + b + c;
     }
    
  2. Set different datatypes or sizes of datatypes for the parameters.

     int add(int a, int b)
     {
         return a + b;
     }
    
     float add(float a, float b)
     {
         return a + b;
     }
    

Use Case: Area Calculation Using Function Overloading

Find the area of the rectangle and square.

Rectangle and square image for the algorithm C++

Since these concepts are already covered in school, I won’t explain them in detail here.

#include <iostream>
using namespace std;

// Area of rectangle
int area(int l, int b)
{
    return l * b;
}

// Area of square
float area(int l)
{
    return l * l;
}

int main(void)
{
    int l = 12;
    int b = 7;
    cout << area(l) << endl;
    cout << area(l, b) << endl;
    return 0;
}

Rectangle and square image for the algorithm C++


Final Thoughts on C++ Functions

So, I hope you understand how functions in C++ work.
We explored what functions are, how to write them, types of functions, function overloading, and solving problems using functions in C++.

Functions help you reuse code, organize logic, and make programs more readable.


Enjoyed this article?
Support the content by buying a coffee ☕


Happy coding! 💻

Evilox

Evilox

A curious programmer exploring programming, problem solving, and code design. This blog is a place to learn, discover, and share ideas for those who love to grow.

Comments

comments powered by Disqus