Scope When we create variables and functions, they are limited in where they are visible and where...

15
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers we create are visible in the block they are created, and in any sub-blocks What blocks are we talking about The global block – the entire program Function blocks – the code following a function definition, contained in {} Condition blocks – the code inside the {} of an if or else Loop blocks – the code inside the {} of a for or while loop

Transcript of Scope When we create variables and functions, they are limited in where they are visible and where...

Page 1: Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.

Scope•When we create variables and functions, they are limited in where they are visible and where they can be referenced

•For the most part, the identifiers we create are visible in the block they are created, and in any sub-blocks

•What blocks are we talking about• The global block – the entire program• Function blocks – the code following a function

definition, contained in {}• Condition blocks – the code inside the {} of an if

or else• Loop blocks – the code inside the {} of a for or

while loop• Generic blocks – any code contained within {}

Page 2: Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.

Scope

...

int main(){

int x = 3;

fun();

...

}

void fun(){

cout << x; //This will cause problems

}

Page 3: Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.

Scope

int x = 0; //This is global

void fun();

int main(){

int x = 1; //this is local to main

cout << x << endl;

fun();

}

void fun(){

cout <<x << endl; //what do we see here?

}

Page 4: Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.

Design

• We can generally break big problems down into smaller ones

• Sometimes the smaller ones can be broken down again

• Often we break down to the point where the small problems are handled by a single function

• This allows us to design a program to solve larger problems through the use of simple to write functions

Page 5: Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.

Design

Example

“Write a program to read a text file containing names and output a new file with the names sorted and formatted”

What are the small problems (look for the verbs)?

- read - output - sort - format

Page 6: Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.

Design

So we’ll probably want functions that can

void readFile(string names[]);

void sortNames(string names[]);

void formatNames(string names[]);

void outputFile(string names[]);

Page 7: Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.

Design

Our main then simple can call the names in the order described by the problem

int main(){

string names[100];

readFile(names);

formatNames(names);

sortNames(names);

outputFile(names);

}

Page 8: Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.

Arrays

• Arrays allow us to make a collection of variables, each identified with the same label

• Each element is of the same type

• We refer to a specific element using an index

• Index are from 0 to NUM-1, where NUM is the number of elements in the array

int x[5]; // an array of 5 int

X[3] = 2; //sets the 4th element to 2

Page 9: Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.

Arrays

We can create multi-dimensional arrays

double y[3][7]; // a 3x7 matrix of doubles

again, elements are indexed in the same manner

y[2][5] = 1.5; //The element at 2,5, or row 2, col 5

Page 10: Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.

Arrays

Working with

Most commonly problems deal with moving through the array

- output every 2nd element

- add all elements

- find the maximum element

Page 11: Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.

Arrays

Arrays are special in that they are always passed by reference into functions (they just are)

This is true for 2-d arrays, but in this case we must always indicate the size of the 2nd dimension( the columns)

int fun(int a[], fload b[][5])

Care must always be taken to keep track of the size of the arrays

Page 12: Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.

For Loops

• The other main form of looping (and often used when dealing with arrays and indexing) are for loops

• For loops allow us to declare code that is executed before the loop, a condition, and code that is executed at the end of each loop

for (initialization code; condition; end of loop code)

{

code block;

}

Page 13: Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.

For Loops

The most common form of for loops utilizes a counting variable, a condition to stop the loop, and a count update

for(int i = 0; i < 5; i++)

{

cout << i << ‘,’; //We get 0,1,2,3,4,

}

Page 14: Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.

Strings

• String is a class (kind of like a type) which not only holds data, but also provides built in functions

• We declare objects (kinda like variables) which holds data strings, and allows us to call the built in functions to operate on the data

• Strings hold characters

string s = “I’m a string”;

Page 15: Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.

Strings

We can do all kinds of fun things with Stringsstring s = “I’m a string”;

cout << s; // output them

s.length(); // find out how many characters

int p = s.find(‘a’); //find the index of a character

s.erase(p,2); // remove characters

cout <<s; // What do we output now “I’m string”