Passing Arguments and Returning Values in C++

 

Passing Arguments and Returning Values in C++:

 

Passing Arguments and Returning Values in C++

When we want to execute functions, we need to pass arguments (values) to a function. The result (values) produced within the function body is then returned back to the calling program.

The following section describes different methods used for passing arguments (values) to functions.


Also Read: Understanding Function Overloading Concept In C++.

Passing Arguments.

The following are the most commonly used methods of passing arguments to functions.

ü Passing arguments by constants.
ü Passing argument by values.
ü Passing arguments by reference.

#1 Passing Argument by Constants.

while calling a function, arguments are passed to the calling function. In passing constants as arguments, the actual constant values are passed instead of passing the variables.

Consider the following program:

write a program that pass integer constant as argument.

#include<iostream.h>
#include<conio.h>
void display(int x)
{
cout<<“x=”<<x<<endl;
}
int main()
{
display(25); // function call
getch();
return 0;
}

Output of the program
x=25

In the above program, the function call, display(5), passes the constant argument (5) to the function.

Consider another program defining a function displayGender() that takes a character constant, ‘F’ or ‘M’, as argument from the calling function:

Write a program that pass character constants as argument

#include<iostream.h>
#include<conio.h>
void displayGender(char ch)
{
cout<<“The gender marker you passed is:”<<ch<<endl;
}
int main()
{
displayGender(‘F’); //function call
getch();
return 0;
}

Output of the program.
The gender marker you passed is: F


Also Read: Scope of Local, Global and Static Variables in C++ (With Examples)

#2 Passing Argument by Values.

By default, argument in C++ are passed by value. When arguments are passed by value, a copy of the argument value is passed to the function formal parameter.

Consider the following program to demonstrate the use of passing arguments by value.

//passing parameters by value(program)
#include<isotream.h>
#include<conio.h>
void foo(int y)
{
cout<<“y in foo()=”<<y<<endl;
y=6;
cout<<“y in foo() = <<y<<endl;
}// y is destroyed here
int main()
{
int x=5;
cout<<“x in main() before call=”<<x<<endl;
foo(x); //argument passed by value
cout<<“x in main() after call=”<<x<<endl;
getch();
return 0;
}

Output of the program
x in main() before call=5
y in foo() =5
y in foo()=6
x in main() after call=5

In the program, the original value of x is not changed before and after calling the function foo().

Consider another example that uses a function addition that gets arguments by values.

//passing parameters by value
#include<iostream.h>
#include<conio.h>
int addition (int a, int b)
{
int result;
result=a+b;
return(result);
}
int main()
{
int z, x=5, y=3;
z=addition (x,y); //arguments passed by value
cout<<“The sum of both value is =”<<z;
getch();
return0;
}

Output of the program
The sum of both values is= 8

#3 Passing arguments by Reference.

In passing by reference, the reference to the function parameters are passed as arguments rather than value of variables. Using pass by reference, the value of arguments can be changed within the function.

In passing arguments by reference, the formal parameters should precede by the ampersand sign &.

 Consider the following example.
#include<iostream.h>
#include<conio.h>
void AddOne(int &y)
{
y++; //changing values in function
}
int main()
{
int x=55;
cout<<“x in main() before call=”<<x<<endl;
AddOne(x); //passing arguments by reference
cout<<“x in main()after call=”<<x<<endl;
getch();
return 0;
}

Output of the program
x in main() before call= 55
x in main() after call=56

In the above example, the value of x is passed by reference and changed inside the function AddOne().
This change affects the value of x in main() because the formal argument $y in the function declaration is a reference to x and any change by y results in change in x.

Consider another example having a function named duplicate that receive arguments by reference:

#include<iostream.h>
#include<conio.h>
void duplicate (int& a, int&b, int&c)
{
a=a*2;
b=b*2;
c=c*2;
}
int main()
{
int x=1, y=3, z=7;
cout<<“Values of x, y and z in main()before calling functionn”;
cout<<“x=”<<x<<“,y=”<<y<<“,z=”<<z<<endl;
duplicate(x, y, z)
cout<<“Values of x, y and z in main() after calling functionn”;
cout<<“x=”<<x<<“,y=”<<y<<“,z=”<<z;
getch();
return 0;
}

Output of the program.
vlaues of x, y and z in main() before calling function
x=1,y=3,z=7
values of x,y and z is main() after calling function
x=2, y=6, z=14

Sometimes we need a function to return multiple values. However, function can only return one value. One way to return multiple values is the use of the method of passing arguments by reference.

Also Read: What are the Components of Function in C/ C++
Passing arguments by this method allow programmers to change the value of the arguments, which is sometimes useful, similarly, it is a fast approach to passing and processing values in a function and also has the facility of returning multiple values form a function.

Also Read: 

Define Function And Explain Types of Functions in C++ (with Examples)

 

2 thoughts on “Passing Arguments and Returning Values in C++

Comments are closed.