Often people give alternative name to a person, may be of love or it is simply easier to pronounce. For example, the family members and friends of Parminder may call him Pummy. Both Parminder and Pummy refer to the same person called Parminder. Here Pummy is a reference to Parminder.
Made By: Chaudhary Amit V.
Made By: Chaudhary Amit V.
Article No: 4
REFERENCE VARIABLES
C++ introduces a new kind of variable known as the reference variable. A reference provide an alias (alternative name) for a previously defined variable. For example, if we make the variable sum a reference to the variable total, then sum and total can be used interchangeably to represent that variable. You must note that any change in the value of references also changes the value of variable.
For creating reference first write the type of the variable for which the reference is being created then write reference operator (&) followed by the name or identifier for the reference. Name of the reference may be decided in the same way as it is done for any variable.
Data type & reference name = variable name // how to declare
Float total = 100;
Float & sum = total;
Here float is a data type, total is variable, sum is an alternative name of total.
Program 1:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float total=100;
float & sum=total;
cout << "\n" << total;
cout << "\n" << sum ;
total = total + 10;
cout << "\n" << total;
cout << "\n" << sum ;
getch();
}
Explanation :
Here, already the value ‘100’ is assigned to total, and we had taken ‘sum’ as a reference, so whenever there will be change in any variable either in sum or total, it will affect to both. After executing this program the value of total as well as value of sum will be changed to110.
Program 2:
#include<iostream.h>
#include<conio.h>
void f(int & x)
{
x=x+10;
}
void main()
{
int m=10;
f(m);
cout << “value of m = “ << m;
getch();
}
FUNCTION PROTOTYPE
A function prototype is a declaration that defines both: the arguments passed to the function and the type of value returned by the function. If we were to call a function add() which receives float and an int as arguments and returns a double value its prototype would look like this:
Syntax:
Return data-type fun-name (argument list); //prototype
double add(float, int); // prototype
The C++ compiler uses this prototype to ensure that the types of the arguments you pass in a function call are same as those mentioned in prototype. If there is a mismatch, the compiler points it out immediately. This is known as strong type checking; something which C lacks.
Without strong type checking, it is easier to pass illegal values to functions. For example, a non-prototyped function would allow you to pass an int to a pointer variable, or a float to a long int. The C++ compiler would immediately report such types of errors. In C++ prototypes it do more than making sure that actual arguments (those used in calling function) and formal arguments (those used in called function) match in number, order and type. C++ internally generates names for functions, including the argument type information. This information is used when several functions have same names.
Remember that all functions in C++ must be prototyped. This means every function must have its argument list declared, and the actual definition of a function must exactly match its prototype in the number, order and types of parameters.
Remember, C also uses prototyping. But it was introduced first in C++ by Strostrup and the success of this feature inspired the ANSI C committee to adopt it. However, there is a major difference in prototyping between C and C++. While C++ makes the prototyping essential, ANSI C makes it optional, perhaps to preserve the compatibility with classic C.
Program 3:
#include<stdio.h>
#include<conio.h>
int add(int x, float y); // prototype
void main()
{
clrscr();
int a,b,sum=0;
cout << “\n enter a and b”;
cin >> a >> b;
sum=add(a,b);
}
int add(int x, float y)
{
int z=x+y;
return z;
}
INLINE FUNCTION
One of the objectives of using functions in a program is to save memory space, which become appreciable when a function is likely to be called many times. However every time a function is called, it takes a lot of extra time in executing a series of instructions for task such as :
· Jumping to the function
· Evaluates the function
· Pushing arguments into the stack
· Returning to the call function
When a function is small, cost of call is very high, so to reduce cost of call to the small function C++ proposes new features called inline function.
An inline function is a function that is expanded inline when it is invoked.
Syntax:
inline function-header
{
Function body
}
Example:
inline double cube(double a)
{
return(a*a*a);
}
It is easy to make a function inline. All we need to do is to prefix the keyword inline to the function definition. All inline functions must be defined before they are invoked.
We should exercise care before making a function inline. The speed benefits of inline functions destroy as the function grows in size. At some point the overhead of the function call becomes small compared to the execution of the function, and the benefits of inline functions may be lost. In such cases, the use of normal function will be more meaningful. Usually, the functions are made inline when they are small enough to be defined in one or two lines.
Remember that the inline keyword merely sends a request, not a command, to the compiler. The compiler may ignore this request if the function definition is too long or too complicated and compile the function as a normal function.
Some of the situation where inline may not work:
· If functions contain static variables.
· If inline functions are recursive.
Inline expansion makes a program run faster because the overhead of a function call and return is removed; however, it makes the program to take up more memory because the statements that define the inline function are reproduced at each point when the function is called. So, a trade-off becomes necessary.
Program 4:
#include<iostream.h>
#include<conio.h>
inline mul(int a,int b)
{
return (a*b);
}
void main()
{
int a=5,b=6;
cout << mul(a,b);
getch();
}
Comments
Post a Comment