Function Call and Return in C++

The function is the way we represent actions and computations. Whenever we want to do something that is worthy of a name, we write a function. The C++ language gives us operators ( such as + and *) with which we can produce new values from operands in expressions, and statements(such as for and if) with which we can control the order of execution. To organize code made out of these primitives, we have functions. To do its job, a function usually needs arguments and many functions return a result.

Page Contents:

Declaring arguments and return type
Returning a value
Pass-by-value
Pass-by-const-reference
Pass-by-reference
Call by Value:
Call by reference:

Declaring arguments and return type

The function is what we use in C++ to name and represent computations and actions. A function declaration consists of a return type followed by the name of the function followed by a list of formal arguments in parentheses. For example:

double fct(int a, doubled);   //declaration of fact(no body)
double fct(int a,double d)
{
return a*d;
} //definition of fct

A definition contains the function body(the statements to be executed by a call), whereas a declaration that isn’t a definition just has a semicolon. Formal arguments are often called parameters. If you don’t want a function to take arguments, just leave out the formal arguments. For example:

int current_power();  //current_power doesn't take an argument

If you don’t want to return a value from a function, give void as its return type. For example:

void increase_power(int level); //increase_power doesn't return a value 

Here void means “doesn’t return a value” or “return nothing”

You can name a parameter or not as it suits you in both declarations and definitions. For example:

//search for s in vs;
//vs[hint] might be a good place to start the search
//return the index of a match;-1 indicates "not found"
int my_find(vector<string>vs, string s,int hint);  //naming arguments
int my_find(vector<string>,string,int);  //not naming arguments

In declarations, formal argument names are not logically necessary, just very useful for writing good comments. From a compiler’s point of view, the second declaration of my_find() is just as good as the first: it has all the information necessary to call my_find().

Usually, we name all the arguments in the definition. For example:

int my_find(vector<string>vs, string s, int hint)
//search for s in vs starting at hint
{
if(hint<0||vs.size()<=hint)
hint=0;
for(int i=hint;i<vs.size();++i)  //search starting from hint
if(vs[i]==s)
return i;
if(0<hint)
{
//if we didn't find s search before hint
for(int i=0;i<hint;++i)
if(vs[i]==s)
return i;
}
return -1;
}

Returning a value

We return a value from a function using a return statement which stops execution and returns the calling function Examples for returning a value is given below:

double my_abs(int x)
{
if(x<0)
return -x;
else if(x>0)
return x;
}  //error:no value returned if x is 0

Pass-by-value

The simplest way of passing an argument to the function is to give the function a copy of the value you use as the argument.An argument of a function f() is a local variable in f() that are initialized each time f() is called.For example:

//pass-by-value(give the function a copy of the value passed)
int f(int x)
{
x=x+1;  //give the local x a new value 
return x;
}
int main()
{
int xx=0;
cout<<f(xx)<<'\n';  //write :1
cout<<xx<<'\n';   //write :0;f() doesn't change xx
int yy=7;
cout<<f(yy)<<'\n';  //write:8
cout<<yy<<'\n'; //write:7;f() doesn't change yy
}

Since a copy is passed, the x=x+1 in f() does not change the values xx and yy passed in the two calls.

Note* Use pass-by-value to pass a very small object

1.Example for Pass-by-value in C++

#include<iostream> 
 using namespace std; 
 void change(int data); 
 int main() 
 { 
 int data = 3; 
 change(data); 
 cout << "Value of the data is: " << data<< endl; 
 return 0; 
 } 
 void change(int data) 
 { 
 data = 5; 
 } 

OUTPUT:

Value of the data is: 3

Pass-by-const-reference

Pass-by-value is simple, straightforward, and efficient when we pass small values, such as an int, a double, or a Token. But what if a value is large, such as an image(often, several million bits), a large table of values(say, thousands of integers), or a long string(say, hundreds of characters)? Then, copying can be costly. We should not be obsessed with cost but doing unnecessary work can be embarrassing because it is an indication that we didn’t directly express our idea of what we wanted. For example, we could write a function to print out a vector of floating-point numbers like this:

void print(vector<double>v)  //pass by value;
{
cout<<"{";
for(int i=0;i<v.size();i++)
{
cout<<v[i];
if(i!=v.size()-1)
cout<<",";
}
cout<<"}\n";
}

We could use this print() for vectors of all sizes

Use pass-by-const-reference to pass large objects that you don’t need to modify

Pass-by-reference

In Pass by reference case, the reference of the variable is passed to the function by passing the address of the parameters, Since the address of the variables are available, the called function can access the actual parameters

void init(vector<double>&v)  //pass-by-reference
{
for(int i=0;i<v.size();++i)
v[i]=i;
}
void g(int x)
{
vector<double>
vd1(10);  //small vector
vector<double>
vd2(1000000);  //large vector
vector<double>
vd3(x);  //vector of some
init(vd1);
init(vd2);
init(vd3);
}

Here, we wanted to init() to modify the argument vector, so we did not copy(did not use pass-by-value) or declare the reference const(did not use pass-by-const-reference) but simply passed a “plain reference” to the vector

1.Example for Pass-by-reference in C++

#include<iostream>
 using namespace std; 
 void swap(int *x, int *y) 
 { 
 int swap; 
 swap=*x; 
 *x=*y; 
 *y=swap; 
 } 
 int main() 
 { 
 int x=500, y=100; 
 swap(&x, &y); // passing value to function 
 \cout<<"Value of x is: "<<x<<endl; 
 cout<<"Value of y is: "<<y<<endl; 
 return 0; 
 } 

OUTPUT:

Value of x is: 100
Value of y is: 500

Use pass-by-reference only when you have to.

Call by value and reference
Call by value and reference

Call by Value:

  1. In this case, the value of the parameters is passed to the called function
  2. In this case, the actual parameters are not accessible by the called function
  3. This Call-by-value is implemented by using simple variable names
  4. Hence the actual parameters remain unchanged in case of the call by value

Call by reference:

  1. In this case, the reference of the variable is passed to the function by passing the address of the parameters
  2. In this case, since the address of the variables are available, the called function can access the actual parameters
  3. This Call-by-reference is implemented by the use of pointer variables
  4. Hence the actual parameters can be altered if required in case of the call by reference method
Translate »