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

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

A function is a group of statements that together perform a task. Every C++ program has at least one function, which is main(), and all the most trivial programs can define additional functions.

You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division usually is such that each function performs a specific task.

A function declaration tells the compiler about a function’s name, return type, and parameters. A function definition provides the actual body of the function.

Dividing a complex problem into smaller chunks makes our program easy to understand and reusable.

Syntax:

// C++ Program to demonstrate working of a function
#include <iostream>    //header file
using namespace std;

// Following function that takes two parameters 'a' and 'b'
// as input and returns max of two input numbers
int max(int a, int b)
{
	if (a > b)
		return a;
	else
		return b;
}

// main function that doesn't receive any parameter and
// returns integer
int main()
{
	int a = 30, b = 40; //input a and b

	// Calling above function to find max of 'a' and 'b'
	int ma = max(a, b);  //find the max element

	cout << "max is: " << ma;   //max element print
	return 0;
}

Output:

max is: 40

There are two types of function:

  1. Standard Library Functions: Predefined in C++
  2. User-defined Function: Created by users

C++ User-defined Function:

C++ allows the programmer to define their own function. A user-defined function groups code to perform a specific task and that group of code is given a name (identifier). User Defined functions are customer-defined blocks of code specially customized to reduce the complexity of a big program help to run the code. They are also commonly known as “tailor-made functions” which are built only to satisfy the condition reducing the complexity.

Standard Library Functions:

Standard Library Functions are also called “builtin Functions“. These functions are a part of a compiler package that is already defined and consists of a special function with special and different meanings.

Each function here performs a specific operation. We can use this Standard Library Functions to get the pre-defined output. All C++ standard library functions are declared by using many header files. These library functions are created at the time of designing the compilers.

Defining a Function

return_type function_name(parameter1, parameter2,...) {
   //body of the function
}

Calling a Function

In the above program, we have declared a function named cfa(). To use the cfa() function, we need to call it.

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

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

}
How Function works in C++

A C++ function definition consists of a function header and a function body. Here are all the parts of a function −

  • Return Type − A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.
  • Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature.
  • Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.
  • Function Body − The function body contains a collection of statements that define what the function does.

Advantage of functions in C++

There are many advantages of functions.

1) Code Reusability:

By creating functions in C++, you can call it many times. So we don’t need to write the same code again and again.

2) Code optimization:

It makes the code optimized, we don’t need to write much code.

Suppose, you have to check 4 numbers (222, 101, 123 and 191) whether it is prime number or not. Without using function, you need to write the prime number logic 4 times. So, there is repetition of code.

But if you use functions, you need to write the logic only once and you can reuse it several times. Therefore the code reducing the complexity.

Return Statement

In the programs, we have used void in the function declaration.

For example,

void print_Number() {
    // code
}

This means the function is not returning any 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, the return statement can be used to return a value from a function.

For example,

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

Here, we have the data type int instead of void. Thats means that the function returns an int value. The code return (a + b); returns the sum of the two parameters of the function.

Example 1: Print a Text

#include <iostream>
using namespace std;

// declaring a function
void cfa() {
    cout << "Hello Bro!";
}

int main() {

    // calling the function
    cfa();

    return 0;
}

Output:

Hello Bro!

Example 2: Function with Parameters

#include <iostream>

using namespace std;

// display a number and a character
void cfa(int n1, char n2) {
    cout << "The int number is " << n1<<endl;
    cout << "The character  is " << n2<<endl;
}

int main() {
     
     int num1 = 5;
     char num2 = 'C';

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

    return 0;
}

Output:

The int number is 5
The character is C

Example 3: Add two numbers

// program to add two numbers using a function

#include <iostream>   //header file

using namespace std;

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

int main() {

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

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

    return 0;
}

Output:

100 + 20 = 120
Working of C++ Function with return statement

In the above program, the addition() the function is used to find the sum of two numbers.

To master Functions in C++ in the most efficient and effective way, do check out this:

This article is contributed by : Sayan Dutta to this article on csforall.in

Leave a Comment