08. Special Functions
08. Special Functions
Oriented
Programming
Using
C ++
Special Functions
Abdul Wahab
Lecturer
University of Science and Technology Bannu
abdul_bu@yahoo.com
1
Function
A function is a self contain block of statements that perform some specific task of some
kind
With Functions
– Programs are divided into segments
– Code duplication is avoided
– Bugs can be searched easily in small programs (functions)
– Testing is done easily
2
Advantages of Functions
3
Function Prototype
It consists of Function return type, function name and arguments list, terminated by a
semicolon.
For Example:
void Show(void);
int Square(int);
float Sum(float, float);
4
Function Definition
The ‘Function Declarator’ and ‘Function Prototype’ declaration must match each other.
If the function is defined before its caller function, then its ‘Prototype’ declaration is
optional.
5
Formal & Actual Arguments
void main()
Actual Arguments
{
float x=Sum(y, z);
}
6
Passing Values to Function
Pass by Value
Any change in the formal arguments does not effect actual arguments as
Once control returns back to the calling function, the changes made (to formal
arguments) vanish
7
Example:
void main()
{ void change(int a, int b)
int x, y; {
clrscr(); int k;
void change(int, int); k = a;
cout<<"Enter Values: "; a = b;
cin>>x>>y; b = k;
cout<<"Before change: "<<endl; }
cout<<"X= "<<x<<" and Y= "<<y<<endl;
change(x, y);
cout<<"After Change: "<<endl;
cout<<"X= "<<x<<" and Y= "<<y;
}
8
Output
Enter Values: 50 40
Before change:
X= 50 Y= 40
After Change:
X= 50 Y= 40
9
Passing Values to Function
Pass by Address
10
Example:
void main()
{ void change(int *a, int *b)
int x, y; {
clrscr(); int *k;
void change(int *, int *); *k = *a;
cout<<"Enter Values: "; *a = *b;
cin>>x>>y; *b = *k;
cout<<"Before change: "<<endl; }
cout<<"X= "<<x<<" and Y= "<<y<<endl;
change(&x, &y);
cout<<"After Change"<<endl;
cout<<"X= "<<x<<" and Y= "<<y;
}
11
Output
Enter Values: 50 40
Before change:
X= 50 Y= 40
After Change:
X= 40 Y= 50
12
Passing Values to Function
Pass by Reference
‘&’ operator is nearly the same as pointer (*) operator, but not exaxtly
int k=0;
int &kk=k;
kk=2; //same as, writing k=2;
13
Example:
void main()
{ void change(int &a, int &b)
int x, y; {
clrscr(); int k;
void change(int &, int &); k = a;
cout<<"Enter Values: "; a = b;
cin>>x>>y; b = k;
cout<<"Before change: "<<endl; }
cout<<"X= "<<x<<" and Y= "<<y<<endl;
change(x, y);
cout<<"After Change"<<endl;
cout<<"X= "<<x<<" and Y= "<<y;
}
14
Output
Enter Values: 50 40
Before change:
X= 50 Y= 40
After Change:
X= 40 Y= 50
15
Another Example:
void FunA(int i)
{ ++i; } cout<<"\nAfter Calling Functions"<<endl;
16
Output
Value of S= 4
After Calling Functions
Value of S= 4 by Value
Value of S= 5 by Reference
Value of S= 6 by Address
17
Default Arguments
Usually, a function is called with all the arguments as declared in function prototype
declaration and function definition.
However, we can call a function with less or without parameters using default values.
It is not allowed to assign default value which is in between the variable list.
Variable lacking default values are written first, followed by the variable containing
default values.
18
Default Arguments
In case, the function is defined before caller function, then there is no need to declare
function prototype, so default values are placed in function declarator.
19
Another Example:
void main() cout<<"\nSum with 2 Args= "<<Sum(w, x);
{ cout<<"\nSum with 1 Args= "<<Sum(w);
clrscr(); cout<<"\nSum with 3 Args= "<<Sum(x, y, z);
int Sum(int a, int b=10, int c=15, int d=20);
int w=2; getch();
int x=3; }
int y=4;
int z=5; int Sum(int j, int k, int l, int m)
cout<<"Sum with 4 Args= "<<Sum(w, x, y, z); {
cout<<"\nSum with 3 Args= "<<Sum(w, x, y); return (j + k + l + m);
}
20
Output
21
Inline function
At each call to function, control passes to the function at a specified address in the
memory.
If the same function is called several times, each time the control is passed to the
function.
22
Inline function
Compiler copies the code of ‘Inline function’ in the calling function i.e. function body is
inserted in the place of function call during compilation.
23
Another Example:
inline float Square (float j)
{
return (j * j);
}
void main()
{
clrscr();
int p=3, r;
r=Square(p); //In compilation ‘Square()’ code will be copied here
cout<<”R= ”<<r;
getch();
}
24
Inline function
– Functions, containing control structure statements such as switch, if, for loop etc. can not be
Inline
25
Friend Function
The central idea of Encapsulation and Data Hiding is that any non-member function has
no access to the private data of the class
C++ allows a mechanism, in which a non-member function has access permission to the
private members of the class.
This can be done by declaring a non-member function Friend to the class whose private
data is to be accessed. Key word friend is used
26
Another Example:
class Acount void show(Acount Ac)
{ {
string Name; cout <<"\nName :"<<Ac.Name;
int AcNo; cout<<"\nAcount No: "<<Ac.AcNo;
float Bal; cout<<"\nBalance: "<<Ac.Bal;
}
public:
void Set() void main()
{ {
Name = "Bilal Khan"; clrscr();
AcNo = 2348; Acount AcObj;
Bal = 45000.98; AcObj.Set();
}
friend void show(Acount); show(AcObj);
}; getch();
}
27
Assignment
1. Write a program to exchange values between two classes using a Friend Function
2. Write a program to declare three classes, declare integer array as data member in each
class. Perform addition of the two data member arrays of the two classes into the array
of third class using a Friend Function
28
Have a Good Day!