And side effects this affects how we can use the

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: al • When out of digits, return the total • Simplification: how do we make the algebra Simplification: easier? Can we simplify the calculation? easier? 09/18/13 R. Smith - University of St Thomas - Minnesota 9 Functions and ‘side effects’ • This affects how we can use the functions • Well behaved functions have no side effects – “Pure” mathematical functions (sqrt, sum, etc.) – Functions that don’t do I/O – Functions that don’t modify array elements – We can use well behaved functions as much as we want • We can call them repeatedly without trouble • C expressions yield consistent results as long expressions as the functions they call have NO side effects as 09/18/13 R. Smith - University of St Thomas - Minnesota 10 Functions WITH side-effects • The ‘getline()’ function – It does input • The ‘overtime authorization’ function – We don’t want to call it twice because it collects input • The ‘printstub’ function – We would print the stub twice • The word length function – Modifies the word size count array (earlier version prints) 09/18/13 R. Smith - University of St Thomas - Minnesota 11 Other functions with side effects? • • • • • • • • getint() ? ch2int() ? printf() ? Withholding tax calculation? prompt() ? Histogram function? Function to find the max integer power of 2? Function to print a power of 2, given the Function exponent? exponent? 09/18/13 R. Smith - University of St Thomas - Minnesota 12 Checklist for Side Effects • • • Does it do input? Does it do output? Does it modify its arguments (arrays)? • Any YES to the above => side effects • OK to “look” at arrays, but not to change them 09/18/13 R. Smith - University of St Thomas - Minnesota 13 Assignment 11 • • • A calculator You type in 2 numbers You type in an operator ( + - * / % ) – Do ‘real’ divide and integer modulus • The result is printed and saved • Handling the numbers: – – – You always save – and work with – the last 2 numbers (A & B) The last number is always B – the second operand When you type in a new number, you copy B to A, and the new When number becomes the new B number – When you do an operation (*, -) it is A + B, or A – B. 09/18/13 R. Smith - University of St Thomas - Minnesota 14 Structure for Problem 11 • • • Function for conversion getline() brought in with #include Main: – Initialize a and b – Loop while non-null line is typed in • Read a line • See if it is a number – If so, convert it, move b to a, put new number in b • Or, is it a + – If so, add a and b • Or, is it a / – If so, divide a and b (double divide) • … etc. etc. 09/18/13 R. Smith - University of St Thomas - Minnesota 15 Creative Commons License This work is licensed under the Creative This Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/byvisit sa/3.0/us/ or send a letter to Creative sa/3.0/us/ Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. Francisco, 09/18/13 R. Smith - University of St Thomas - Minnesota 16...
View Full Document

This note was uploaded on 07/14/2013 for the course CISC 130 taught by Professor Smith during the Fall '08 term at St. Thomas.

Ask a homework question - tutors are online