1 / 11

Value Returning Functions

Value Returning Functions. Chapter 5. Value-Returning Functions. All the properties together make up the definition of the function. Information provided by the heading of the function. Information provided in the body of the function.

lundy
Télécharger la présentation

Value Returning 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. Value Returning Functions Chapter 5

  2. Value-Returning Functions All the properties together make up the definition of the function Information provided by the heading of the function Information provided in the body of the function For the compiler to use a function you have written, it must know when it finds that function call in your source code … 1. The name of the function 2. The number of parameters, if any 3. The data type of each parameter 4. Data type of the value returned by the function 5. The code required to do the calculation

  3. Value-Returning Functions • Consider a function for the area of a circle:double circleArea (double radius){ return 3.14159 * radius * radius;} • Note the • Heading (type, name, parameters) • The body • The return statement

  4. Parameters Parameters in the declaration : formal parameters Parameters in the call: actual parameters • Function definition syntax: functionType functionName (formal parameter list) { statements } • Call (invocation of the function) cout << "Enter radius for circle area -> ";cin >> radius;area = circleArea (radius);

  5. The return Statement • A value returning statement must have a return statement • Else a warning from the compiler • Also the function will actually return a "garbage" value • Syntax: return expression;

  6. Function Prototype • Recall that the compiler must know certain things about your function • When it finds a function call in your source code • Must know information in heading • Your program must have at least the heading of a function before it is invoked • Usually listed before function main ( )

  7. Alternative to Prototype • Also possible to place whole function definition before main () • This is a requirement in some languages (Pascal) • Either is acceptable in this class • There may be a standard required of programmers within a particular organization

  8. Flow of Control • First statement executed in any program is the first statement in the function main( ) • When another function called • logical control passed to first statement in that function’s body • program proceeds through sequence of statements within the function • When last statement of function executed • control returns to where function was called • control given to next command after the call

  9. Flow of Control void main ( ) { . . . print_summary (rpt_total); revenue = rpt_total * .72675; . . . } void print_summary (int total) { . . . cout << . . . } - first statement of main- function call, jumps to first statement of that function- proceeds through function- returns to next statement after call

  10. Example One void draw( int I, long L ); long sq( int s ); int main() { long y; int x; y = sq( x ); draw( x, y ); return(); } long sq( int s ) { return( s * s ); } void draw( int I, long L ) { /* Statements defining the draw function here */ return; }

  11. Example Two int max ( int a, int b ){return ( a > b ? a : b );} int main(){int nOne = 5;int nTwo = 7; printf_s(”\n%d is bigger\n”, max( nOne, nTwo )); }

More Related