func2 - Computer Programming TA C162 Next Functions...

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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 .
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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???
Background image of page 6
7 Computer Programming TA C162 Second Semester 2008-2009 Example:
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
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 Right Arrow Icon
Ask a homework question - tutors are online