variable scope - binghamtontbarten1/cs211_fall_2015/lectures... · 2015-11-13 · binghamton...

18
Binghamton University CS-211 Fall 2015 Variable Scope 1

Upload: others

Post on 12-Apr-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Variable Scope - Binghamtontbarten1/CS211_Fall_2015/lectures... · 2015-11-13 · Binghamton University CS-211 Fall 2015 Scope The places in your code that can read and/or write a

Binghamton

University

CS-211

Fall 2015

Binghamton

University

CS-211

Fall 2015

Variable Scope

1

Page 2: Variable Scope - Binghamtontbarten1/CS211_Fall_2015/lectures... · 2015-11-13 · Binghamton University CS-211 Fall 2015 Scope The places in your code that can read and/or write a

Binghamton

University

CS-211

Fall 2015

Binghamton

University

CS-211

Fall 2015

2

Page 3: Variable Scope - Binghamtontbarten1/CS211_Fall_2015/lectures... · 2015-11-13 · Binghamton University CS-211 Fall 2015 Scope The places in your code that can read and/or write a

Binghamton

University

CS-211

Fall 2015

Binghamton

University

CS-211

Fall 2015

Scope

The places in your code that can read and/or write a variable.

• Scope starts at the location where you declare the variable

• There may be holes in the scope!

• Scope ends at the end of the block in which the declare occurs• Usually, the function in which the variable was declared

3

Page 4: Variable Scope - Binghamtontbarten1/CS211_Fall_2015/lectures... · 2015-11-13 · Binghamton University CS-211 Fall 2015 Scope The places in your code that can read and/or write a

Binghamton

University

CS-211

Fall 2015

Binghamton

University

CS-211

Fall 2015

Simple Example

#include <stdio.h>

int main(int argc, char **argv) {

int j;

for(j=0; j<argc; j++) {

printf(“Argument %d = %s\n”,j,argv[j]);

}

return 0;

}

4

Block in which j is declared

Scope of j

Page 5: Variable Scope - Binghamtontbarten1/CS211_Fall_2015/lectures... · 2015-11-13 · Binghamton University CS-211 Fall 2015 Scope The places in your code that can read and/or write a

Binghamton

University

CS-211

Fall 2015

Binghamton

University

CS-211

Fall 2015

“Block” doesn’t have to be a function!

• You can define a variable inside a sub-block of a function

5

Page 6: Variable Scope - Binghamtontbarten1/CS211_Fall_2015/lectures... · 2015-11-13 · Binghamton University CS-211 Fall 2015 Scope The places in your code that can read and/or write a

Binghamton

University

CS-211

Fall 2015

Binghamton

University

CS-211

Fall 2015

Internal Example

#include <stdio.h>

int main(int argc, char **argv) {

int j;

for(j=0; j<argc; j++) {

int k=j+1;

printf(“Argument %d = %s\n”,k,argv[j]);

}

return 0;

}

6

Block in which k is declared

Scope of k

Page 7: Variable Scope - Binghamtontbarten1/CS211_Fall_2015/lectures... · 2015-11-13 · Binghamton University CS-211 Fall 2015 Scope The places in your code that can read and/or write a

Binghamton

University

CS-211

Fall 2015

Binghamton

University

CS-211

Fall 2015

Global Variable

• Declared outside of a block

• Scope is from declaration to the end of the C file!

7

Page 8: Variable Scope - Binghamtontbarten1/CS211_Fall_2015/lectures... · 2015-11-13 · Binghamton University CS-211 Fall 2015 Scope The places in your code that can read and/or write a

Binghamton

University

CS-211

Fall 2015

Binghamton

University

CS-211

Fall 2015

Global Example

#include <stdio.h>

int nc=0;

int myfunc(int n) { nc++; return n; }

int main(int argc, char **argv) {

int j;

for(j=0; j<argc; j++) myFunc(2);

printf(“myfunc called %d times\n”,nc);

return 0;

}

8

Block in which nc is declared

Scope of nc

Page 9: Variable Scope - Binghamtontbarten1/CS211_Fall_2015/lectures... · 2015-11-13 · Binghamton University CS-211 Fall 2015 Scope The places in your code that can read and/or write a

Binghamton

University

CS-211

Fall 2015

Binghamton

University

CS-211

Fall 2015

Global Variable Pros & Cons

Advantages

• Simple and intuitive

• Enables functions to communicate data with each other

• Remembers between function calls as well as within function calls

Disadvantages

• Increases the “outside” information a function needs to be aware of (binding)

• Prevents re-use of functions

• Remembers between function calls as well as within function calls

9

Page 10: Variable Scope - Binghamtontbarten1/CS211_Fall_2015/lectures... · 2015-11-13 · Binghamton University CS-211 Fall 2015 Scope The places in your code that can read and/or write a

