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.

using namespace std;

void codescracker(void);        // function prototype

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

   return 0;

void codescracker(void)        // function definition

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

Starting the execution of the 'codescracker()' function.
Execution of the 'codescracker()' 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.

using namespace std;

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

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

   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:

  • Using the following statement (before main())
    int sum(int x, int y);
    The prototype of the function named "sum()" is declared, where "int" before the name specifies that the function must return an "int" type value. Whereas "int x" and "int y" are the two parameters, this indicates that the function takes two parameters of the "int" type.
  • Now inside the "main(),"  using the statement
    res = sum(a, b);
    The function "sum()" is called or invoked by passing "a" and "b" as its parameter values. Now let's come to the function definition part.
  • The values of "a" and "b" are copied to "x" and "y,"  and "x+y" will be calculated and initialized to the "add" variable.
  • And using the "return" statement, the value of "add" is padded to the calling function.
  • Therefore, the statement "res = sum(a, b)" becomes "res = add,"  which will be the value of "a + b" in the end. Therefore, I just printed the value of "res" on the output.

The above program can also be written as:

using namespace std;
int sum(int, int);
int main()
   int a, b;
   cout<<"Enter two numbers: ";
   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++:

  • local scope: A name that is declared in a block (i.e., "{...}") is considered to be local to that block and can only be used within that block and the blocks that are contained under it.
  • function scope: The variables that are declared in the last block of a function have function scope, which means that they can only be accessed within the function that declares them. This is also known as local scope. Additionally, labels (of goto) have function scope, which means that they cannot be used in any context outside of the function itself.
  • file scope: A name that is declared outside of all blocks and functions has file scope, which means that it can be used in all of the blocks and functions that are written inside the file that contains the name declaration.
  • class scope: A name of a class member has the scope of the class and is only used within that class.

C++ Quiz

« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook

Subscribe Us on YouTube