CS 211:
Introduction to Computer Programming II
|
|
|
Instructor: Brian M. Dennis |
|
Teaching Assistants: |
|
Tom Lechner, Bin Lin |
|
http://www.cs.northwestern.edu/~bmd/cs211/ |
|
|
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"; |
|
} |
Procedure Invocation
|
|
|
#include <cmath> |
|
#include <iostream> |
|
using namespace std; |
|
|
|
main() { |
|
double two = 2.0; |
|
|
|
cout << "Root 2.0
is: " << sqrt(two) << "\n"; |
|
} |
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"; |
|
} |
Procedure Definition
|
|
|
#include <cmath> |
|
#include <iostream> |
|
using namespace std; |
|
|
|
main() { |
|
} |
|
|
|
// Coming from a Schemish
perspective |
|
square(x) { |
|
x * x; |
|
} |
Procedure Definition
|
|
|
#include <cmath> |
|
#include <iostream> |
|
using namespace std; |
|
|
|
main() { |
|
} |
|
|
|
double square(double x) { |
|
return x * x; |
|
} |
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; |
|
} |
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; |
|
} |
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; |
|
} |
Recursive Procedures
|
|
|
(define (fib n) |
|
(cond ((= n 0) 0) |
|
((= n 1) 1) |
|
(else |
|
(+ (fib (- n 2)) |
|
(fib (- n 1)))) |
|
)) |
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 |
|
} |
Dynamically Typed
Languages
Statically Typed
Languages
Scoping
Scoping
Scoping
Storage & Lifetime
Last In, First Out ==
Stack
|
|
|
|
C++ procedures |
|
strictly LIFO |
|
No nesting |
|
Automatic storage |
|
Is automatically reclaimed
after procedure returns |
That’s a Wrap
|
|
|
|
Take aways |
|
Procedure invocation |
|
Procedure definition |
|
Procedure declaration |
|
Variables |
|
Scoping rules |
|
Storage rules |