Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arithmetic Expressions Function Calls Output

Similar presentations


Presentation on theme: "Arithmetic Expressions Function Calls Output"— Presentation transcript:

1 Arithmetic Expressions Function Calls Output
Chapter 3

2 Coming Up Next Understanding and evaluating expressions.
Formatting output Calling functions and using parameters

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

4 Precedence Rules What is the output of this program ? Integer Division

5 Type Coercion 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

6 Type Casting 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

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

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

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

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 Creating Blank Lines cout << “whatever” << endl;
Stands for “end Line” Sends a “carriage return” and “line feed” Caused paper roller to advance (rotate) one line Sent the typing head to the left margin

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 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 Note rounding

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


Download ppt "Arithmetic Expressions Function Calls Output"

Similar presentations


Ads by Google