What is the Difference Between Friend Function and Normal Function in C++

The main difference between Friend Function and Normal Function in C++ is that Friend function helps to access private and protected data while Normal Function is a group of statements that performs a specific task.

C++ is a programming language developed by Bjarne Stroustrup in 1979. This language is similar to the C language, but it has additional features than C. One major programming concept in C++ is functions. C++ has a special function called friend function which is different from a normal function.

Key Areas Covered

1. What is Friend Function in C++
     – Definition, Functionality
2. What is Normal Function in C++
     – Definition, Functionality
3. What is the Difference Between Friend Function and Normal Function in C++
    – Comparison of Key Differences

Key Terms

C, C++, Friend Function, Normal Function

Difference Between Friend Function and Normal Function in C++ - Comparison Summary

What is Friend Function in C++

There can be private and protected data members in a class. It is not possible to access the private data members outside the class. We can access the protected members within the class and by its subclasses. Therefore, these two access modifiers restrict accessing the data members. Moreover, we can access private and protected data members of a class using a friend function. The “friend” keyword declares a function as a friend function.

Difference Between Friend Function and Normal Function in C++

Figure 1: C++ Program with Friend Function

In the above program, Calculate class contains a private data member called number. The programmer has to declare the friend function inside the class as in line 10.  Then calculate constructor initializes the number to 0.  The printNumber is the friend function. In the main method, c is an object of Calculate. We can pass that object to the printNumber method. As printNumber is a friend function, it can access the private member ‘number’ and modify the value. Therefore, it accesses that value and adds 20 to it and returns the result. Finally, the result is displayed on the console.

What is Normal Function in C++

A normal function is a set of instructions that perform a specific task. Rather than writing all instructions in a single method, we can divide the program into multiple functions and call them in the main method as required.  Therefore, normal functions increase code reusability and improve code maintainability.

There are two types of functions as library functions and user-defined functions. C++ provides library functions. These are declared in the C++ header files. Some examples are sin(x), sqrt(x), etc. The programmer can use them directly in the program.  Furthermore, the programmer can write his own functions. These are called user-defined functions.

Main Difference - Friend Function vs Normal Function in C++

Figure 2: C++ Program with Normal Function

In the above program, x and y are two integer values in the main method. The findSum is a method, and we can pass x and y values to it. It calculates the sum and returns the result. The result is stored in the sum variable. Similarly, there is a function called findMultiply. We can pass x and y values to it. It calculates the multiplication and returns the result. Likewise, these two functions perform the tasks and return the answers back to the main method.

Difference Between Friend Function and Normal Function in C++

Definition

Friend function in C++ is a method that gives access to private and protected data while normal function in C++ is a method to provide modularity to a program. This is the main difference between Friend Function and Normal Function in C++.

Usage

Another difference between Friend Function and Normal Function in C++ is their usage. Friend function helps to modify the private and protected data members of a class whereas normal function improves code reusability and makes the code maintainable.

Conclusion

The main difference between Friend Function and Normal Function in C++ is that Friend function helps to access private and protected data while Normal Function is a group of statements that performs a specific task. In brief, friend function is a type of function.

Reference:

1. “C Friend Function – Javatpoint.” Www.javatpoint.com, Available here.
2. “C Functions – Javatpoint.” Www.javatpoint.com, Available here.
3. “Functions in C .” Types of Network Topology in Computer Networks | Studytonight, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply