functions in c++

Functions in C++ with Example | C++ Programming

In this tutorial, we will learn about functions in c++ and function expressions with the help of examples.

A function is a code unit that accomplishes a certain task.

Let’s say we need to write a programme that would draw a circle and colour it. To fix this problem, we can create two functions:

  • a function to draw the circle 
  • a function to colour the circle 

Our programme is easy to understand and reuse since it breaks down a big problem into smaller bits.

There are two different kinds of functions:

  • Functions of a Standard Library In C++, this is predefined.
  • A function that is defined by the user: User-defined.

We’ll concentrate on user-defined functions in this session.

C++ User-defined Function

The programmer can define their own functions in C++.

A user-defined function is a collection of code that performs a specified task and is given a name (identifier).

When the function is called from anywhere in the programme, it executes all of the codes defined in the function’s body.

C++ Function Declaration

The following is the syntax for declaring a function:

returnType functionName (parameter1, parameter2,...) {
    // function body   
}

An example of a function declaration is shown below.

// function declaration
void sample() {
    cout << "Hello World";
}

Here,

  • a sample is the name of the function ()
  • The function’s return type is void.
  • It has no arguments if the parentheses are empty.
  • The body of the function is written inside
Note: ReturnType and arguments will be covered later in this tutorial.

Calling a Function

We’ve declared a function called sample in the previous program (). We must first call the sample() function.

Here’s how we can call the above sample() function.

int main() {
     
    // calling a function   
    sample(); 

}

Example 1: Display a Text

#include <iostream>
using namespace std;

// declaring a function
void greet() {
    cout << "Hello there!";
}

int main() {

    // calling the function
    greet();

    return 0;
}

Output

Hello there !

Function Parameters

A function can be declared with parameters, as previously described (arguments). A parameter is a value that is supplied to a function when it is declared.

Take, for example, the following function:

void printNum(int num) {
    cout << num;
}

The function parameter is the int variable num.

While calling the function, we pass a value to the function parameter.

int main() {
    int n = 7;
    
    // calling the function
    // n is passed to the function as argument
    printNum(n);
    
    return 0;
}

Example 2: Function with Parameters

// program to print a text

#include <iostream>
using namespace std;

// display a number
void displayNum(int n1, float n2) {
    cout << "The int number is " << n1;
    cout << "The double number is " << n2;
}

int main() {
     
     int num1 = 5;
     double num2 = 5.5;

    // calling the function
    displayNum(num1, num2);

    return 0;
}

Output

The int number is 5
The double number is 5.5

We utilised a function with one int parameter and one double parameter in the above application.

Then, as parameters, we pass num1 and num2. The function arguments n1 and n2 are used to hold these values.

Note: The type of the arguments passed while calling the function must match with the corresponding parameters defined in the function declaration.

Return Statement

We utilised void in the function declarations in the previous programmes. As an example,

void displayNumber() {
    // code
}

This indicates that the function does not return a value.

It’s also possible to return a value from a function. For this, we need to specify the returntype of the function during function declaration.

Then, to return a value from a function, use the return statement.

For example,

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

Instead of the void, we have int. The function returns a value as a result of this.

The code return (a + b); returns the function value as the sum of the two parameters.

The end of the function is indicated by the return statement. Any code inside the function after the return is not executed.

Example 3: Add Two Numbers

// program to add two numbers using a function

#include <iostream>

using namespace std;

// declaring a function
int add(int a, int b) {
    return (a + b);
}

int main() {

    int sum;
    
    // calling the function and storing
    // the returned value in sum
    sum = add(100, 78);

    cout << "100 + 78 = " << sum << endl;

    return 0;
}

Output

100 + 78 = 178

The add() function is used to find the sum of two numbers in the above programme.

When calling the function, we pass two int literals, 100 and 78.

We print the function’s returned value after storing it in the variable sum.

Notice that sum is a variable of int type. This is because the return value of add ( ) is of int type.

Function Prototype

The function declaration code should be placed before the function call in C++. However, we must utilise the function prototype if we wish to define a function after the function call. As an example,

// function prototype
void add(int, int);

int main() {
    // calling the function before declaration.
    add(5, 3);
    return 0;
}

// function definition
void add(int a, int b) {
    cout << (a + b);
}

In the above code, the function prototype is:

void add(int, int);

This tells the compiler the name of the function and the parameters it has. Because of this, we can use the code to call a function before it has been defined.

The syntax of a function prototype is:

returnType functionName(dataType1, dataType2, ...);

Example 4: C++ Function Prototype

// using function definition after main() function
// function prototype is declared before main()

#include <iostream>

using namespace std;

// function prototype
int add(int, int);

int main() {
    int sum;

    // calling the function and storing
    // the returned value in sum
    sum = add(100, 78);

    cout << "100 + 78 = " << sum << endl;

    return 0;
}

// function definition
int add(int a, int b) {
    return (a + b);
}

Output

100 + 78 = 178

Example 3 is substantially identical to the previous programme. The only difference is that the function is defined after the call to the function.

That’s why, in this example, we’ve utilised a function prototype.

Benefits of Using User-Defined Functions

  • The use of functions allows the code to be reused. They can be declared once and used several times.
  • Each little task is split into a function, making the application easy to use.
  • The use of functions improves readability.

C++ Library Functions

In C++ programming, library functions are built-in functions.

  • Programmers can use library functions by directly invoking them; they are not required to write the functions themselves.
  • sqrt(), abs(), isdigit(), and other C++ library functions are examples.
  • We normally need to include the header file in which the library functions are defined in order to use them.
  • For example, we need to include the cmath header file in order to use mathematical functions like sqrt() and abs().

Example 5: C++ Program to Find the Square Root of a Number

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double number, squareRoot;
    
    number = 25.0;

    // sqrt() is a library function to calculate the square root
    squareRoot = sqrt(number);

    cout << "Square root of " << number << " = " << squareRoot;

    return 0;
}

Output

Square root of 25 = 5

The sqrt() library function is used to calculate the square root of an integer in this application.

The sqrt() function declaration is found in the cmath header file. To use the sqrt() function, we need to use the line #include <cmath>.

You may like:

C++ Goto statement with Example | C++ Programming

C++ Continue Statement with Example | C++ Programming

This Post Has 5 Comments

Leave a Reply