NIOS Computer Science: Chapter 12 – Function Part 9

Download PDF of This Page (Size: 547K)

Example of Call by Reference

Example of Call by Reference

Example of Call by Reference

Call-by-Reference

Consider the following program which will swap the value of two variables using call-by-reference method.

# include < iostream.h >

void swap (int &, int &);

void main ( )

{

int a, b;

cin > > a > > b;

Swap (a, b);

cout < < a < < b < < “\n”;

}

void swap (int &c, int &d)

Inline Function

Functions save memory space because all the calls to the function cause the same code to be executed

The functions body need not be duplicated in memory.

When the compiler sees a function call, it normally jumps to the function.

At the end of the function, it jumps back to the statement following the call.

The sequence of events may save memory space, it takes some extra time.

Each time there is a function call, the actual code from the function is inserted instead of a jump to the function.

The inline function is used only for shorter code.

Example 6

# include < iostream.h >

inline int cube (int r)

{

return

}

void main()

{

Int number y;

cout<<”Enter number”;

cin>> number;

y=cube(number);

cout<<”The cube of the number is”<<y;

}

Some important points to be noted:

(1) Function is made inline by putting a word inline in the beginning.

(2) Inline function should be declared before main ( ) function.

(3) It does not have function prototype.

(4) Only shorter code is used in inline function.

(5) If longer code is made inline then compiler ignores the request and it will be executed as normal function.