C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C++ programming language

New in C++ Functions
(Call by References, Call By Values, Call by Values, Return Reference, Default Arguments)


Function calling : CALL BY REFERENCE

We have already learned in C that function calling done by two types : 1) Call by value. 2) Call by address.

C++ introduces the concept of call by reference ,it enables us to pass parameters to the functions by reference. when we pass arguments by reference, the format arguments in the called functions become alias to the actual arguments in the calling function, this means that when the function is working with its own arguments, it is actually working on the original data.

Syntax

//Declaration
return_type function_name( type_of_argument,type_of_argument,..);

//Calling
function_name(&actual_argument,&actual_argument,..);

//Definition
return_type function_name(type_of_argument &alias_variable,..)
{ 
    ....; 
}
    







Sponsored Links



Consider the example

// Program to swap two values using CALL BY REFERENCE
#include <iostream>
using namespace std;

// function declaration
void swap(int&,int&);

int main()
{
	int a=10,b=20;
	cout<<"\nVALUES BEFORE SWAPPING :\n";
	cout<<"A:"<< a<<",B:"<< b;
	// function calling
	swap(a,b);
	cout<<"\nVALUES AFTER SWAPPING :\n";
	cout<<"A:"<< a<<",B:"<< b;
	cout<<"\n";
	return 0;
}
// function definition
void swap(int &x,int &y)
{
	int t;
	t=x;
	x=y;
	y=t;
}
VALUES BEFORE SWAPPING :
A:10,B:20
VALUES AFTER SWAPPING :
A:20,B:10

Function returning : BY REFERENCE

In C++ a function can also return reference.

Syntax

//Declaration
return_type& function_name( type_of_argument,type_of_argument,..);

//Calling
container_var=function_name(&actual_argument,&actual_argument,..);

//Definition
return_type& function_name(type_of_argument &alias_variable,..)
{ 
    ....; 
    return reference_variable;
}

Consider the example

// Program to find largest number among three numbers using 

// call by reference and return reference...
#include <iostream>
using namespace std;

int& largestNumber(int&,int&,int&);
int main(){
	int a,b,c;
	a=10,b=22,c=21;

	cout<<"Largest number is :"<< largestNumber(a,b,c)<< endl;
	return 0;
}
int & largestNumber(int &x,int &y,int &z){
	if(x > y && x > z)
		return x;
	else if(y > x && y> z)
		return y;
	else
		return z;
}
    
Largest number is :22

Default Argument

C++ allows us to assign default values to function arguments, which is helpful in case a matching argument is not given by the user in function call statement.

If user does not provide value to the matching argument (that is assigned as a default argument, function will take default value as an argument.

A default argument is a part of function declaration (not definition - in function definition, it must always be provided). It instructs the compiler what value to pass for an argument if the programmer deliberately misses the argument when calling a function.

Syntax

//Function declaration with default argument
return_type function_name(type arg1,type arg2=default_value,...);
//here, arg2 is default argument....

Consider the example

// function declaration 
float simpleInterest(int amount,int time,float rate=3.5);
int main(){
	int amount,time;
	float rate;
	char choice;

	cout<<"Enter amount :"; cin>>amount;
	cout<<"Enter time   :"; cin>>time;
	cout<<"Do you want to input interest rate ? :";
	cin>>choice;

	if(choice=='Y'||choice=='y'){
		cout<<"Enter rate ?:"; cin>>rate;
		cout<<"Simple interest is : "<< simpleInterest(amount,time,rate);
	}
	else{
		// We are not passing rate, default value (3.5) will be used.
		cout<<"Simple interest is : "<< simpleInterest(amount,time);
	}
	cout<<"\n";
	return 0;
}
// function definition..
float simpleInterest(int amount,int time,float rate){
	float si;
	si=((float)amount*(float)time*rate)/100;
	return si;
}
    
First run
Enter amount :12450
Enter time   :3
Do you want to input interest rate ? :y
Enter rate ?:12.5
Simple interest is : 4668.75
    
Second run
Enter amount :12450
Enter time   :3
Do you want to input interest rate ? :N
Simple interest is : 1307.25