cs31 lecture 17

Side effect functions 63 the memory life of a

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: ely surrounding loop/switch control flow the x goto will jump to anywhere in the code, including outside goto the function (but should never be used!!)
 the 61 Another example typedef unsigned int myuint_t; int mydivide(int x)
 { myuint_t tmp; myuint_t std::cin >> tmp; std::cin if (tmp == 0)
 { if std::cout << “Error: cannot divide by 0!” << std::endl; std::cout exit(1)
; exit(1)
; } if (tmp % 2 == 0 && x == 2)
 // tmp is even, divide by 2 if x >>= tmp; // bit-shift by tmp >>= else else x /= tmp; /= return x; return } 62 Some observations x If the argument is of type int, float, double, … (a scalar If type)
 then the argument is copied in a “new” variable in the function (same name, same input value)
 the x Changing its value inside the function does not change it Changing outside the function (it is a copy)
 outside x Precise rules about the data being input, output, and Precise modified by a function modified Pure vs. side-effect functions 63 The (memory) life of a function x A function has an internal state, which needs memory Arguments Local...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online