More about functions in C++

Video Tutorial
FREE
Functions thumbnail
This video belongs to
C++ Course: Learn the Essentials
14 modules
Certificate
Topics Covered

Overview

The functions in C++ are used to reduce code redundancy and memory consumption. Some features of functions include making a function inline, where one-liner code inside a function is processed in an optimized manner, pass by reference where the alias of variable is passed as a parameter, function overloading where a function with the same name and a different number or type of arguments. There are function templates in C++ that can be used for writing special code for different types of arguments to enhance the reusability of the code.

Inline functions

The study of functions start with the first topic, an inline function. Let's understand it more in-depth about it.

What is a function, and why do we need an inline function ?

A normal function is both to reduce code redundancy and reduce memory consumption. A lot happens when a function is called, such as matching arguments, matching returns, and passing control from calling to the definition and vice versa. But it appears to be time-consuming when the function definitions consist of hardly a single or two simple statements.

For this reason, C++ has the concept of inline functions.

What is inline function ?

The inline function is a C++ optimization meant to speed up programs. The inline functions are initialized the same way as normal functions, but a keyword inline is added before them. A keyword inline requests the compiler instead of giving a command to make a function inline. For example, consider a classroom where you must request your teacher's permission to enter the classroom and your teacher has the final say on whether you can enter the classroom or not. Similarly, the compiler decides whether to treat inline functions as inline or not. If the compiler treats a function as an inline function, it substitutes the code of the function in a single line that is inline. Function bodies are replicated as function-calling places. Due to this replication, the time it takes to transfer control from the call to the definition is reduced.

However, the inline function comes with a few restrictions, such as no more than one argument, control statements, or other complex logic like recursion are not allowed. A function can be made inline by adding the keyword inline before it.

For inline functions, the syntax is:

Syntax for inline function :

Here are some C++ programs that illustrate inline functions.

Program : A C++ code that finds the greatest number among the two.

Code :

Output :

Explanation:

In the above C++ program, a function named fun is defined and subsequently initialized as an inline function with the keyword inline. The fun() contains the logic to find the greatest of two numbers written in one instruction, which states that if x is greater than y, then return x; otherwise, return y. We now come to the main function where the fun() is called. Here, two integers are passed, among which the greatest will be printed as output.

Reference Variables

What is a reference variable ?

Variables that provide an alternative name (alias) to an existing or previously defined variable are called reference variables. By including '&'(Ampersand) in the declaration, a variable can be declared as a reference variable.

How to create a reference variable ? The syntax to create a reference variable is as follows :

Syntax :

Here, the variable is declared a standard variable, whereas the reference_name variable is declared using &, which points to a normal variable.

How do reference variables work ?

How do reference variables work

Here is how variables in the above diagram are declared in a C++ program:

Let's say that variable a is declared as an integer and stores the value 15. The memory address will be assigned to variable a where the value 15 is stored. Now when we declare another variable b as a reference variable to a so, instead of allocating another memory area, the variable b will be pointing to the memory address of a.

There are a few properties/rules for using reference variables :

  • An initialization must take place when the variable is declared.
  • It is impossible to change the reference of a reference variable. If you do that, you get a compile-time error.

Example :

  • Changes in the value of the reference variable will affect the value of the variable it refers to as they share the same memory address.

Here is a simple C++ program to understand how reference variables work :

Program : A simple C++ code to understand reference variables.

Code :

Output:

Explanation:

In the above C++ program, we have declared a normal variable named a, assigning value 15 to it, and created a reference variable b, which refers to the memory address of a. To check how reference variables work, we first print the value of the variable a, which is 15. We then assign the value 20 to the reference variable b, and we know it will be equal to 20 after printing it. This time, however, we again print variable a, which is 20. As a result, a and b have the same memory location on the computer, and changes to the reference variable will impact the variables they refer to.

How to Pass Function Arguments by Reference?

What is passing function arguments by reference ?

Function arguments passed by reference are also known as pass by address or call by reference. While calling a function, instead of passing a variable holding a value to a function, we pass a reference of that variable to them so that changes in the value of the reference will also affect the original variable and vice versa.

What happens when arguments are passed by reference ?

When the reference variable is passed as an argument in the function, the formal parameter becomes a reference to the argument passed by reference. During its execution, the function accesses the argument's original value.

Here is a simple C++ program to understand arguments being passed by reference.

Program : C++ code for swapping two values using reference variables.

code :

Output :

Explanation: pass function arguments by reference

For the C++ program above, the actual and the reference variables point to the same memory address when they are passed as parameters to the function named "passByRef". There is no difference between a and x because both are aliases of the same memory address, and the same applies to b and y. Because of this, each change we make in the passByRef function will be applied to the actual variable. As you probably noticed in the function definition, we use the * operator before the variables, which then are named as dereferencing variables so that we can access values stored in the given memory location. This means the values stored as a=5 and b=15 can be accessed using *x and *y, respectively. Therefore *x=5 and *y=15.

Function Overloading

