Home | | Computer Science 11th std | C++: Methods of calling functions

Chapter: 11th Computer Science : Chapter 11 : Functions

C++: Methods of calling functions

In C++, the arguments can be passed to a function in two ways.

Methods of calling functions

 

In C++, the arguments can be passed to a function in two ways. Based on the method of passing the arguments, the function calling methods can be classified as Call by Value method and Call by Reference or Address method.

 

Call by value Method

 

This method copies the value of an actual parameter into the formal parameter of the function. In this case, changes made to formal parameter within the function will have no effect on the actual parameter.

 

Program 11.17

#include<iostream>

using namespace std;

void display(int x)

{

      int a=x*x;

      cout<<"\n\nThe Value inside display function (a * a):"<<a;

}

int main()

{

      int a;

      cout<<”\nExample : Function call by value:”;

      cout<<"\n\nEnter the Value for A :";

      cin>>a;

      display(a);

      cout<<"\n\nThe Value inside main function "<<a;

      return(0);

}

Output :

Example : Function call by value

Enter the Value for A : 5

The Value inside display function (a * a) : 25

The Value inside main function 5

 

Call by reference or address Method

 

This method copies the address of the actual argument into the formal parameter. Since the address of the argument is passed ,any change made in the formal parameter will be reflected back in the actual parameter.

 

Program 11.18

#include<iostream>

using namespace std;

void display(int &x) //passing address of a//

{

      x=x*x;

      cout<<"\n\nThe Value inside display function (n1 x n1) :"<<x ;

}

int main()

{

int n1;

cout<<"\nEnter the Value for N1 :";

cin>>n1;

cout<<"\nThe Value of N1 is inside main function Before passing : "<< n1; display(n1);

cout<<"\nThe Value of N1 is inside main function After passing (n1 x n1) : "<< n1; return(0);

}

Output :

Enter the Value for N1 :45

The Value of N1 is inside main function Before passing : 45

The Value inside display function (n1 x n1) :2025

The Value of N1 is inside main function After passing (n1 x n1) : 2025

 

Note that the only change in the display() function is in the function header. The & symbol in the declaration of the parameter x means that the argument is a reference variable and hence the function will be called by passing reference. Hence when the argument num1 is passed to the display() function, the variable x gets the address of num1 so that the location will be shared. In other words, the variables x and num1 refer to the same memory location. We use the name num1 in the main() function, and the name x in the display() function to refer the same storage location. So, when we change the value of x, we are actually changing the value of num1.

 

Inline function

 

Normally the call statement to a function makes a compiler to jump to the functions (the definition of the functions are stored in STACKS) and also jump back to the instruction following the call statement. This reduces the speed of program execution. Inline functions can be used to reduce the overheads like STACKS for small function definition.

An inline function looks like normal function in the source file but inserts the function's code directly into the calling program. To make a function inline, one has to insert the keyword inline in the function header.

 

Syntax :

inline returntype functionname(datatype parametername1, … datatype parameternameN)

 

Advantages of inline functions:

• Inline functions execute faster but requires more memory space.

• Reduce the complexity of using STACKS.

 

Program 11.19

#include <iostream>

using namespace std;

inline float simpleinterest(float p1,float n1, float r1)

{

      float si1=(p1*n1*r1)/100;

      return(si1);

}

int main ()

{

      float si,p,n,r;

      cout<<"\nEnter the Principle Amount Rs. :";

      cin>>p;

      cout<<"\nEnter the Number of Years :";

      cin>>n;

      cout<<"\nEnter the Rate of Interest :";

      cin>>r;

      si=simpleinterest(p,n,r);

      cout << "\nThe Simple Interest = Rs."<<si;

      return 0;

}

Output:

Enter the Principle Amount Rs. :60000

Enter the Number of Years     :10

Enter the Rate of Interest        :5

The Simple Interest = Rs.30000

 

Though the above program is written in the normal function definition format during compilation the function code (p1*n1*r1)/100 will be directly inserted in the calling statement i.e.si=simpleinterest(p,n,r); this makes the calling statement to change as si= (p1*n1*r1)/100;

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 11 : Functions : C++: Methods of calling functions |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.