Presentation is loading. Please wait.

Presentation is loading. Please wait.

User-defined Functions

Similar presentations


Presentation on theme: "User-defined Functions"— Presentation transcript:

1 User-defined Functions
Divide and Conquer

2 Definition of a Module One way in One way out
Executes the same – all of the time (predictable) Highly “cohesive”

3 Function – Module by any other name
Principle module in C and C++ is a “function” Three basic parts to function use (order not significant) Definition – the programming Prototype – alert to the compiler Call – transfer of program control to the function

4 Function Definition The block of code that tells the computer what to do Has an identifying heading which contains a Return type – the type of information created as a result of the execution Function name – identifier so other parts of the program can reference (call) the block of code Function parameters – transfers copies of data from other parts of the program

5 Basic Syntax A function can only return ONE value
double div_two( double val1, double val2) { double result = 0.0; if( val2 != 0.0) result = val1/val2; return result; } // End of double div_two( double val1, double val2) A function can only return ONE value

6 The Heading

7 The Body Sends a copy of result back to the calling block

8 The Prototype Alerts the compiler that the specified programming block follows the main() Is essentially a copy of the function heading followed by a semicolon Example: double div_two( double val1, double val2); Placed prior to main()

9 The Call Transfers control of the program to the called function
Typically allows the copying of values from the calling code to the function (usually for processing)- these are generally called “arguments” Assigns any returned value to an appropriate variable Example: double ans = div_two( 23.5, 12.4); The values 23.5 and 12.4 are called “arguments of div_two” They could (and most often are) variables. These would be considered a form of input for the function div_two().

10 Four general categories of Functions – based on parameters and return type
Does not receive data and does not return information int transition( ) // Sometimes has a void return type { cout << “Press <ENTER> to Continue”; getline(cin, resp); return 0; } // End of int transition()

11 Receives parameters but does not return any values
void printData(string name, double balance) { cout << name << “has a balance of” <<fixed << setprecision(2) << balance << endl; }

12 Has no parameters but returns a value
double inputPrice( ) { double price = 0.0; string price_str = “XXXX”; cout << “Enter the wholesale price: “; getline(cin, price_str); while( (price_str.at(0) <=‘0’ )|| (price_str.at(0) >=‘9’)) cout << “Entry ERROR – Please enter a numeric value: “; } //End of - while( (price_str.at(0) <=‘0’ )|| (price_str.at(0) >=‘9’)) price = atof(price_str.data()); return price; }// End of - double inputPrice( )

13 Has parameters and a return value
double div_two( double val1, double val2) { double result = 0.0; if( val2 != 0.0) result = val1/val2; return result; } // End of double div_two( double val1, double val2)

14 Now Look at Project 2


Download ppt "User-defined Functions"

Similar presentations


Ads by Google