What is function overloading ? Function overloading can be considered a part of the polymorphism in C++. C++ allows functions to have the same name if there are different numbers of arguments and/or types. These functions, which have the same name and different arguments are termed overloaded functions.

In the code below, all the functions are named Fun1, but they have different sets of arguments. Here, all the functions are overloaded.

In function overloading, the return type may or may not be kept the same for two overloaded functions, but if it has the same return type, it should not have the same number of arguments, as it can cause the compiler to throw an error. The example for the same is given below.

Let's look at an example of function overloading, which has the same number of parameters with different return types.

Program : A C++ code to calculate an absolute value of a number.

  • For the same number of function parameters having different types-

Code :

Output :

Explanation:

In the above C++ program, we are calculating the absolute value of a number. Here we have two functions, both named Fun1(). The first Fun1 has return type int, which executes when it encounters the integer input. The second Fun1 has a return type float which executes when it encounters the float input. Hence the C++ overloads the function.

Let's look at another example that has different numbers and types of function parameters.

Program : A simple C++ code to print the value of a number according to its return type.

  • For different number of function parameters having the same or different types

Code :

Output :

Explanation:

As you can see in the above C++ program, there are three functions named Fun1() with different types of parameters. There is only one parameter of type int in the first Fun1(). Second Fun1() has a single parameter of type float, while third Fun1() has two parameters of types int and float, respectively. Now we declare two variables in the main() function that have the types int(x) and float(y), and we call the functions Fun1(x), Fun1(y), and Fun1(x,y) so that depending on the type and number of parameters, the appropriate function gets called and takes the input values to get the expected output.

Function Templates

What are function templates, and where can they be used ?

For example, a Student got a task to calculate the absolute value of any number. An absolute value of a number is the same number if the number is positive. However, if the number is negative, then the absolute value of the number is that number with a different sign. But while taking the different numbers as inputs, the datatype of the inputs can differ; for example, a student can get 20(which is int), 30.5(which is float), -33.45(which is double). So, we need to define a function for each type of parameter we get as input. So, instead of maintaining different codes with a different type of parameter, the student can write a single function template that he can use again and again to calculate the absolute number. With template functions, it does not matter what type of data we input. It will internally make a function with parameters of the input type.

So, the 'function templates' can be used instead of writing different code for different datatype parameters in the function.

How to define and call the function template?

The function template is defined with the keyword template, which includes the function parameters written inside angle brackets<>. Below the template definition, the function is defined.

Definition :

Here, the T represents the template argument that accepts different types of datatypes, whereas the typename keyword represents the different types of datatype like int, float, etc.

Each time an argument of any type is passed inside functionName(), the new kind of functionName() is generated internally for that respected datatype.

Calling :

The function call happens inside the main() function by using the syntax below :

How do function templates work ? How function templates work

A function template is a block of code that expands at the compilation time. When the function is called, the type of parameters is checked by the compiler, according to which compiler internally generates the code according to the different types of parameters and adds the result to the output.

Let's consider an example to understand this in a better way :

Program : A program to use C++ function templates for calculating the average marks.

Code :

Output:

Explanation:

In the above C++ program, we have created a function template with a return type float named AVGfunc() where we simply calculate the average of two numbers. When we pass parameters with different data types in the function calling the typename in the function template helps to initialize the variable with that type internally.

Function Template Specializations

Why do we need function template specializations?

We know that the function templates are used instead of writing different code for different datatype parameters in a function. But what if we need to write special code for a particular datatype so that each time that datatype is passed, the program control will switch to the specialized template, and it should execute as a function template? This process in C++ is called template specializations. This can be used when we need special combinations of datatype parameters.

How to use function template specializations ?

The function specialization is defined the same as normal function templates, but instead of template arguments, we directly pass the arguments with the data type name as shown in the syntax below for a special template. While calling the function when the datatype in arguments is encountered, which is present in a special template, it directly executes the special template written for it.

Syntax :

Here is a C++ code to understand template specialization in a better way :

Program : A simple C++ program to illustrate template specializations

Code:

Output :

Explanation:

In the above C++ program, we have one normal function template where we just print the variable's value. In addition, we have a specialized template for char type variables that do the same thing, but the difference is that the program control is shifted to the specialized template when the value is of the char type.

Conclusion

So, let's conclude what we have learned about functions. Given below are the takeaways from this article:

  • The inline function is a C++ optimization meant to speed up programs. They are defined using the inline keyword.
  • The reference variable in C++ is the alias given to the variable by including & in the declaration, a variable can be declared as a reference variable.
  • We can pass reference arguments in C++ functions. The ‘&’ symbol is used in the function argument to define them.
  • C++ also supports function overloading, allowing functions to have the same name if there are different numbers of arguments and/or types.
  • To reduce the re-writing of code, again and again, C++ supports function templates. So, the function templates are used instead of writing different codes for different datatypes in the function.
  • Special function template in C++ specializes functions for different datatypes where we can specialize the code so that when we pass that particular datatype in a function call, it triggers this special code.