Function Arguments in C++

Hello learners, today we are going to learn a very interesting and important concept that is function arguments in C ++. This particular concept is used in all the programming languages not only in C++. If you are clear with this concept, I am sure that you will like to code the programs on your own.

Types of function arguments in C++

Basically function arguments in any programming language is classified into three categories:

  • Call by value
  • Call by reference
  • Call by pointer

In detail, we will discuss all three categories with the help of a code.

Call by value

In this method, the actual values will be passed to the function but the values when it gets printed the values will not be the same it will get changed.

In the code, the value of arguments doesn’t change they remain the same. Just they are going to pass the value of a and b to the function and do the multiplication operation of those values.

code:

#include<iostream>
using namespace std;
class multi
{
private:
int c;
public:
void multiply(int x,int y)
{
  x=x+2;
  y=y+4;
  c=x*y;
  cout<<"multiplication is:"<<c<<endl;
}
};
int main()
{
 int a=2,b=3;
 multi obj;
 obj.multiply(a,b);
 cout<<"a="<<a<<endl<<"b="<<b;
 return 0;
}

Output:

     multiplication is: 28     a=2
     b=3

Call by reference

Call by reference value passes the actual values, to the function by referencing the variable. In this case, if you pass the values outside the function the values remain the same it will not change. But in the case of call by value the value of the variable gets change.

Code:

#include<iostream>
using namespace std;
class multi
{
private:
int c;
public:
void multiply(int &x,int &y)
{
  x=x+2;
  y=y+4;
  c=x*y;
  cout<<"Multiplication:"<<c<<endl;
}
};
int main()
{
 int a=2,b=3;
 multi obj;
 obj.multiply(a,b);
 cout<<"a="<<a<<endl<<"b="<<b;
 return 0;
}

The value of a and b are going to be changed after passing the values to the function and calling the function.

Output:   multiplication: 28

                  a=4

                  b=7

Call by pointer

Call by pointer passes the value to the function using the address of the variable and the pointer. And the value remains the same if you print the final values inside the function or inside the main function.

Code:

#include<iostream>
using namespace std;
class multi
{
private:
int c;
public:
void multiply(int *x,int *y)
{
  *x=*x+2;
  *y=*y+4;
  c=*x * *y;
  cout<<"multiplication is:"<<c<<endl;
}
};
int main()
{
 int a=2,b=3;
 multi obj;
 obj.multiply(&a,&b);
 cout<<"a="<<a<<endl<<"b="<<b;
 return 0;
}

Output:

multiplication is:28
a=4b=7

This is all about the function arguments in C++. If you have any doubts regarding this topic please ask the doubts in the comment section below.

Also read: C++ program to get Nth node in a Linked List

One response to “Function Arguments in C++”

  1. Uppaluri Anirudh says:

    Well. Thank you. The way you presented was pretty good and understandable. Really I was happy with the program

Leave a Reply

Your email address will not be published. Required fields are marked *