- Formal and actual parameter are associated with the concept of 'call by value' and 'call by reference'
- see the effect of 'call by value' and 'call by reference'
//this program to illustrate call_by_reference
#include <iostream>
using namespace std;
int add(int &);
int main()
{
int a=2;
int square;
square = add(a);
cout << "\n" << a;
cout << "\n" << square;
}
int add(int &a)
{
a=a+1;
return(a*a);
}
OUTPUT:
3
9
======================================================
// this program to illustrate call_by_value
#include <iostream>
using namespace std;
int add(int);
int main()
{
int a=2;
int square;
square = add(a);
cout << "\n" << a;
cout << "\n" << square;
}
int add(int a)
{
a=a+1;
return(a*a);
}
OUTPUT:
2
9
======================================================
// This is a extra program to understand 'actual' and 'formal' parameter
// which associated with the 'call by value' and 'call by reference'
#include <iostream>
using namespace std;
void add(int a, int b) //here a, b are formal parameter
{
int c = 0;
c = a + b;
cout << "\n Addition of 2 number is: " << c << endl;
}
int main()
{
int a=0;
int b=0;
int c=0;
int d=0;
cout << "\n Please Enter 2 number: ";
cin >> a >> b;
d = a-b+c; //actual parameter
cout << " \n substraction is(d): " << d << endl;
add(a, b); //actual parameter
}
No comments:
Post a Comment