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