Function Animation

FUNCTIONS - I
Chapter 5
ANIMATION
1
3 Demos
Demo of a simple value-returning function
Demo of a void function
Demo of a void function calling a valuereturning function
2
Demo 1
A Simple Value Returning
Function
3
Demo 1) simple float-function
float cube(float x)
{ float y=x*x*x;
return y;
}
int main()
{
float n=5;
cout<< cube(n) << endl;
}
4
Demo 1) simple float-function /2
float cube(float x)
{ float y=x*x*x;
return y;
}
n
int main()
5
{
float n=5;  START EXECUTION
cout<< cube(n) << endl;
}
5
Demo 1) simple float-function /3
x
float cube(float x)
5
{ float y=x*x*x;
return y;
}
n
int main()
5
{
float n=5;
cout<< cube(n) << endl; Fn Call
}
6
Demo 1) simple float-function /4
x
float cube(float x)
5
{ float y=x*x*x; In Fn
return y;
}
int main()
{
float n=5;
cout<< cube(n) << endl;
}
y
125
7
Demo 1) simple float-function /5
x
float cube(float x)
5
{ float y=x*x*x;
return y; RETURN
}
int main()
{
float n=5;
cout<< cube(n) << endl;
}
y
125
8
Demo 1) simple float-function /6
float cube(float x)
{ float y=x*x*x;
return y;
}
n
int main()
5
{
float n=5;
cout<< 125 << endl; Continue
}
9
Demo 1) simple float-function /7
Console
125
float cube(float x)
{ float y=x*x*x;
return y;
}
n
int main()
5
{
float n=5;
cout<<cube(n)<< endl;
} done
10
Demo 2
A Void Function
11
Demo 2) void-function /1
void printhello(float x)
{
for (int i=0; i<x; i++)
{
cout<< "Hi There !!!!" << endl;
}
}
int main()
{
printhello(2); START HERE
}
12
Demo 2) void-function /2
x
void printhello(float x)
2
{
for (int i=0; i<x; i++)
{
cout<< "Hi There !!!!" << endl;
}
}
int main()
{
printhello(2); COPY ARGUMENT TO PARAMETER
}
13
Demo 2) void-function /3
x
void printhello(float x)
2
{
CREATE LOCAL VAR
i 0
for (int i=0; i<x; i++)
{
cout<< "Hi There !!!!" << endl;
}
}
int main()
{
printhello(2);
}
14
Demo 2) void-function /4
x
void printhello(float x)
2
{Check Counter
i 0
for (int i=0; i<x; i++)
{
cout<< "Hi There !!!!" << endl;
}
}
int main()
{
printhello(2);
}
15
Demo 2) void-function /5
x
void printhello(float x)
2
{
i 0
for (int i=0; i<x; i++)
{
cout<< "Hi There !!!!" << endl;
}
Console
}
Hi There!!!
int main()
{
printhello(2);
}
16
Demo 2) void-function /6
x
void printhello(float x)
2
{ Add 1 to counter
i 0
for (int i=0; i<x; i++)
{
cout<< "Hi There !!!!" << endl;
}
Console
}
Hi There!!!
int main()
{
printhello(2);
}
17
Demo 2) void-function /7
x
void printhello(float x)
2
{Check Counter
i 1
for (int i=0; i<x; i++)
{
cout<< "Hi There !!!!" << endl;
}
Console
}
Hi There!!!
int main()
{
printhello(2);
}
18
Demo 2) void-function /8
x
void printhello(float x)
2
{
i 1
for (int i=0; i<x; i++)
{
cout<< "Hi There !!!!" << endl;
}
Console
}
Hi There!!!
Hi There!!!
int main()
{
printhello(2);
}
19
Demo 2) void-function /9
x
void printhello(float x)
2
{ Add 1 to counter
i 1
for (int i=0; i<x; i++)
{
cout<< "Hi There !!!!" << endl;
}
Console
}
Hi There!!!
Hi There!!!
int main()
{
printhello(2);
}
20
Demo 2) void-function /10
x
void printhello(float x)
2
{Check Counter
i 2
for (int i=0; i<x; i++)
{
cout<< "Hi There !!!!" << endl;
}
Console
}
Hi There!!!
Hi There!!!
int main()
{
printhello(2);
}
21
Demo 2) void-function /11
x
void printhello(float x)
2
{
i 2
for (int i=0; i<x; i++)
{
cout<< "Hi There !!!!" << endl;
}
} Done, go to line after
Console
call
Hi There!!!
Hi There!!!
int main()
{
printhello(2);
}
22
Demo 2) void-function /12
void printhello(float x)
{
for (int i=0; i<x; i++)
{
cout<< "Hi There !!!!" << endl;
}
Console
}
Hi There!!!
Hi There!!!
int main()
{
printhello(2);
}  Done
23
Demo 3
Function calling another
Function
24
Demo 3) Multiple functions /1
float calc(int l, int w)
{
return l * w;
}
void print_area(int len, int wid)
{
cout<< “Area is:“ <<calc(len,wid);
}
int main()
{
print_area(4,5); START HERE
}
25
Demo 3) Multiple functions /2
float calc(int l, int w)
{
return l * w;
}
len
void print_area(int len, int wid) 4
{
cout<< “Area is:“ <<calc(len,wid);
}
int main()
{
print_area(4,5); Copy args to params
}
26
wid
5
Demo 3) Multiple functions /3
float calc(int l, int w)
{
return l * w;
}
l
w
4
5
len
wid
void print_area(int len, int wid) 4
{
cout<< “Area is:“ <<calc(len,wid);
}
int main()
{
print_area(4,5);
}
27
5
Demo 3) Multiple functions /4
float calc(int l, int w)
{
return l * w; 
20
}
l
w
4
5
len
wid
void print_area(int len, int wid) 4
{
cout<< “Area is:“ <<calc(len,wid);
}
int main()
{
print_area(4,5);
}
28
5
Demo 3) Multiple functions /5
float calc(int l, int w)
{
return l * w;
20
}
l
w
4
5
len
wid
void print_area(int len, int wid) 4
{
cout<< “Area is:“ <<20;
}
int main()
{
print_area(4,5);
}
5
29
Demo 3) Multiple functions /6
float calc(int l, int w)
{
return l * w;
}
len
wid
5
void print_area(int len, int wid) 4
{
cout<< “Area is:“ <<20;
Console
}
Area is 20
int main()
{
print_area(4,5);
}
30
Demo 3) Multiple functions /7
float calc(int l, int w)
{
return l * w;
}
len
wid
5
void print_area(int len, int wid) 4
{
cout<< “Area is:“ <<calc(len,wid); Console
} Done, go to line after call Area is 20
int main()
{
print_area(4,5);
}
31
Demo 3) Multiple functions /8
float calc(int l, int w)
{
return l * w;
}
void print_area(int len, int wid)
{
cout<< “Area is:“ <<calc(len,wid); Console
}
Area is 20
int main()
{
print_area(4,5);
} Done
32