C++ Functions

This post was written and published in order to describe "C++ functions." The list of sub-topics that will be covered under the subject "C++ functions" is as follows.

  1. What are functions?
  2. Why should we use functions in our program?
  3. Function prototype
  4. Function definition
  5. Function calling
  6. The return statement
  7. Types of functions
  8. Friend function

The first, second, and sixth topics are covered in this post, while the remaining topics are covered in a separate post, beginning with the "function prototype," which is available in the post that follows this one.

What are functions?

A function is a sub-program that performs an operation on data and typically returns a value. Because it is difficult to manage a single list of instructions, large programs are generally avoided. Thus, a large program is broken down into smaller units known as "functions." A function is a named unit of a group of program statements. This unit can be invoked from other parts of the program.

Why should we use functions in our program?

The primary advantage of utilizing functions is that they simplify the management of programs by ensuring that only a limited portion of the code is processed at any given time, which eliminates any possibility of ambiguity. Another reason to use functions is to reduce program size. Functions make a program more readable and understandable to a programmer, thereby making program management much easier.

Now, before reading the content after this paragraph, I recommend that you read the "function prototype," "function definition," and "function calling" sections first.

C++: The return statement

As important as invoking a function is, returning from a function is equally important, as it not only terminates the function's execution but also passes control back to the calling function. A function terminates when either a return statement is encountered or the last statement in the function is executed. Generally, a return statement is used to terminate a function, whether or not it returns a value.

The return statement is useful in two ways. First, an immediate exit from the function is caused as soon as a return statement is encountered, and control passes back to the operating system, which is main's caller. The second use of the return statement is that it is used to return a value to the calling code.

Even though it is not necessary to have a return statement in a function, most functions rely on the return statement to stop the execution, either because a value must be returned or to make the function's code simpler and more efficient.

A function may contain several return statements. However, only one of them gets executed because the execution of the function terminates as soon as a return is encountered.

Now let me take an example program that shows how the "return" statement stops the execution of the function and transfers the controls to the calling function.

#include<iostream>
using namespace std;

void fresherearth(void);        // function prototype

int main()
{
   cout<<"Starting the execution of the 'fresherearth()' function.\n";
   fresherearth();             // function calling
   cout<<"\nExecution of the 'fresherearth()' function has been ended.\n";

   return 0;
}

void fresherearth(void)        // function definition
{
   cout<<"xyz";
   return;
   cout<<"abc";
}

The output produced by the above C++ program should be:

Starting the execution of the 'fresherearth()' function.
xyz
Execution of the 'fresherearth()' function has been ended.

As you can see from the output, there is another statement that prints "abc" on the output that was not executed because before the statement, I had written the "return" statement, which transferred control to the calling function.

Let me give you another example to help you understand the "return" statement practically.

#include<iostream>
using namespace std;

int sum(int x, int y);        // function prototype

int main()
{
   int a, b, res;
   cout<<"Enter two numbers: ";
   cin>>a>>b;
   res = sum(a, b);          // function calling
   cout<<"\nResult = "<<res;
   cout<<endl;

   return 0;
}

int sum(int x, int y)        // function definition
{
   int add;
   add = x+y;
   return add;
}

The following snapshot shows the initial output produced by this example:

c++ return statement example

Now type any number, say 10, and then hit the ENTER key. Again,  type another number, say 50, and hit the ENTER key again to find the addition and print the result of the two entered numbers as shown in the snapshot given below:

c++ return statement program

Let me explain the above program step by step. While explaining the above program, I will only explain the main portion:

The above program can also be written as:

#include<iostream>
using namespace std;
int sum(int, int);
int main()
{
   int a, b;
   cout<<"Enter two numbers: ";
   cin>>a>>b;
   cout<<"\nResult = "<<sum(a, b)<<endl;
   return 0;
}
int sum(int x, int y)
{
   return (x+y);
}

This program does the same job as the previous program.

C++ Scope Rules

The program part(s) in which a particular piece of code or a data value (e.g., a variable) can be accessed is known as the piece of code's or variable's scope.

The scope rules of a language are the rules that specify in which portions of the program a particular chunk of code or data item is known and can be accessed. These rules can also be thought of as the rules that define the boundaries of a language.

There are four kinds of scopes in C++:

C++ Quiz


« Previous Tutorial Next Tutorial »