func2 - Computer Programming TA C162 Next Functions...

This preview shows pages 1–8. Sign up to view the full content.

1 Computer Programming TA C162 Second Semester 2008-2009 Next… Functions Variable Scope and Visibility Multifunction Program Example Calling a Function from an Address value Passing Array to a Function

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Computer Programming TA C162 Second Semester 2008-2009 Variable storage classes The class of a variable may be either permanent or temporary. Global variables are always permanent. They are created and initialized before the program starts and remain until it terminates. Temporary variables are allocated from a section of memory called the stack at the beginning of the block. Each time the block is entered, the temporary variables are initialized. Local variables are temporary unless they are declared static .
3 Computer Programming TA C162 Second Semester 2008-2009 Scope, Visibility and Lifetime of Variables Scope determines over what region of the program a variable is active. Lifetime refers to the period during which a variable retains a given value. Visibility refers to the accessibility of a variable from the memory. Inference: Visibility of the variable is within the scope only

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Computer Programming TA C162 Second Semester 2008-2009 How automatic variables work? void fun1(void) { int x=25; fun2(); printf(“%d\n”,x); } void fun2(void) { int x=5; printf(“%d\n”,x); } void main() { int x=125; fun1(); printf(“%d\n”,x); } Execution completed Output: 5 25 125
5 Computer Programming TA C162 Second Semester 2008-2009 How global variables work? int val ; int fun1(void) { val += 5; return val ; } int fun2(void) { int val =0; return val ; } int fun3(void) { val +=5; return val; } void main() { val = 5; printf(“val= %d\n”,val); printf(“val= %d\n”,fun1()); printf(“val= %d\n”,fun2()); printf(“val= %d\n”,fun3()); } Output: val=5 val=10 val =0 val=15

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 Computer Programming TA C162 Second Semester 2008-2009 Exchanging values of two variables int x = 10,y = 5; void Exchange(void) { int temp; temp = x; x = y; y = temp; printf(“Values inside Exchange\n”); printf(“x = %d \n y = %d”, x, y); } void main() { printf(“Values before calling Exchange\n”); printf(“x = %d \n y = %d”, x, y); printf(“Values after calling Exchange\n”); Exchange(); printf(“x = %d \n y = %d”, x, y); } Output???
7 Computer Programming TA C162 Second Semester 2008-2009 Example:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/14/2010 for the course CS SS ZG653 taught by Professor Shanta during the Spring '09 term at Birla Institute of Technology & Science.

Page1 / 24

func2 - Computer Programming TA C162 Next Functions...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online