Presentation is loading. Please wait.

Presentation is loading. Please wait.

FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message ****...... ** Welcome.

Similar presentations


Presentation on theme: "FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message ****...... ** Welcome."— Presentation transcript:

1 FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message ****...... ** Welcome Home! ****...... **

2 int main() { Print2lines(); cout << “ Welcome Home!” << endl; Print4lines(); return 0; } where the 1 st function is void Print2lines() { cout << “****...... **” << endl; } The compiler needs to know that Print2lines and Print4lines are VOID functions with no parameter lists. Therefore, PROTOTYPING declarations are needed before they are called in ‘main’. After #include put void Print2lines(); // prototypes void Print4lines();

3 A better solution is to use one function that prints a variable number of lines indicated in the parameter list. #include void Printlines(int); // function prototype int main() { Printlines(2); cout << “ Welcome Home!” << endl; Printlines(4); return 0; } void Printlines(int numlines) { int count; for (count = 1; count <= numlines; count++) cout << “****...... **” << endl; }

4 Next Day Problem cin >> month >> day >> year; cout << “The day following” << month << ‘/’ << day << ‘/’ << year << “is”; UPDATE DATE cout << month << ‘/’ << day << ‘/’ << year; example 5 1 1966 The day following 5/1/1966 is 5/2/1966 Convert month to letters 5May

5 void writemonth (int) ; void writemonth (int month) { switch (month) { case 1: cout << “January”; break; case 2: cout << “Februrary”; break; : case 12: cout << “December”; break; }

6 The final output statement would be: cout << writemonth(month) << “ “ day << “, “ << year;

7 Value Parameters Used when the only role of the parameter(s) is to carry value(s) into a FUNCTION void writemeters(int feet, int inches) { int lengthins; float meters; lengthins = 12 * feet + inches; meters = lengthins / 39.39; cout << setw(6) << setprecision(2) << meters; } feet and inches are VALUE parameters Possible calls are writemeters(6, 4); writemeters(ft, ins); or ft+2, ins-1

8 Reference Parameters Used to carry value(s) into and back from a FUNCTION ex: to swap or exchange the values of two variables void swap(int& a, int& b) { int temp; // temporary variable temp = a; a = b; b = temp; } Possible calls swap(x, y); swap(m, n); where the actual parameters must be integer variables and must have already been assigned values

9 Parameter Passing e.g. writemeters(ft, ins) void writemeters(int feet, int inches) swap(x, y) void swap(int& a, int& b) Pass by VALUE A copy of the value of the parameter is passed into the FUNCTION Values in the calling FUNCTION are not changed Pass by REFERENCE (Address) The address of the parameter is passed into the FUNCTION All changes made inside the FUNCTION are permanent (i.e. values of x and y)

10 Parameter Passing Mechanisms void calc(int& p1, float p2); (in main) size = 3; distance = 4.5; calc(size, distance); ACTUAL PARAMETERS matched to FORMAL PARAMETERS on a 1 to 1 basis Note: Type and Order must be the same MAIN FUNCTION CALLED FUNCTION 3 calc(size, distance) void calc(int& p1, float p2) 4.5 4.5....

11 Function Declarations and Calls void TryThis(int, int&, float); int main() { int int1; int int2; float someFloat; TryThis(int1, int2, someFloat); } void TryThis(int param1, int& param2, float param3) { int i; float x; ( statements involving “params” ) }....


Download ppt "FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message ****...... ** Welcome."

Similar presentations


Ads by Google