210 likes | 362 Views
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
E N D
Understandingand evaluatingexpressions. Calling functions and using parameters Formatting output Coming Up Next
Mult division mod (remainder) Addn subt Precedence Rules • Expressions are made up of • variables • operators • parentheses • Precedence similar to algebraic precedence • Highest is ( ) • * / % • + -
Precedence Rules What is the output of this program ? IntegerDivision
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
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
When operands are of different data types Results in (implicit) type coercion What are the types of these expressions? Mixed Type Expressions
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
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
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
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)
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
Library Functions • What do the following functions do?Value returning or void? • exp(x) • atof(str) • strcpy(dest, source) • tolower(ch) • isdigit (ch)
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
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
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”
Inserting Blanks Within a Line • Used to separate output values • Include character space or string of spaces
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
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
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
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