10. Variables, Scope, Lifetime

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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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; }
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online