A function is a block of code that performs a certain task and provides the output. It is mainly used to eliminate repetitive code. In this tutorial, we will look into the friend function in C and explain its concept with working examples.

What is Friend Function?

Friend function is a non-member function that can access the private and protected members of a class. “Friend” is a keyword used to indicate that a function is the friend of a class. This allows the compiler to know that the particular function is a friend of the given class. The friend function then should be able to access the private and protected member of a given class. Without the friend keyword, a non-member outside function can only access the public members of a class.

Key Features of Friend Function:

Here are the key features of the friend function:

  1. A friend function is not in the scope of the class.
  2. The friend function cannot be invoked using an instance of a class.
  3. It can access the members using the object and dot operator.

Syntax of Friend Function:

Here is the syntax of the friend function:

class Class_Name_Demo

{


   ………………………………………


   ………………………………………

    friend return_Type function_Name(arg_1, arg_2,);

};

Example of Friend Function:

Now, let us look into an example program to understand the concept of the friend function in C . In the below example program, we have the “Friend_Demo” class. It has three different types of data members, i.e., private, protected, and public.

We have defined another function, i.e., “friendDemo_Func()” outside the scope of the “Friend_Demo” class and tried to access the members (private, protected, and public) of the “Friend_Demo” class.

But, as you can see in the output below when we compile the program, it throws compilation errors. The friend function is going to exactly solve this problem.

#include


using namespace std;

class Friend_Demo

{


   private:


    int i_private;


   protected:


    int i_protected;


   public:


    int i_public;

};

void friendDemo_Func()

{


    Friend_Demo fd;


   


    fd.i_private    = 10;


    fd.i_protected  = 15;


    fd.i_public     = 20;


   


    cout << fd.i_private << endl;


    cout << fd.i_protected << endl;


    cout << fd.i_public << endl;

}

int main()

{


    friendDemo_Func();

    return 0;

}

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-126.png" data-lazy- height="492" src="data:image/svg xml,” width=”833″>

In the previous program, we were getting compilation errors while trying to access the private, protected, and public members of a class from a non-member function. This is because a non-member function is not allowed to access the private and protected members of a class from outside the scope of the class.

Now, in this example, we have declared “friendDemo_Func()” function as a friend inside the scope of the class, i.e., “Friend_Demo”:

friend void friendDemo_Func();

We have created an object, i.e., “fd” of the “Friend_Demo” class inside the “friendDemo_Func()” function. Now, we can access the private, protected, and public members of the “Friend_Demo” class using the dot operator. We have assigned 10, 15, and 20 to i_private, i_protected, and i_public, respectively.

As you can see in the output below, this program is now compiled and executed without any errors and print the output as expected.

#include


using namespace std;

class Friend_Demo

{


   private:


    int i_private;


   protected:


    int i_protected;


   public:


    int i_public;


    friend void friendDemo_Func();

};

void friendDemo_Func()

{


    Friend_Demo fd;


   


    fd.i_private    = 10;


    fd.i_protected  = 15;


    fd.i_public = 20;


   


    cout << fd.i_private << endl;


    cout << fd.i_protected << endl;


    cout << fd.i_public << endl;

}

int main()

{


    friendDemo_Func();

    return 0;

}

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-127.png" data-lazy- height="251" src="data:image/svg xml,” width=”560″>

Conclusion:

In this article, I have explained the concept of the friend function in C . I have also shown two working examples to explain how the friend function behaves in C . Sometimes, the friend function can be very useful in a complex programming environment. However, a programmer should be cautious about overusing it and compromising its OOP features.

About the author

<img alt="Sukumar Paul" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/Sukumar-Paul-150×150.jpeg" height="112" src="data:image/svg xml,” width=”112″>

Sukumar Paul

I am a passionate software engineer and blogger. I have done my Masters in Software Engineering from BITS PILANI University, India. I have very good experience in real-time software development and testing using C, C , and Python. Follow me at thecloudstrap.com.