Binghamton

University

CS-211

Fall 2015

Binghamton

University

CS-211

Fall 2015

Holes in Scopes

• If the same variable is declared inside a sub-block, the internal declaration temporarily replaces the external definition!

10

Page 11: Variable Scope - Binghamtontbarten1/CS211_Fall_2015/lectures... · 2015-11-13 · Binghamton University CS-211 Fall 2015 Scope The places in your code that can read and/or write a

Binghamton

University

CS-211

Fall 2015

Binghamton

University

CS-211

Fall 2015

Example Scope Hole

{ int i; int j=7;

for (i=0; i<3; i++) {

int j=i+1;

printf(“j=%d\n”,j);

}

printf(“j=%d\n”);

}

11

Scope of outside j

Scope of inside j(Hole for outside j)

j=1j=2j=3j=7

Page 12: Variable Scope - Binghamtontbarten1/CS211_Fall_2015/lectures... · 2015-11-13 · Binghamton University CS-211 Fall 2015 Scope The places in your code that can read and/or write a

Binghamton

University

CS-211

Fall 2015

Binghamton

University

CS-211

Fall 2015

Variable Class

•Automatic •Created/Initialized on entry to block•Destroyed on exit from block

•Static•Created/Initialized when program starts•Destroyed when program ends

12

Page 13: Variable Scope - Binghamtontbarten1/CS211_Fall_2015/lectures... · 2015-11-13 · Binghamton University CS-211 Fall 2015 Scope The places in your code that can read and/or write a

Binghamton

University

CS-211

Fall 2015

Binghamton

University

CS-211

Fall 2015

Default Class

• Function/Block Variables are automatic• Created/Initialized on entry to that function/block

• Deleted when that function/block ends

• Global Variables are Static• Created/Initialized when the program starts

• Deleted when the program ends

• “Automatic” has no meaning!

13

Page 14: Variable Scope - Binghamtontbarten1/CS211_Fall_2015/lectures... · 2015-11-13 · Binghamton University CS-211 Fall 2015 Scope The places in your code that can read and/or write a

Binghamton

University

CS-211

Fall 2015

Binghamton

University

CS-211

Fall 2015

Local Static Variables

• We can specify “static” keyword in a declaration

• Implies variable is created when program starts, deleted when program ends

• Does NOT mean that the scope is global!!!!• Scope is still within that function

14

Page 15: Variable Scope - Binghamtontbarten1/CS211_Fall_2015/lectures... · 2015-11-13 · Binghamton University CS-211 Fall 2015 Scope The places in your code that can read and/or write a

Binghamton

University

CS-211

Fall 2015

Binghamton

University

CS-211

Fall 2015

Example Local Static

char * flipflop() {

static int flip=1;

if (flip) { flip=0; return “flip”; }

else { flip=1; return “flop”; }

}

for(i=0;i<8;i++) printf(“%s “,flipflop());

flip flop flip flop flip flop flip flop

15

Page 16: Variable Scope - Binghamtontbarten1/CS211_Fall_2015/lectures... · 2015-11-13 · Binghamton University CS-211 Fall 2015 Scope The places in your code that can read and/or write a

Binghamton

University

CS-211

Fall 2015

Binghamton

University

CS-211

Fall 2015

BAD FORM : Pseudo-Globals

• It is legal in C to nest a function inside another function

• This allows the variables in the outside function to be visible (in scope) for the inside function

• C Coders frown on this practice!• Nested functions have other complications

• Nested functions cannot be re-used

• It’s ugly and confusing

16

Page 17: Variable Scope - Binghamtontbarten1/CS211_Fall_2015/lectures... · 2015-11-13 · Binghamton University CS-211 Fall 2015 Scope The places in your code that can read and/or write a

Binghamton

University

CS-211

Fall 2015

Binghamton

University

CS-211

Fall 2015

Example Nested Functions

int main(int argc, char **argv) {

char firstArgLetter(int i) {

return argv[i][0];

}

int j;

for(j=0;j<argc;j++) printf(“Arg start: %c\n”,

firstArgLetter(j));

return 0;

}

17

Page 18: Variable Scope - Binghamtontbarten1/CS211_Fall_2015/lectures... · 2015-11-13 · Binghamton University CS-211 Fall 2015 Scope The places in your code that can read and/or write a

Binghamton

University

CS-211

Fall 2015

Binghamton

University

CS-211

Fall 2015

Resources

• Programming in C, Chapter 7

• Wikipedia Variable https://en.wikipedia.org/wiki/Variable_(computer_science)

• Scope Tutorial http://www.tutorialspoint.com/cprogramming/c_scope_rules.htm

18