Professional Documents
Culture Documents
(LECTURE 3)
Agenda
• What is a function? • Sharing data among
• Types of C++ functions: functions through
• Standard functions function parameters
• User-defined functions • Value parameters
• Reference parameters
• C++ function structure
• Const reference
• Function signature
parameters
• Function body
• Scope of variables
• Declaring and
• Local Variables
Implementing C++ • Global variable
functions
3
Outline
A basic function in C++ looks similar to the function in C.
input/output.
• Formatted I/p reads characters from the input file and converts
Unformatted i/o
• cout, cin
• getline (name_of_variable, size);
• Examples : cin.getline (title,256);
7
• The general form for given above user – defined data type is as follows:
Global Variable
10
void main()
{
x = 4;
fun();
cout << x << endl;
}
void fun()
{
int x = 10; // Local variable
cout << x << endl;
}
11
void fun()
{
int x = 10; // Local variable
cout << x << endl;
}
12
void main()
{
x = 4;
fun();
cout << x << endl;
}
void main()
{ void fun()
x = 4; x 10
fun();
cout << x << endl; {
} int x = 10;
4 cout << x << endl;
void fun() }
{ void main()
int x = 10; // Local variable {
cout << x << endl; x = 4;
} 2 fun();
cout << x << endl;
}
16
void main()
{ void fun()
x = 4; x 10
fun();
cout << x << endl; {
} int x = 10;
cout << x << endl;
void fun() 5 }
{ void main()
int x = 10; // Local variable {
cout << x << endl; x = 4;
} 2 fun();
cout << x << endl;
}
17
void main()
{
x = 4;
fun();
cout << x << endl;
}
void fun()
{ void main()
int x = 10; // Local variable {
cout << x << endl; x = 4;
} fun();
6 cout << x << endl;
}
18
void main()
{
x = 4;
fun();
cout << x << endl;
}
void fun()
{ void main()
int x = 10; // Local variable {
cout << x << endl; x = 4;
} fun();
cout << x << endl;
7 }
Function Call Stack and Activation Records
• Function-Call Stack
• Stack Frames
Fig 5
Fig 6
33
Inline Function
34
Inline Functions
• Sometimes, we use the keyword inline to define
user-defined functions
– Inline functions are very small functions, generally, one
or two lines of code
– Inline functions are very fast functions compared to the
functions declared without the inline keyword
• Example
inline double degrees( double radian)
{
return radian * 180.0 / 3.1415;
}
35
Example #1
• Write a function to test if a number is an odd number
int x = 0;
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2();
cout << x << endl; void main()
{
} f2();
7 cout << x << endl;
}
44
int x = 0;
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2();
cout << x << endl; void main()
{
} f2();
cout << x << endl;
8 }
45
int x = 0;
Inline void f1() { x++; } The inline keyword
instructs the compiler
Inline void f2() { x+=4; f1();} to replace the function
call with the function
void main() body!
{
f2();
void main()
cout << x << endl; {
1 x+=4;
} x++;
cout << x << endl;
}
48
int x = 0;
Inline void f1() { x++; }
Inline void f2() { x+=4; f1();}
void main()
{
f2();
void main()
cout << x << endl; {
x+=4;
} 2 x++;
cout << x << endl;
}
49
int x = 0;
Inline void f1() { x++; }
Inline void f2() { x+=4; f1();}
void main()
{
f2();
void main()
cout << x << endl; {
x+=4;
} x++;
3 cout << x << endl;
}
50
int x = 0;
Inline void f1() { x++; }
Inline void f2() { x+=4; f1();}
void main()
{
f2();
void main()
cout << x << endl; {
x+=4;
} x++;
cout << x << endl;
4 }
51
definition.
FRIEND FUNCTION
class myclass
{
friend void buddy(myclass,int);
private:
int c;
public:
void display()
{
cout<<"the value of private variable is :” <<c<<endl;
}
};
Any Questions ??