Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.

Similar presentations


Presentation on theme: "Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call."— Presentation transcript:

1 Functions in C CSE 2451 Rong Shi

2 Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call the function, not how it works

3 Functions To use a function, you must know – Its name – What it does – What arguments it takes – What kind of result the function returns No function overloading in C – unique function names

4 Function definition The code body of the function type name (formal parameters) { Declarations and statements } Function call Name(actual parameters) ;

5 Function Prototype Function declaration or definition, specifies – number of arguments – types of the arguments Style comparison – char *strcpy( char *, char *); – char *strcpy( char * destination, char * source); Need to define or prototype a function before it can be used

6 Return The return keyword immediately ends a function Functions with a type also need a return value return expression; Ex: double example() { return 5; }

7 Example Prototype int add(int p,int q); Function call z = add(4,3); Function definition int add(int p,int q) { return p+q; }

8 Function Arguments All parameters are passed by value! Functions get a copy of the actual parameter passed in What about pointers? What about arrays?

9 Example #include int swap(int x, int y) { int k; k=x; x=y; y=k; return 0; } int main( ) { int x=1, y=2; swap(x,y); printf(“%d\t%d\n”, x, y); }

10 Recursion Function calls itself – Directly (f calls f) – Indirectly (f calls g, g calls h, h calls f) Ex: calculate the power of x to y double power (int x, int y) { if(y==1) return x; else return x*power(x,y-1); }

11 Scope Where variables or function definitions can be accessed Local variable – Defined inside of a function, visible inside the function – Defined inside of a block, visible inside the block Global variables – Defined outside any function/block Visible from its declaration to the end of the file where it is declared during compilation Visible to the whole program after linking

12 Function scope All functions are defined externally – Visible to the whole program – Normally, prototypes are put into a header file (*.h) static function(), limited scope – Only visible to the file in which the function is defined

13 Functions with #define #include #define square(x) x*x int main( ) { int y=8; int z=square(y+1); printf(“%d\n”,z); return 0; }

14 Fixing swap int swap(int * x, int *y) { int k; k=*x; *x=*y; *y=k; return 0; }


Download ppt "Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call."

Similar presentations


Ads by Google