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
15
Embed
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.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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 {}
Scope
...
int main(){
int x = 3;
fun();
...
}
void fun(){
cout << x; //This will cause problems
}
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?
}
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
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
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[]);
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);
}
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
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
Arrays
Working with
Most commonly problems deal with moving through the array
- output every 2nd element
- add all elements
- find the maximum element
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
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;
}
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,
}
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”;
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