Skip to main content

Refernce Variables, Function Prototype, Inline Function

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.
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

Popular posts from this blog

Characteristics of a Good Programming Language

Till now there are many high level languages which are very popular, and there are others, which could not become so popular in-spite of being very powerful. There might be many reasons for the success of a language, but one obvious reason is the characteristics of the language. Several characteristics believed to be important with respect to making a programming language good are briefly discussed below. Simplicity A good programming language must be simple and easy to learn and use. For example, BASIC is liked by many programmers only because of its simplicity. Thus, a good programming language should provide a programmer with a clear, simple and unified set of concepts which can be easily grasped. It is also easy to develop and implement a compiler or an interpreter for a programming language that is simple. However, the power needed for the language should not be sacrificed for simplicity. The overall simplicity of a programming language strongly affects the readability of the pr...

Angular 4 and Firebase Authentication: Setup

If you have come to this article, it means you are keen to learn new technology and that too Firebase. I love Firebase because it provides all basic but important features of any web application. Take for example, authentication, which is cumbersome and risky if not implemented with utmost care. And when we have the integration of Angular and Firebase, it becomes a lot easier for a developer to build such crucial modules in less time with minimal efforts. In this article we will create a simple Angular application using Firebase. I am going ahead with the understanding that you know the benefits of Firebase and have little knowledge about it. There are plenty of posts out on the web if you are behind. Step 1: Install Angular CLI (if not installed). npm install -g angular-cli Step 2: Create new Angular 4 project. By default now angular CLI will create Angular 4 project so you need not fret. ng new firebase-authentication Step 3: Check whether the ne...

Switch-case, simple calculator . . .

11.      Write a program to perform some mathematic functions with the help of “switch-case” statement. #include<stdio.h> #include<conio.h> void main() { int a,b,ans; char c; clrscr(); printf("\ Enter a and b:"); scanf("%d%d",&a,&b); printf("\n Enter operator:"); fflush(stdin); scanf("%c",&c); switch(c) { case'+':ans=a+b;       break; case'-':ans=a-b;       break; case'*':ans=a*b;       break; case'/':ans=a/b;       break; } printf("\n ans %d",ans); getch(); } 12.      Print the Fibonacci series #include<stdio.h> #include<conio.h> void main() { int a=1,b=1,s=3,c,ch; clrscr(); printf("how many steps="); scanf("%d",&ch); printf("%5d%5d",a,b); for(s=3;s<=ch;s++) {       c=a+b;       printf("%5d",c);       a=b;    ...