DOC PREVIEW
UIUC CS 101 - lect18

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

18-2 • Understand “Scope” of an Identifier • Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.1118-3 1. Problem Definition Write a function “swap” that has two integer input arguments. This function should swap the values of the variables. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = two integers Output= no value is returned to the calling function, but the values of the called variables should be swapped. 3. Develop Algorithm temp = a; a = b; b = temp;/* C Function to swap values. */ #include <stdio.h> void swap(int, int); /* prototype for swap */ void main(void) /* function header */ { int x = 1; int y = 2; swap(x,y); printf("x = %i , y = %i \n",x,y); /* prints x = 1 , y = 2 */ } void swap(int a , int b) { int temp = a; a = b; b = temp; return; }x 1000 Main Memory before call 1 The swap function will successfully swap the values of the variables a and b. But, since non-arrays are passed by value the swap of the values for a and b will not have any affect on the variables x and y. It is important to note that even if we had used the variable names x and y rather than a and b the swap function would still not work. See the next slide... y 2 Main Memory while executing swap, but before the return statement x 1000 1 y 2 a 2 b 1 3000 Why swap doesn’t work./* Modified function to swap values. */ #include <stdio.h> void swap(int, int); void main(void) { int x = 1; int y = 2; swap(x,y); printf("x = %i , y = %i \n",x,y); /* prints x = 1 , y = 2 */ } void swap(int x , int y) /* we now use x and y */ { int temp = x; x = y; y = temp; return; }x 1000 Main Memory before call 1 The C compiler will keep track of the variables x and y declared in main and the variables x and y declared in swap. The x in main is a different variable than the x in swap. Similarly for the y variable. The reason that the x variable in main is different than the x variable in swap is that two declarations of x occur in different blocks. That is the scope of the x in main is different than the scope of x in swap. One solution to the problem is to use pointer variables. We will discuss pointer variables in a future lecture. y 2 Main Memory while executing swap, but before the return statement x 1000 1 y 2 x 2 y 1 3000 Why the modified swap doesn’t work.18-8 An identifier is a name that is composed of a sequence of letters, digits, and the ‘_’ underscore character. Variable names are identifiers and so are function names and symbolic constant names. The scope of an identifier is the portion of the program in which the identifier is visible or accessible. Both variables and functions have two attributes: type and storage class. The scope of a variable or function is related to its storage class.18-9 •Local Variables - are declared within a block and cannot be referenced by outside the block; i.e., each function is assigned its own "local" storage areas. •Global Variables - declared outside any block and are known to all blocks in the same file . By default, global variables are "static” storage class. In the examples on the following slides, drawing a picture of memory is helpful in understanding how scoping works.18-10 int globalVar = 1; /* global variable */ int myFunction(int); /* function prototypes */ void main(void) /* local variable: result, in main */ { int result; result = myFunction(globalVar); /* call myFunction */ printf("%i",result); /* prints “2” */ printf("%i",globalVar); /* prints “2” */ printf("%i",x); /* compile error! x not known in main */ } int myFunction(int x) /* Local variable x */ { ++x; printf("%i",x); /* prints value “2” */ printf("%i",globalVar); /*prints value“1” */ ++globalVar; return x; }18-11 globalVar 1000 Main Memory before call to Myfunction 1 result ??? Main Memory while executing MyFunction, but before ++globalVar; 1000 1 x 2 3000 ??? globalVar result18-12 globalVar 1000 Main Memory after call to Myfunction 2 result 2 Main Memory while executing MyFunction, but before return(x); 1000 2 ??? x 2 3000 globalVar result18-13 int globalVar = 1; /* global variable */ int myFunction(int); /* function prototypes */ void main(void) { int result; result = myFunction(globalVar); /* call myFunction */ printf("%i",result); /* prints “2” */ } int myFunction(int x) /* Local variables: x, globalVar */ { int globalVar; /* new “local” variable */ printf("%i\n",globalVar);/* prints ??? */ return x + 1; }18-14 int myFunction(int); /* function prototypes */ void main(void) /* local variables: x,result in main */ { int result, x = 2; result = myFunction(x); /* call myFunction */ printf("%i",result); /* prints “3” */ printf("%i",x); /* prints “2” WHY??? */ } int myFunction(int x) /* Local variable: x */ { x = x + 1; printf("%i\n",x); /* prints “3” */ return x; }#include <stdio.h> int x; /* x is a global variable */ int y; /* y is a global variable */ void swap(int, int); /* prototype for swap */ void main(void) { x = 1; /* x and y are not declared here!!! */ y = 2; swap(x,y); printf("x = %i , y = %i \n",x,y); /* prints x = 1,y = 2 */ } void swap(int x , int y) { int temp = x; x = y; y = temp; return; }18-16 A function can be called by any other function, provided that either the function definition or its prototype is in the same file as the calling function and precedes the function call. If no prototype is specified for a function, its header serves as the function prototype. Note: Functions cannot be defined within each other18-17 - Determines the storage duration and scope of identifiers, and also linkage between files. auto - creates storage for variables when the block that declares them is entered, and deletes the storage when the block is exited. Local variables have "auto" storage by default. e.g.,typing auto float a, b, c; is equivalent to typing float a, b, c;18-18 static - creates and initializes storage for variables when the program begins execution. Storage continues to exist until execution terminates. If an initial value is not explicitly stated, a static variable is initialized to 0. We can retain values of local variables by declaring them to be static. In the following example, the static variable sum is initialized to 1.


View Full Document

UIUC CS 101 - lect18

Documents in this Course
Notes

Notes

114 pages

lect2223

lect2223

35 pages

lect2223

lect2223

35 pages

lect1920

lect1920

23 pages

lect1920

lect1920

23 pages

lect1617

lect1617

25 pages

lect1617

lect1617

25 pages

lect1314

lect1314

34 pages

lect1314

lect1314

34 pages

lect0607

lect0607

25 pages

lect0607

lect0607

25 pages

lect25

lect25

31 pages

lect24

lect24

15 pages

lect21

lect21

25 pages

lect21

lect21

25 pages

lect18

lect18

22 pages

lect15

lect15

37 pages

lect15

lect15

37 pages

lect12

lect12

31 pages

lect12

lect12

31 pages

lect11

lect11

28 pages

lect11

lect11

28 pages

lect10

lect10

28 pages

lect09

lect09

24 pages

lect09

lect09

6 pages

lect08

lect08

23 pages

lect08

lect08

23 pages

lect05

lect05

26 pages

lect05

lect05

26 pages

lect04

lect04

36 pages

lect04

lect04

36 pages

lect03

lect03

26 pages

lect03

lect03

26 pages

lect02

lect02

36 pages

lect02

lect02

36 pages

lect01

lect01

32 pages

lect01

lect01

32 pages

lect00

lect00

23 pages

lect00

lect00

23 pages

Load more
Download lect18
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view lect18 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view lect18 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?