Functions
A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.
There are two types of functions in c++
Syntax:-
functionname(argument/parameter)
{
statement
}
Argument/parameter:-It is like a variable which is used to hold any value comes from main
- Call by value
- Call by refrence
Syntax:-
functionname(argument/parameter)
{
statement
}
Argument/parameter:-It is like a variable which is used to hold any value comes from main
Call by Value
When we pass any variable from main to function is called call by value.In call by value method of parameter passing, the values of actual parameters are copied to the function’s formal parameters.
- There are two copies of parameters stored in different memory locations.
- One is the original copy and the other is the function copy.
- Any changes made inside functions are not reflected in the actual parameters of the caller.
There are 4 types of call by value
- With argument and with return
- with argument and without return
- without argument and with return
- without argument and without return
With Argument and with return
#include<iostream>.h
#include<conio>.h
class aptech
{
public:
sum(int a,int b)
{
return a+b;
}
};
void main()
{
aptech ap;
int x,y;
clrscr();
cout<<Enter two nos=";
cin>>x>>y;
cout<<ap.sum(x,y);
getch();
}
#include<iostream>.h
#include<conio>.h
class aptech
{
public:
sum(int a,int b)
{
return a+b;
}
};
void main()
{
aptech ap;
int x,y;
clrscr();
cout<<Enter two nos=";
cin>>x>>y;
cout<<ap.sum(x,y);
getch();
}
With Argument and without return
#include<iostream>.h
#include<conio>.h
class aptech
{
public:
sum(int a,int b)
{
cout<<a+b;
}
};
void main()
{
aptech ap;
int x,y;
clrscr();
cout<<Enter two nos=";
cin>>x>>y;
ap.sum(x,y);
getch();
}
#include<iostream>.h
#include<conio>.h
class aptech
{
public:
sum(int a,int b)
{
cout<<a+b;
}
};
void main()
{
aptech ap;
int x,y;
clrscr();
cout<<Enter two nos=";
cin>>x>>y;
ap.sum(x,y);
getch();
}
Without Argument and with return
#include<iostream>.h
#include<conio>.h
class aptech
{
public:
sum()
{
int x,y;
cout<<Enter two nos=";
cin>>x>>y;
return a+b;
}
};
void main()
{
aptech ap;
clrscr();
cout<<ap.sum();
getch();
}
#include<iostream>.h
#include<conio>.h
class aptech
{
public:
sum()
{
int x,y;
cout<<Enter two nos=";
cin>>x>>y;
return a+b;
}
};
void main()
{
aptech ap;
clrscr();
cout<<ap.sum();
getch();
}
Without Argument and without return
#include<iostream>.h
#include<conio>.h
class aptech
{
public:
sum()
{
int x,y;
cout<<Enter two nos=";
cin>>x>>y;
cout<<a+b;
}
};
void main()
{
aptech ap;
clrscr();
ap.sum();
getch();
}
#include<iostream>.h
#include<conio>.h
class aptech
{
public:
sum()
{
int x,y;
cout<<Enter two nos=";
cin>>x>>y;
cout<<a+b;
}
};
void main()
{
aptech ap;
clrscr();
ap.sum();
getch();
}
Call by refrence
When we pass any address of variable from main to function.The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.
We have already disussed call by refrence in C pointer session