1 / 21

Arithmetic Expressions Function Calls Output

Arithmetic Expressions Function Calls Output. Chapter 3. Understanding and evaluating expressions. Calling functions and using parameters. Formatting output. Coming Up Next. Mult division mod (remainder). Addn subt. Precedence Rules. Expressions are made up of

asher
Download Presentation

Arithmetic Expressions Function Calls Output

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. Arithmetic ExpressionsFunction CallsOutput Chapter 3

  2. Understandingand evaluatingexpressions. Calling functions and using parameters Formatting output Coming Up Next

  3. Mult division mod (remainder) Addn subt Precedence Rules • Expressions are made up of • variables • operators • parentheses • Precedence similar to algebraic precedence • Highest is ( ) • * / % • + -

  4. Precedence Rules What is the output of this program ? IntegerDivision

  5. Integer values stored differently in memory than float values Variables declared as float can hold only float values This would “coerce” the program to change 5 to 5.0 and store it in num Happens implicitly Type Coercion

  6. Sometimes you wish to explicitly force the program to convert from one type to another Use the name of the type as if it is a function Type Casting

  7. When operands are of different data types Results in (implicit) type coercion What are the types of these expressions? Mixed Type Expressions

  8. A function is called (invoked) by using its name as a statement or command as a value (to be printed, assigned, etc.) Function Calls & Library Functions

  9. All functions have a parameter list The list may be empty, but the parentheses must be included in the call Function Calls and Library Functions

  10. Functions -- Two Versions • Value returning function • called as if it is a value • needs a return statement • Function to do a task • a void function • called as if it is a statement

  11. Value Returning Functions • Function call is used as a value (as in an expression) • Do not use as a separate statement • The function computes a value (or result) • The function returns exactly one result • A function written by a programmer should not to tasks (such as I/O)

  12. Library Functions • A standard library of often used functions exists • See partial listing, pg 66 of text • See Appendix C, pg A2 for larger listing

  13. Library Functions • What do the following functions do?Value returning or void? • exp(x) • atof(str) • strcpy(dest, source) • tolower(ch) • isdigit (ch)

  14. Void Functions • When declared, type is void • A function that does not return a value • return statement will be a syntax error • It’s purpose is to do a task • Called a procedure or subroutine in some languages • Invoked or called as a statement • not as a value

  15. Formatting the Output • Normally cout will just jam values together • uses scientific notation for float values • We need formatting capabilities • include blanks (blank lines, blank spaces) • right and left justify output in a specified number of columns • specify fieldwidth, precision for float output

  16. Caused paper roller to advance (rotate) one line Sent the typing head to the left margin Creating Blank Lines • cout << “whatever” << endl; • Stands for “end Line” • Sends a “carriage return” and “line feed”

  17. Inserting Blanks Within a Line • Used to separate output values • Include character space or string of spaces

  18. Manipulators • We have used endl as a “manipulator” • Behaves like a function • causes an action to occur • Looks like a data object • appears in midst of insertion << operations

  19. Note rounding Manipulators • To right justify output in specified number of spaces • To specify number of decimal digits to be output • Note required include and cout.setf

  20. Style -- Program Formatting • Header with /* comments */ about the program • Required documentation • // comments for clarification of identifiers • // comments with descriptions of program sections • Use white space, indenting

  21. Testing and Debugging Hints • Check precedence in expressions • Explicit type casts make program more clear • Both sides of assignment statements should be same type • Library functions require correct #include statement • Functions need correct number and order for parameter lists

More Related