1 / 15

Modular Programming – User Defined Functions

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

zamora
Download Presentation

Modular Programming – User Defined Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Modular Programming – User Defined Functions

  2. Outline • Modular programming – user defined functions • Value returning functions • return statement CSCE 106

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. #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

  13. Argument Correspondence Corresponds to Formal Argument num1 num2 Actual Argument x y CSCE 106

  14. 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

  15. Next lecture we will continue the Modular Construct in C++ CSCE 106

More Related