180 likes | 326 Views
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
E N D
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 • 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
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) ; }
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
#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
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
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][])
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
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
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
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
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) { …; }
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
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
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
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
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
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