# 10. Variables, Scope, Lifetime - ENGR 101 Names Variable...

This preview shows pages 1–5. Sign up to view the full content.

ENGR 101 1 Names: Variable, Scope, Lifetime ENGR 101, Lecture 10: 13 Oct 10 Announcements Project 4 due tonight, 9pm Project 5 out soon , 2-week (almost) assignment

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ENGR 101 2 Names Programming entails introduction and management of names for procedure : functions, operations for data : variables, files Today we discuss some key naming issues and principles Avoid Unnecessary Names double square( double x, double y ) { double result = x * y; return result ; } double square( double x, double y ) { return x * y; }
ENGR 101 3 When Does Data Need a Name? Manipulation across steps Use in multiple calculations int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Do we need a name for: x? y? Constants Variable : a named data object that may change in the course of computation Constant : named data that does not change C++ provides a declaration qualifier ( const ) to indicate that a new identifier represents a constant: const T identifier = expression ; const double speedOfLight = 3e8; const declaration must include initialization Attempt to change value considered an error

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ENGR 101 4 Why Introduce Named Constants? If intent is that identifier does not change, get compiler to help assure that it does not. if (turboCharged) speedOfLight = speedOfLight * 1.5; Why not just use expression in place of identifier ? double x; cout << "Enter distance: "; cin >> x; cout << "Will take " << x / 3e8 << " seconds." << endl; cout << "Enter time: "; cin >> x; cout << "Can travel " << x * 2.9979e8 << " meters." << endl; magic numbers const double sOL = 3e8; // speed of light, in m/s RPS with Switch int a, b; const
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 05/25/2011 for the course ENGR 101 taught by Professor Ringenberg during the Fall '07 term at University of Michigan.

### Page1 / 15

10. Variables, Scope, Lifetime - ENGR 101 Names Variable...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online