Pass by value, pass by reference, call by referance
Pass by valu, pass by reference , call by reference
a】Call by value⇒ In Call by Value the value that is fast is not changed after foming some operations on it.
⇒ In Call by Value it creates at caps of the variable into stack memory or stack section
⇒ when the value is changes the change value create a copy with actual value remaining the change
Ex:-
#include<iostream.h>
#include<conio.h>
class abc
{
public:
void add(int a,int b)
{
int c;
c=a+b;
cout<<"c="<<c;
}
};
int main()
{
abc s;
clrscr();
s.add(4,5);
getch();
}
b】pass by reference
⇒In Call by Reference function we pass the arguments with reference variable by using emperent
⇒In Call by Reference, actual value is passed as the argument ,it argument is changed after forming some options on it
⇒When Call by Reference it use it create the Call by Reference a copy of reference into the copy of the reference into the Stark section of memory.
⇒In Call by Reference if the value is modified than the actual value is also modified.
⇒In Call by Value it an independent passing of argument is an dependent passing by argument.
#include<iostream.h>
class test
{
int a;
public:
void change(int &a)
{
a=6;
cout<<"a="<<a<<endl;
}
};
int main()
{
test t;
int a=9;
t.change(a);
cout<<"a="<<a<<endl;
return 0;
}
c】call by reference
In call by address is a new process introduced in C + +
⇒ In call by address with pass pointer variable as an argument and it invoke the function call by reference address of respective Pointer variable such type of calling function is known as call by address
⇒ In call by address we use pointer variable to send exact memory address.
⇒ call by address is similar to the call by reference.
#include<iostream.h>
class test
{
int a,b,t;
public:
void change(int *a,int *b)
{
t=*a;
*a=*b;
*b=t;
}
};
int main()
{
test s;
int a=9,b=7;
cout<<"Before swap\na="<<a<<"\nb="<<b;
s.change(&a,&b);
cout<<"\nafter swap\na="<<a<<"\nb="<<b;
return 0;
}
2.Inline function{short}
Inline function:-
⇒ One of the objective of the using function in the programming is to save memory space
⇒ If we call the same some functions in many times for moving of cursor here and there to overcome this problem c++ introduce a new concept called inline
⇒ inline is a Keyword which replaces the function call with function definition
⇒ Situation where inline function not works.
- If function contain condition statement
- If function return values ,if a return statement exists
- Function contain static variable.
- If inline function are recursive.
Syntax:-
inline data_type function_name() { Function body; } |
inline int sq(int a)
{
return (a*a);
}
3.Function Overloading
⇒In C++ function overloading is any polymorphism is a represented Static Binding or compile time polymorphism function overloading is Defined by defined some functions and name multiple times and different sisnitures is known as function overloading.
⇒It is the process of using the same function name for different functions.
Rules for function Over loading:-
⇒In function overloading same function is define more than one time or multiple times.
⇒In function overloading process overloading function differ in passing arguments.
⇒In overloading function Defined by the differentiating in return type function overloading.
Note:-
⇒In function overloading whenever function is called prototype suitable to function is such type of function is known as the function polymorphism.
Syntax:-
Return_type function_name() { Function body; } Return_type fun_nm(argumentes list) { Function body; } |
void f1()
{
cout<<"hello";
}
int f1(int x)
{
return x;
}
char f1(char ch)
{
return ch;
}
Sample program:-
#include<iostream.h>
class test
{
int a,b;
public:
void sum()
{
int a=5,b=8;
cout<<(a+b)<<endl;
}
int sum(int x,int y)
{
return x+y;
}
float sum(float x,float y,float z)
{
return (x+y+z);
}
};
int main()
{
test t;
t.sum();
cout<<"call 2:"<<t.sum(3,5);
cout<<"\n call 3:"<<t.sum(2.5,5.6,7.3);
return 0;
}
Out put:-
13
call 2:8
call 3:15.4
[Program finished]
It completed answer will be uploaded soon.
4.Object as function argument's .
⇒In C++ we pass arruguments as objects with in specified function name.
⇒In C++ we define object as an arrugument
Using respective class name.
Syntax:-
Return_type function_name(class_nm obj_nm); |
Void disp (demo obj);
⇒In passing argument as an object into method we access the specified method by using object and by passing object.
Syntax:-
Class name obj_nm; Return_type function_name(class name); |
Obj.disp(obj)
Comments