Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scope: Portion of the program in which the identifier can be referenced. Various types of scope, my examples are of block scope and global scope.

Similar presentations


Presentation on theme: "Scope: Portion of the program in which the identifier can be referenced. Various types of scope, my examples are of block scope and global scope."— Presentation transcript:

1 Scope: Portion of the program in which the identifier can be referenced. Various types of scope, my examples are of block scope and global scope.

2 #include int main(void) { int i = 10; printf(“i = %d\n”, i); if (i < 20) { int i = 10; i = i + 10; printf(“i = %d\n”, i); } printf(“i = %d\n”, i); } 1. i = 10 i = 20 i = 10 2. i = 10 i = 20 3. i = 10 i = 10 2. i = 10 i = 10 i = 20

3 #include int main(void) { int i = 10; printf(“i = %d\n”, i); { int i = 10; i = i + 10; printf(“i = %d\n”, i); } printf(“i = %d\n”, i); } 1. i = 10 i = 20 i = 10 2. i = 10 i = 20 3. i = 10 i = 10 2. i = 10 i = 10 i = 20

4 Output? #include int main(void) { int i = 10; printf(“i = %d\n”, i); { i = i + 10; printf(“i = %d\n”, i); } printf(“i = %d\n”, i); } 1. i = 10 i = 20 i = 10 2. i = 10 i = 20 3. i = 10 i = 10 2. i = 10 i = 10 i = 20

5 What is the output? #include int doSomething(int, int); int main(void) { int a = 5, b =2, c; c = doSomething(a,b); printf(“In Main: a=%d, b=%d, c=%d\n", a, b, c); return 0; } int doSomething(int a, int b) { a = 10; b = 12; printf("In Function: a=%d, b=%d\n", a, b); return a+b; } 1. In Function: a=10, b=12 In Main: a=5, b=2, c=22 2. In Function: a=10, b=12 In Main: a=10, b=12, c=22 3. In Function: a=5, b=2 In Main: a=5, b=2, c=22 4. In Function: a=10, b=12 In Main: a=5, b=2, c=7

6 What is the output? #include int doSomething(); int main(void) { int a = 5, b =2, c=0; printf("In Main: a=%d, b=%d, c=%d\n", a, b, c); c = doSomething(); return 0; } int doSomething() { int a = 10; int b = 12; a = a*b; printf("In Function: a=%d, b=%d\n", a, b); return a; } 1. In Main: a=5, b=2, c=0 In Function: a=120, b=12 In Function: a=1440, b=12 2. In Main: a=5, b=2, c=0 In Function: a=10, b=12 3. In Main: a=5, b=2, c=0 In Function: a=120, b=12 4. In Main: a=5, b=2, c=0 In Function: a=1440, b=12

7 What is the output? #include int doSomething(); int main(void) { int a = 5, b =2, c=0; printf("In Main: a=%d, b=%d, c=%d\n", a, b, c); c = doSomething(); return 0; } int doSomething() { static int a = 10; static int b = 12; a = a*b; printf("In Function: a=%d, b=%d\n", a, b); return a; } 1. In Main: a=5, b=2, c=0 In Function: a=120, b=12 In Function: a=1440, b=12 2. In Main: a=5, b=2, c=0 In Function: a=10, b=12 3. In Main: a=5, b=2, c=0 In Function: a=120, b=12 4. In Main: a=5, b=2, c=0 In Function: a=1440, b=12

8 What is the output? #include int doSomething(); int b = 10; int main(void) { int a = 5, b = 2, c=0; printf("In Main: a=%d, b=%d, c=%d\n", a, b, c); c = doSomething(); return 0; } int doSomething() { int a = 10; a = a*b; printf("In Function: a=%d, b=%d\n", a, b); return a; } 1. In Main: a=5, b=2, c=0 In Function: a=100, b=10 2. In Main: a=5, b=2, c=0 In Function: a=10, b=10 3. In Main: a=5, b=2, c=0 In Function: a=20, b=2 4. In Main: a=5, b=10, c=0 In Function: a=100, b=10

9 Function syntax reminder for your assignment /*************************************** Comment block for the program ****************************************/ /*************************************** Comment block for the function ****************************************/ int function_name (parameters); /* Function prototype */ int main(void) { main program stuff; } int function_name (parameters) { function stuff; }


Download ppt "Scope: Portion of the program in which the identifier can be referenced. Various types of scope, my examples are of block scope and global scope."

Similar presentations


Ads by Google