Notes
Slide Show
Outline
1
CS 211:
Introduction to Computer Programming II
  • Instructor: Brian M. Dennis
  • Teaching Assistants:
  • Tom Lechner, Bin Lin
  • http://www.cs.northwestern.edu/~bmd/cs211/


2
Our Last Valid Program
  • #include <iostream>
  • using namespace std;


  • main() {
  •  int integer_temp =  1 + 1;
  •  cout << "1 + 1 = " << integer_temp << "\n";


  •  float floating_point_temp;
  •  floating_point_temp = 1.414f;
  •  cout << "Root 2 = " << floating_point_temp
  •       << "\n";
  • }
3
Procedure Invocation
  • #include <cmath>
  • #include <iostream>
  • using namespace std;


  • main() {
  • double two = 2.0;
  • cout << "Root 2.0 is: " << sqrt(two) << "\n";
  • }
4
Procedure Invocation
  • #include <cmath>
  • #include <iostream>
  • using namespace std;


  • main() {
  • double two = 2.0;
  • cout << "Root 2.0 squared is: "
  •        << sqrt(two) * sqrt(two) << "\n";
  • }
5
Procedure Definition
  • #include <cmath>
  • #include <iostream>
  • using namespace std;


  • main() {
  • }


  • // Coming from a Schemish perspective
  • square(x) {
  • x * x;
  • }
6
Procedure Definition
  • #include <cmath>
  • #include <iostream>
  • using namespace std;


  • main() {
  • }


  • double square(double x) {
  • return x * x;
  • }
7
Procedure Definition
  • #include <cmath>
  • #include <iostream>
  • using namespace std;


  • main() {
  • cout << "2.11 squared is: " << square(2.11)
  •      << "\n";
  • }


  • double square(double x) {
  • return x * x;
  • }
8
Procedure Definition
  • #include <cmath>
  • #include <iostream>
  • using namespace std;
  • double square(double x);
  • main() {
  • cout << "2.11 squared is: " << square(2.11)
  •      << "\n";
  • }


  • double square(double x) {
  • return x * x;
  • }
9
Procedure Definition
  • #include <cmath>
  • #include <iostream>
  • using namespace std;
  • double square(double x);
  • int main() {
  • double foo = 2.11;
  • cout << "2.11 squared is: " << square(foo)
  •      << "\n";
  • }


  • double square(double x) {
  • return x * x;
  • }
10
Recursive Procedures
  • (define (fib n)
  • (cond ((= n 0) 0)
  •    ((= n 1) 1)
  •          (else
  •           (+ (fib (- n 2))
  •              (fib (- n 1))))
  •           ))
11
Recursive Procedures
  • #include <iostream>
  • using namespace std;


  • int fib(int n) {
  • if (n == 0) return 0;
  • if (n == 1) return 1;
  • return fib(n-2) + fib(n-1);
  • }


  • int main() {
  • // call fib
  • }
12
Dynamically Typed Languages
13
Statically Typed Languages
14
Scoping
15
Scoping
16
Scoping
17
Storage & Lifetime
18
Last In, First Out == Stack
  • C++ procedures
    • strictly LIFO
    • No nesting
  • Automatic storage
    • Is automatically reclaimed after procedure returns
19
That’s a Wrap
  • Take aways
    • Procedure invocation
    • Procedure definition
    • Procedure declaration
  • Variables
    • Scoping rules
    • Storage rules