Presentation is loading. Please wait.

Presentation is loading. Please wait.

Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic.

Similar presentations


Presentation on theme: "Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic."— Presentation transcript:

1 Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic

2 Department of Electronic & Electrical Engineering Functions are cool --- use them. Functions are very useful for encapsulating a procedure. Allows it to be reused. printf and scanf are examples of library functions that help us with IO Some languages have subroutines. These are just functions that do not return anything.

3 Department of Electronic & Electrical Engineering Function. Declarations and definitions float func(float z); / * Declaration of a function prototype */ /* Should be done before using it */ int main() { printf(" func()=%f",func(9.0)); /* calling a function */ } float func(float z) { /* definition of the function */ return z*z; } type of thing returned Parameter 9.0 is an argument

4 Department of Electronic & Electrical Engineering Function. Declarations and definitions /* If you define a function before using you don't need to declare it */ float func(float z) { /* definition of the function */ return z*z; } int main() { printf(" func()=%f",func(9.0)); /* calling a function */ }

5 Department of Electronic & Electrical Engineering void type. void func(float x) { printf(" The value passed was %f \n",x); } Use void type if function does not return a value

6 Department of Electronic & Electrical Engineering Arguments are copied into functions. void func(float x) { x=5.0; } int main() { float y=2.0; func(y); /* Y is still 2.0 */ } argument values are copied into parameters memory x y 2 ? Use void type if function does not return a value

7 Department of Electronic & Electrical Engineering Pointers and dereferencing (*) int x; int y; int *ptr; x=6; ptr = &x; y=*ptr; *ptr=7; int *ptr is how we declare pointers. ptr is a pointer variable. it is used as an address in memory x ptr 7 34A45678 memory (after running) Address of x Using * to get or set the memory is called dereferencing y 6

8 Department of Electronic & Electrical Engineering Using pointers as arguments to a function void func(int *ptr) { *ptr=7; } int main() { int z=6; func(&z); printf("%d\n",z); } argument points at z e.g. addressof Parameter is a pointer to an int Now z holds the value 7

9 Department of Electronic & Electrical Engineering Local variables only exist within the declaring block Scope of variables. int x; float func(int y) { float z=6.0; return x*z*y; } int main() { int i=6; x=2.0; x=func(i)*x; } anything declared outside a function is global you can use it anywhere. Parameters are local and only exist within the function.

10 Department of Electronic & Electrical Engineering static versus automatic storage int x; void func(){ float y; static float z;... } global variables are always persistent (static). Storage is allocated when the program loads. Any initialisation happens once. by default local variables are allocated storage when we enter the block (automatic) using static means storage is allocated when program loads.

11 Department of Electronic & Electrical Engineering Example. int z=2; void func(){ static int y=4; int x=3; x++; y++; z++; printf("x=%d y=%d z=%d\n",x,y,z); } int main(){ func(); } Each time Once at the start

12 Department of Electronic & Electrical Engineering Summary Use functions to encapsulate code - reuse + easier to read program Pointers are used if you want to change the value of an argument. Scope - Global (declared outside any function) - Local (within a function/block) static key word allocate local storage once at the start (global variables are always static by default) local variables are automatic by default.


Download ppt "Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic."

Similar presentations


Ads by Google