Lecture8
Lecture8
Programming
Steve James
Lecture 8
Recap
• Made the switch from Python to C++
• Upcoming assignment:
– Friday, 11 April
• Upcoming test:
– COMS1018A: 10 April, 14h15 in MSL
– COMS1022A: 9 April, 17h30 in MSL
General Form
• Python
if <exp>:
# do something
else:
# do something else
Must have round brackets
while (<exp>){
// statement 1
// statement 2
}
For Loop General Form
• Python
for i in range(start, stop, step):
# statement 1
# statement 2
FUNCTIONS
2 𝑓 𝑥 =
𝑓 𝑥 =𝑥
𝑥2
𝑠𝑞𝑢𝑎𝑟𝑒 𝑛𝑢𝑚 {
𝑛𝑢𝑚2
}
void greet(){
cout << "Hello!" << endl; greet();
}
if (y == 0){
cout << "y can't be 0" << endl;
return; //returns here
}
int main(){
return 0;
}
Function Declarations
• Like variables, we can declare functions
without defining what they actually do
• Function declaration (prototype) specifies
return type, function name and parameter list
• Function prototype and definition must agree
C++
int main(){
int x = 0;
cout << increment(x) << endl; //prints 1
cout << x << endl; //prints 0; function didn't change x
int y = 7;
cout << increment(y) << endl; //prints 8
cout << y << endl; //prints 7; function didn't change y
return 0;
}
increment
main Copy the value
x:0
x:0
x:1
stack frame
stack frame
main increment
Copy the value x:7
x:0
y:7 x:8
Pass-by-Value
• The value of the arguments are passed to the
function
• The original variables are thus unaffected
/*
x’s value: 2 x’s address: 0x7fffda1837d0
y’s value: 5 y’s address: 0x7fffda1837d4
z’s value: 7 z’s address: 0x7fffda1837d8
size of x: 4 bytes
size of y: 4 bytes
size of z: 8 bytes
*/
#include <iostream>
using namespace std;
int square(int x) {
cout << "In function square(), x is located at " << &x << endl;
return (x * x);
}
int cube(int x) {
cout << "In function cube() , x is located at " << &x << endl;
return (x * x * x);
}
int main() {
int x = 2, squared, cubed;
cout << "In main():\n"
<< " x is located at " << &x << endl
<< " squared is located at " << &squared << endl
<< " cubed is located at " << &cubed << endl
<< "Before square() and cube() function calls:\n"
<< " x = " << x << endl
<< " squared = " << squared << endl
<< " cubed = " << cubed << endl;
squared = square(x);
cubed = cube(x);
int main() {
int x = 2, squared, cubed;
cout << "In main():\n"
<<
<<
" x is located at " << &x << endl
" squared is located at " << &squared << endl
y and z get the address of the
<<
<<
" cubed is located at " << &cubed << endl
"Before squareCube() function calls:\n" variables passed to the function
<< " x = " << x << endl
<< " squared = " << squared << endl
<< " cubed = " << cubed << endl;
int i = 0;
int &r = i;
r = 9; //i becomes 9 also!
int main(void) {
int m = 0, n = 0;
What is the output?
cout << "Before callByValue() function call:"
<< " m = " << m << ", n = " << n << "\n";
callByValue(m, n);
callByReference(m, n);
callByValue(m, n);
callByReference(m, n);
return 0;
}
Early Abstraction
SCOPE
Scope
• Refers to visibility, accessibility and lifetime of
objects, functions
string name;
void greet(){
cout << "Hello " << name << endl;
}
int number;
int main(){
number = 2;
name = "Bob";
greet();
return 0;
}
int main(){
int x;
cin >> x;
if (x > 0){
}
else{
double q = i + 2;
cout << q << endl;
return 0;
}
Block Scope
• There are also nested blocks e.g. nested if-
statements/loops
• We can define our own blocks simply by using
{ ... } anywhere
• Variables carry into nested scope blocks
• Variable in inner block can have same name as
one in outer block
– Outer block variable will be hidden
Nested Scope
#include <iostream>
int main(){
{ //scope block
int p;
}
{
int x;
{ //nested block
int y; //both x, y exist here
}
//only x exists here
}