Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.

Similar presentations


Presentation on theme: "Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement."— Presentation transcript:

1 Modular Programming – User Defined Functions

2 CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement

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

4 CSCE 1064 User-Defined Functions You can define two types of functions: 1.“void” function that does not return a value (and does not have a data type). 2.Value-returning function that has a data type. A function is defined by writing its heading and body. void drawCircle() { cout << “ * “ << endl; cout << “* *“ << endl; } Function name Function type Function statements

5 CSCE 1065 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().

6 CSCE 1066 #include using namespace std; void drawCircle(); void main() { cout << “-----------“ << endl; drawCircle(); cout << “-----------“ << endl; } void drawCircle() { cout << “ * “ << endl; cout << “* *“ << endl; } Function prototype Function heading Function call Execution

7 CSCE 1067 Functions With Arguments #include 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; } Formal parameter Parameter Actual parameter

8 CSCE 1068 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

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

10 CSCE 10610 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

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

12 CSCE 10612 #include 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

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

14 CSCE 10614 Tracing Exercise #include 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

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


Download ppt "Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement."

Similar presentations


Ads by Google