C++ interaction between function - programming



    
Interaction between Function

The   main() function and other function are interact with each other through parameter we pass the 
parameter to the function for the communication of calling function and call function .
the interaction can take pace through :

1.      Call by value.
2.      Call by reference .
3.      Return by reference .
4.      Default argument .
5.      Constant parameter.
6.      Passing pointer.




Call By Value:-

  Like c language C++ also provides a call by value technique of parameter passing.
  During the function call the values of actual parameter gets copied into formal passing.
  Any change made to formal parameter does not affect to actual parameter.

 Swapping program done by call by value.



#include <cstdlib>
#include<iostream>
using namespace std;

void swap(int n1, int n2)
{
int temp;
temp=n1;
n1=n2;
n2=temp;
cout<<"\nAfter swap : ";
cout<<"\t"<<n1<<"\t"<<n2;
}


int main(int argc, char** argv) {
int num1,num2;
void swap(int,int);
cout<<"enter two numbers ";
cin>>num1>>num2;
cout<<"before swap : ";
cout<<"\t"<<num1<<"\t"<<num2;
swap(num1,num2);
cout<<"\n";
    return 0;
}
****************************************
Output
enter two numbers 5  4
before swap :   5  4
After swap :    4  5




Call By Reference:-

The disadvantage of call by value technique of parameter passing is we can not return more than 1 value by called function.
With call by reference technique of parameter passing we can not return more than one value but we can directly change the respective value.
In this method of passing parameter if any change made in formal parameter that affects the actual parameter.

Program for swapping two variable using call by reference.

* Author: ROHAN
 *
 * Created on July 11, 2018, 12:14 PM
 */

#include <cstdlib>
#include<iostream>
using namespace std;

void swap(int &n1, int &n2)
{
int temp;
temp=n1;
n1=n2;
n2=temp;
cout<<"\nAfter swap : ";
cout<<"\t"<<n1<<"\t"<<n2;
}


int main(int argc, char** argv) {
int num1,num2;
void swap(int&,int&);
cout<<"enter two numbers ";
cin>>num1>>num2;
cout<<"before swap : ";
cout<<"\t"<<num1<<"\t"<<num2;
swap(num1,num2);
cout<<"\n";
    return 0;
}

***********************************************
Output/
enter two numbers  4  5
before swap :   4   5
After swap :    5   4


Constant Argument:-

The constant value as a function argument is passing using  ‘const ‘ qualifier.
This tell to compiler that function should not modified the argument .
If the value change compiler gives an error .

Program to find area of circle without const

/* Author: ROHAN
 *
 * Created on July 11, 2018, 1:12 PM
 */

#include <cstdlib>
#include<iostream>
using namespace std;

float area (int r, float p)
{
     p=0;
    return(p*r*r);
}
int main(int argc, char** argv) {
    int rad;
     float pi=3.24;
    float a;
    float area(int ,const float);
    cout<<"Enter the radius : ";
    cin>>rad;
    a=area(rad,pi);
    cout<<"the areaof circle : "<<a;
    return 0;
}

**********************************************
output
Enter the radius : 5
the areaof circle : 0



Program with constant

/*
 * File:   main.cpp
 * Author: ROHAN
 *
 * Created on July 11, 2018, 1:12 PM
 */

#include <cstdlib>
#include<iostream>
using namespace std;
float area (int r,const float p)
{
    return(p*r*r);
}
int main(int argc, char** argv) {
    int rad;
    const float pi=3.24;
    float a;
    float area(int ,const float);
    cout<<"Enter the radius : ";
    cin>>rad;
    a=area(rad,pi);
    cout<<"the areaof circle : "<<a;
    return 0;
}

****************************************************

Output
Enter the radius : 5
the areaof circle : 81

Default argument:

C++ allow to pass default argument to function ,so lets see the syntax and program.

Syntax:

Datatype function name(datatype=value);

float amount(float a=30,int p);

program addition using default argument

/* Author: ROHAN
 *
 * Created on July 11, 2018, 1:33 PM
 */

#include <cstdlib>

using namespace std;

#include<iostream>
using namespace std;
int main()
{
int add (int a,int b=20,int c=30,int d=40);
cout<<add(10)<<endl;
cout<<add(10,5)<<endl;
cout<<add(1,2,3)<<endl;
return 0;
}

int add (int p, int q, int r, int s)
{
return (p+q+r+s);
}

********************************************************
Output
100
85
46


Passing pointer to function:-

C++ allows to pass pointer to function, so lets see directly by doing program. How to pass pointer to the function.

#include<iostream>
#include<ctime>
using namespace std;
void get(int *p);
int main()
{
int s=10;
get(&s);
cout<<"Number of second :  "<<s<<endl;
return 0;

}
void get(int  *p){
*p=time(NULL);
return;
}

*********************************************

Output:

Number of second :  1531331852


Comments