150 likes | 278 Vues
Modular Programming – User Defined Functions. Outline. Modular programming – user defined functions Value returning functions return statement. General Functions’ Guidelines. To use any function in general, you need to: include the correct header file know the name of the function
E N D
Outline • Modular programming – user defined functions • Value returning functions • return statement CSCE 106
General Functions’ Guidelines To use any function in general, you need to: • include the correct header file • know the name of the function • know the number of parameters/arguments, if any • know the data type of each parameter • know the data type of the value computed by the function, called the type of the function A value-returning function is either used in an assignment statement or in an output statement. CSCE 106
User-Defined Functions You can define two types of functions: • “void” function that does not return a value (and does not have a data type). • Value-returning function that has a data type. A function is defined by writing its heading and body. void drawCircle() { cout<< “ * “ <<endl; cout<< “* *“ << endl; cout<< “ * * “ <<endl; } Function name Function type Function statements CSCE 106
User-Defined Functions (cont’d) • You need to declare function prototypes (functions’ headings without the body of the functions) before your main() function. • Like standard functions, you need to call the function from your main()to activate it. • The function definition should come after your main(). CSCE 106
Function prototype #include <iostream> using namespace std; void drawCircle(); void main() { cout << “-----------“ << endl; drawCircle(); cout << “-----------“ << endl; } void drawCircle() { cout << “ * “ << endl; cout << “* *“ << endl; cout << “ * * “ << endl; } Function heading Execution Function call Function heading CSCE 106
Functions With Arguments • #include <iostream> • using namespace std; • void drawCircleChar(char symbol); • void main() • { • cout << “-----------“ << endl; • drawCircleChar(‘*’); • cout << “-----------“ << endl; • drawCircleChar(‘o’); • } • void drawCircleChar(char symbol) • { • cout << “ “ << symbol << endl; • cout << symbol << “ “ << symbol << endl; • cout << “ “ << symbol << “ “ << symbol << endl; • } Parameter Actual parameter Formal parameter CSCE 106
Value-Returning Functions The syntax for defining a value-returning function is: functionType functionName(formal parameter list) { statements } • functionType – data type of the value returned by the function • formal parameter - a variable declared in the function heading • actual parameter - a variable or expression listed in a call to a function CSCE 106
Value-Returning Functions (cont’d) • The syntax of the formal parameter list is: dataType identifier, dataType identifier, ... • The syntax for a function call is: functionName(actual parameter list) • The syntax for the actual parameter list is: expression or variable,expression or variable, ... • There is a one-to-one correspondence between actual and formal parameters • The formal parameter list can be empty. CSCE 106
The return Statement • Once the function computes the value, the function returns this value via the return statement • The syntax of the return statement is: return expression or variable; • When a return statement executes in a function, the function immediately terminates and the control goes back to the caller • When a return statement executes in the function main(), the program terminates CSCE 106
Exercise Write a complete C++ program, for an algorithm that calculates the average of two numbers by the use of a value returning function (computeAverage). The main function should input the two numbers, as well as outputting the average. CSCE 106
#include <iostream> • using namespace std; • float computeAverage(float num1, float num2); • int main() • { • float x, y, av; • cout << “Please enter two numbers:“ << endl; • cin >> x >> y; • av = computeAverage(x,y); • cout << “The average of the two numbers is:“ << av << endl; • return 0; • } • float computeAverage(float num1, float num2) // 2 parameters • { // Compute the average of the data. • return ((num1 + num2) / 2.0); • } // end computeAverage function Actual parameters Formal parameters CSCE 106
Argument Correspondence Corresponds to Formal Argument num1 num2 Actual Argument x y CSCE 106
Tracing Exercise #include <iostream> using namespace std; int Blend( int red, int green ); // prototype void main() { int red = 5, blue; blue = Blend(3, red + 1); cout << red << ' ' << blue << '\n'; blue = Blend(blue, red); cout << red << ' ' << blue << '\n'; } int Blend( int red, int green ) { int yellow; cout << “enter Blend “ << red <<‘ ‘ << green << ‘\n’; yellow = red + green; cout << “leave Blend “ << red <<‘ ‘ << green << ‘\n’; return (yellow + 1); } Output: enter Blend 3 6 leave Blend 3 6 5 10 enter Blend 10 5 leave Blend 10 5 5 16 CSCE 106
Next lecture we will continue the Modular Construct in C++ CSCE 106