Professional Documents
Culture Documents
(LECTURE 3-4)
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
• Each function eventually must return control to the function
that called it.
• Each time a function calls another function, an entry is pushed
onto the function call stack.
• This entry, called a stack frame or an activation record, contains the
return address that the called function needs in order to return to the
calling function.
Function Call Stack and Activation Records (cont.)
int x = 0;
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2();
cout << x << endl; void main()
{
} f2();
7 cout << x << endl;
}
I. Using Global Variables
#include <iostream.h> x 5
4
int x = 0;
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2();
cout << x << endl; void main()
{
} f2();
cout << x << endl;
8 }
I. Using Global Variables
#include <iostream.h>
int x = 0;
void f1() { x++; }
void f2() { x+=4; f1(); }
void main()
{
f2();
cout << x << endl;
}
What Happens When We Use Inline
Keyword?
#include <iostream.h>
int x = 0;
Inline void f1() { x++; }
Inline void f2() { x+=4; f1();}
void main()
{
f2();
cout << x << endl;
}
What Happens When We Use Inline
Keyword?
#include <iostream.h> x 0
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;
}
What Happens When We Use Inline
Keyword?
#include <iostream.h> x 4
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;
}
What Happens When We Use Inline
Keyword?
#include <iostream.h> x 5
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;
}
What Happens When We Use Inline
Keyword?
#include <iostream.h> x 5
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 }
What Happens When We Use Inline
Keyword?
#include <iostream.h>
int x = 0;
Inline void f1() { x++; }
Inline void f2() { x+=4; f1();}
void main()
{
f2();
cout << x << endl;
}
Static Function Member
STATIC FUNCTION MEMBERS
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;
}
};
Some Important concepts in C++
Review: What = and & Mean
• In C++ the = symbol means either initialization or assignment
• If it’s used with a type declaration, it means initialization
• If it’s used without a type declaration, it means assignment
int j(7); // j is initialized with value 7
int k = 4; // k is initialized with value 4
j = 3; // j is assigned value 3