Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.

Similar presentations


Presentation on theme: "COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo."— Presentation transcript:

1 COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo

2 Lecture Review Function Prototypes  Define the function outlook at the beginning ( ); E.g. void drawRectange(int height, int width); double absValue(double num);

3 Lecture Review Function Implementation  Implement the function content ( ){ … } E.g. void drawRectange(int height, int width){ for (int i=0; i<height; i++){ for (int j=0; j<width; j++) cout<<“*”; cout<<endl; }

4 Lecture Review Return output  Give the result to back to the caller  Note, in the function prototype, we have define the return type : void, int, double, char, bool, etc… void = no return E.g. void drawRectange(int height, int width){…} double absValue(double num){ return abs;}

5 Lecture Review Function Call  Using the function ( ); = ( ); E.g. drawRectange(5, 5); x = absValue(10); h=4; w=4; x=-6; drawRectange(h, w); x = absValue(x);

6 Lecture Review Pass-by-value  Value of variable is passed as a parameter  Operations The value of parameters are copied to a storage and used inside the function The storage memory will be destroy after the termination of the function  Notes Its value will not be changed after returning from the function As the function only manipulates the copy of the variable  Syntax

7 Lecture Review Pass-by-value example void Increment(int Number) { Number = Number + 1; cout << "The parameter Number is: " << Number << endl; } int main() { int i = 10; Increment(i); cout << "The variable i is: "<< i <<endl; return 0; }

8 Lecture Review Pass-by-reference  Variable link is passed as a parameter  Notes The value of caller’s variable will also be changed  Syntax &

9 Lecture Review Pass-by-reference example void Increment(int& Number){ Number = Number + 1; cout << "The parameter Number: " << Number << endl; } int main(){ int i = 10; Increment(i); cout << "The variable i is: "<< i <<endl; return 0; }

10 Exercise – 1 (past paper) void Test (int&, int, int&); int main() { int d = 2; int e = 4; int f = 0; Test(d, e, f); cout << d << ' ' << e << ' ' << f << endl; d = 5; e = 7; f = 0; Test (f, e, d); cout << d << ' ' << e << ' '<< f << endl; return 0; } void Test( int& s, int t, int& x) { s = s + 2; t = 4 * s; x ++; cout << s << ' ' << t << ' ' << x << endl; }

11 Exercise - 1 (Answer) 4161 441 286 672

12 Exercise - 2 void Two(int&,int); int main() { int a=1, b=2; Two(b,a); cout << “Main: a=“ <<a <<“, b=“ <<b <<endl; return 0; } void Two(int& a, int b) { a+=2; b*=2; cout<<“Two: a=“ <<a <<“, b=“ <<b <<endl; }

13 Exercise – 2 (Answer) Two: a=4, b=2 Main: a=1, b=4

14 Lecture Review Writing program with functions  With prototypes declaration  Without prototypes declaration

15 Summary By the end of this lab, you should be able to:  Understand different part of function Prototypes Implementation Return output Function Call Parameter input  pass-by-value  pass-by-reference  Writing program with functions  Evaluate code segment with function call

16 Lab6 Use functions  Return Value  Pass by value  Pass by reference Download the template from website MUST NOT change the template


Download ppt "COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo."

Similar presentations


Ads by Google