Introduction to C and C++ Programming Tuesday, March 7, 1995 11 AM -- 5 PM Eastern Time Dr. H. E. Dunsmore Department of Computer Science Purdue University West Lafayette, Indiana Copyright 1995, H.E. Dunsmore, Purdue University CLASS SCHEDULE 11:00-12:00 (6) Functions 12:00-12:30 BREAK 12:30- 1:25 (7) Function Defaults, Recursion, and Overloading 1:25- 1:30 BREAK 1:30- 2:30 (8) Function Templates 2:30- 3:00 BREAK 3:00- 3:55 (9) Subscripted Variables and Pointers 3:55- 4:00 BREAK 4:00- 5:00 (10) Structures, Memory Management FUNCTIONS Functions Function Prototypes Call by value and by reference Functions Function is block of statements to perform specific task Functions eliminate need for duplicate statements -- same function can be invoked from multiple locations Use of functions enhances program readability Function format return_type function_name [(parameter_list)] { // declare local variables statement-1; ... statement-n; return [(return_value)]; } void main () { ... s = max (y, z); ... if (max (a,b) > ROOF) {...} ... } int max (int num1, int num2) { int part; part = num1; if (num2 > num1) { part = num2; } return (part); } Function header Function header is return_type function_name [(parameter_list)] Type of returned value Name of function Types and local names of parameters May have none, one, or more arguments (parameters) Can be parameters of different types float resolve (int a, float b, char c, float d) Do not duplicate function parameters in local list float resolve (int a, float b, char c, float d) { char e; //OK int a; // NO! float c; // NO! ... } Actual arguments and function parameters should be same number and types int max (int num1, int num2) {...} int a; int b; int c; float d; char e; ... a = max (b,c); //OK a = max (a,b,c); // wrong number a = max (c,d); //wrong type d = max (a,b); //OK If function returns no value, use return type void void print_heading (int store) { cout << "Total sales for store " << store << endl; return; } Return statement should be last statement of function Functions need not have any parameters void main () { ... print_heading (); ... } void print_heading () { cout << "Total sales for all stores" << endl; return; } Functions may not have any parameters, but may still return a value ... time = current_time (); ... int current_time () {...} Function Prototypes Recommended that main function be followed by all other functions in order that approximates top-down left-to-right function diagram Must use Function Prototype Gives function's name, type of return value, number and type of parameters float sum (float, float); void main () { float x; float y; float z; ... z = sum (x,y); } float sum (float num1, float num2) { return (num1 + num2); } Function prototype necessary in C++ any time function definition appears after call to it or if function defined in another file C++ compiler checks all calls to function for agreement with prototype float sum (float num1, float num2); Also is prototype for sum -- names may be included, if desired -- ignored by compiler If names used, need not be same as those used in function definition float sum (float number, float slumber); Call by value and by reference Reference is pointer to existing variable Reference is declared via the use of the "address-of" operator & int a; int& b = a; b is pointer to a a = 5; cout << b << endl; Major use of references -- function parameters and function return values Avoid inconvenience associated with pointers void increase (int); void main () { int thing = 4; increase (thing); cout << thing << endl; } void increase (int number) { ++number; } Without reference notation, increase gets call-by-value copy of parameter -- outputs 4 void increase (int&); void main () { int thing = 4; increase (thing); cout << thing << endl; } void increase (int& number) { ++number; } With reference notation, increase gets pointer to parameter -- outputs 5 With pointer notation... void increase (int*); void main () { int thing = 4; increase (&thing); cout << thing << endl; } void increase (int* number) { ++*number; } Use of references can reduce amount of memory used by a program -- especially if parameters are large data structures Return of reference... int& smaller (int&, int&); void main () { int x; int y; ... smaller (x, y) = 0; } int& smaller (int& num1, int& num2) { if (num1 < num2) return (num1); else return (num2); }