1 / 18

Functions 2

Functions 2. Scope Automatic(Local) and Static Variables. Pass by Value. Passing a copy of the value as an argument Parameters receive a distinct copy of the caller's arguments, as if the parameters were assigned from the arguments

dewey
Download Presentation

Functions 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions 2 Scope Automatic(Local) and Static Variables

  2. Pass by Value • Passing a copy of the value as an argument • Parameters receive a distinct copy of the caller's arguments, as if the parameters were assigned from the arguments • Changes made to parameters have no effect on the caller’s arguments • Examples: h = 3; w = 4; area = CalcArea(h, w); 3, 4 • double CalcArea(double height, double width) • { …; • } height = 3, width = 4

  3. Local variables Local variables Local Variables – I • What is local variables • Variables declared within a function • Example: double CalcMax(double a[10]) { int i; double maxValue; …; } int main() { int i; double a[10] double maxValue; maxValue = CalcMax(a) ; }

  4. Local Variables – II • Why need local variables? • To store temporary information • Values are private to current function • Can't be accessed by other functions • Unless they are passed as arguments • Parameters are local variables

  5. #include <stdio.h> void foo(int x); int main() { int x = 3, y = 2; printf("x1: %i\t\ty1: %i\n", x, y); foo(x); printf("x4: %i\t\ty4: %i\n", x, y); printf("z: %i\n", z); return 0; } void foo(int x) { int y = 8, z = 12; printf("x2: %i\t\ty2: %i\t\tz2: %i\n", x, y, z); x = 7; printf("x3: %i\t\ty3: %i\t\tz3: %i\n", x, y, z); } Local Variables - 3 x1: 3 y1: 2 x2: 3 y2: 8 z2: 12 x3: 7 y3: 8 z3: 12 x4: 3 y4: 2 Syntax Error

  6. Array as parameters • Pass entire array to a function • Example: • int minimum(int values[100]) • int minimum(int values[ ], int numberOfElements) • Only the location of array is passed to the function • Not the value of all elements in the array • Changes made to the parameter in the called function will be reflected in the argument when the function returns

  7. Array as parameters – cont. • Omitting size of array • For one-dimension array • int minimum(int array[ ]) • For multi-dimensional array • int minimum(int matrix[ ][10]) • int minimum(int matrix[10][])

  8. Array as Parameters – Example 1 #include <stdio.h> void foo(int x[10]); int main() { int x[10] = {[4] = 5, [3]= 4,[2] = 2}; printf("x1: %i\n", x[0]); foo(x); printf("x4: %i\n", x[0]); return 0; } void foo(int x[10]) { printf("x2: %i\n", x[0]); x[0] = 7; printf("x3: %i\n", x[0]); } x1: 0 x2: 0 x3: 7 x4: 7

  9. Array as Parameters – Example 2 #include <stdio.h> void foo(int x[]); int main() { int x[10] = {[4] = 5, [3]= 4,[2] = 2}; printf("x1: %i\n", x[0]); foo(x); printf("x4: %i\n", x[0]); return 0; } void foo(int x[]) { printf("x2: %i\n", x[0]); x[0] = 7; printf("x3: %i\n", x[0]); } x1: 0 x2: 0 x3: 7 x4: 7

  10. Array as Parameters – Example 3 #include <stdio.h> void foo(int x[10][2]); int main() { int x[10][2] = {[4][0] = 5, [3][1]= 4,[2][0] = 2}; printf("x1: %i\n", x[0][0]); foo(x); printf("x4: %i\n", x[0][0]); return 0; } void foo(int x[10][2]) { printf("x2: %i\n", x[0][0]); x[0][0] = 7; printf("x3: %i\n", x[0][0]); } x1: 0 x2: 0 x3: 7 x4: 7

  11. Array as Parameters – Example 1 #include <stdio.h> void foo(int x[][2]); int main() { int x[10][2] = {[4] = 5, [3]= 4,[2] = 2}; printf("x1: %i\n", x[0][0]); foo(x); printf("x4: %i\n", x[0][0]); return 0; } void foo(int x[][2]) { printf("x2: %i\n", x[0][0]); x[0][0] = 7; printf("x3: %i\n", x[0][0]); } x1: 0 x2: 0 x3: 7 x4: 7

  12. Pass by Reference • Passing the address itself rather than the value • Changes to parameters will affect the caller's arguments as well, for they are the same variable • Used for array, variable address • Use ‘&’ to get the location of a particular variable • Example values a int values[100], minVal; minVal = minimum(values); double minimum(int a[100]) { …; } int b, c; swap(&b, &c); void swap(int *v1, int *v2) { …; }

  13. Pass by Reference #include <stdio.h> void swap(int *x, int *y); int main() { int x = 8, y = 9; printf("x: %i,\t y:%i\n", x, y); swap(&x, &y); printf("x: %i,\t y:%i\n", x, y); return 0; } void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } x 8 9 *x x: 8, y:9 x: 9, y:8 temp 8 y 8 9 *y

  14. Pass by Value #include <stdio.h> void thisWontSwap(int x2, int y2); int main() { int x = 8, y = 9; printf("x: %i,\t y:%i\n", x, y); swap(x, y); printf("x: %i,\t y:%i\n", x, y); return 0; } void swap(int x2, int y2) { int temp = x2; x2 = y2; y2 = temp; } x 8 y 9 x: 8, y:9 x: 8, y:9 x2 8 y2 9 temp 8

  15. Global Variables • What is a global variable • A variable does not belong to any function • A variable can be accessed by any function in a program • Why need a global variable • avoid passing frequently-used variables continuously throughout several functions, • How to define a global variable • Same as other variable declaration, except it is outside any function

  16. Example int x; /* Global variable */ int y = 10; /* Initialized global variable */ int foo(int z) { int w; /* local variable */ x = 42; /* assign to a global variable */ w = 10; /* assign to a local variable */ return (x % y + z / w); } int main() { printf("x:%i,\t y:%i\n\n", x, y); printf("x:%i,\t y:%i\t, returnValue:%i\n\n", x, y, foo(32)); return 0; } x:0, y:10 x:42, y:10 , returnValue:5

  17. Automatic and static variables • By default, all variables defined within function are automatic local variables • Static variables • Using keyword static • Does not disappear after function call • Initialized only once

  18. Example void auto_static(void) { int autoVar = 1; static int staticVar = 1; printf("automatic = %i, static = %i\n", autoVar, staticVar); autoVar++; staticVar++; } int main() { int i; for(i = 0; i < 5; i++) auto_static(); return 0; } automatic = 1, static = 1 automatic = 1, static = 2 automatic = 1, static = 3 automatic = 1, static = 4 automatic = 1, static = 5

More Related