1 / 32

Functions in C Language

Functions in C Language. Rohit Khokher. Functions in C Language. Why do we need functions? Function definition Types of functions Functions with no arguments and no return values, Functions with arguments but no return values, Functions with arguments and return values,

leena
Télécharger la présentation

Functions in C Language

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 in C Language RohitKhokher

  2. Functions in C Language • Why do we need functions? • Function definition • Types of functions • Functions with no arguments and no return values, • Functions with arguments but no return values, • Functions with arguments and return values, • Return value data type of function, • Void functions.

  3. Why do we need functions? • To partition a large and complex C program into a set of smaller subprograms (functions).

  4. Subprogram A Subprogram 1 Main Program Use Subprogram 1.1 Uses Subprogram 1 Uses Subprogram A Subprogram B Subprogram 2 Subprogram 1.2 …. …………. Subprogram m …………. Subprogram 2 Subprogram X …………. Subprogram 1.k Subprogram n …………. …………. ………….

  5. Main Program Calls Calls Calls Function call means Go to the first instruction of the function code stored in the code segment. Starting from the first code execute all function code. After executing the last instruction of the function cod, return to the caller with execution results. ……… function X function A function B Calls Calls Calls function A1 function A2 …. function Am Calls Calls Calls function A1.k function A1.1 function A1.2 …………. Subprogram n …………. …………. ………….

  6. Main Program Advantages Top –down modular software design. Software reuse as functions can be used in different software development. Large software can be developed with ease by breaking it into manageable independent smaller functions. Functions can be easily debugged and tested in isolation for correctness. Large software can be developed by integrating the tested independent functions. Calls Return to Caller function A Calls Return to Caller function A1 Return to Caller Return to Caller Calls Calls Calls Return to Caller function A1.k function A1.1 function A1.2 …………. Subprogram n …………. …………. ………….

  7. Modular Programming • Each module should do only one type of action. • Communication between modules is allowed through calling module. • Every module should have single entry point and single exit point. • In sequential arrangement a calling module can be called by a caller module that precedes it.

  8. main() { float x , y; float add(x , y); x=2.3; y=1.7; w=add(x , y); } Declarations Data segment A function is a complete and independent program which is used (or invoked) by the main program or other subprograms. A subprogram receives values called arguments from a calling program, performs calculations and returns the results to the calling program. Function calls Code segment float add (a , b) float a , b; { return(a + b); } Definitions Data segment Data segment

  9. Importance of functions • Many programs require to use a function repeatedly. • Programs with using functions are compact & easy to understand. • Testing and correcting errors is easy because errors are localized and corrected. • We can understand the flow of program and its code easily since the readability is enhanced while using the functions. .

  10. Elements of User Defined Functions Function Definition function return data type function name (parameter list) Function Header { local variable declarations; executable statement1; executable statement2; ……. return statement] } Function body

  11. Data type like float, double, void that a function returns to the calling function (default is int type). void specifies no return type function return function name Variable name List of variable names that would receive the data sent by the caller function, and the called function can use it to send the return values to the caller program. It is also referred to as formal parameter list or arguments. Parameter list may be nil. parameter list

  12. Examples float quadratic ( float am float b, float c) double power (double x, int y) float mult (float x, float y) int sum (int a, int b) void justprint (void) void justprint ()

  13. Function Body Variables used by the function { local variable declarations; executable statement1; executable statement2; ……. return statement] } Statements that perform actions Returns the value

  14. Return Values • The statement return; does not return any value. • The statement return (expression); returns the expression value.

  15. Function Calls Formal l parameter main () { int y; y=mul (10, 5); } Int mul (int x, int y); { int p; p=x * y; return (p); } Actual parameter If actual parameter and formal must agree in numbers and data types

  16. Function Declarations In C all functions should be declared before using them (It is also referred to as function prototype) Syntax: function return data type function name (parameter list); It may be placed before the main (global prototype) or inside a function definition (local prototype) Prototype declaration is not essential, but global prototype declaration is a good practice.

  17. Function Categories • Functions with no arguments and no return values. • Functions with arguments and no return values. • Functions with arguments and return values. • No argument but return value. • Returns multiple values

  18. Functions with no arguments and no return values. /*Functioprintline () */ void printline (void) inti; for (i=0;i<=10;i++) printf (“%c”, ‘?’); printf (“\n”); } /* Function Declaration */ void printline (void); void compute (void); main () { int z; printline(); z=compute (); } no return values /*Functioprintline () */ void compute (void) inta,b,c; c=5; b=7; c=a+b; } no return values

  19. Functions with arguments and no return values. /*Functioprintline () */ void printline (char x) inti; for (i=0;i<=10;i++) printf (“%c”, x); printf (“\n”); } /* Function Declaration */ void printline (char c); void compute (int a,int b); main () {int z;char c; scanf (“%c”, c)’ printline( c); z= compute (5,7); } Arguments but no return Formal Parameters /*Functioprintline (inta,int b) */ void compute (int a, int b) {int c; c=a+b; } Arguments but no return Actual Parameters

  20. Functions with arguments and return values. /*Functioprintline () */ void printline (char x) inti; for (i=0;i<=10;i++) printf (“%c”, x); printf (“\n”); } /* Function Declaration */ void printline (char c); void compute (int a,int b); main () {int z;char c; scanf (“%c”, c)’ printline( c); z=compute (5,7); } Arguments but no return Formal Parameters /*Functioprintline (inta,int b) */ void compute (int a, int b) {int c; c=a+b; Return( c ); } Actual Parameters Arguments with return

  21. No argument but return value. /*Functioprintline () */ void printline (char x) inti; for (i=0;i<=10;i++) printf (“%c”, x); printf (“\n”); } /* Function Declaration */ void printline (char c); void compute (int a,int b); main () {int z;char c; scanf (“%c”, c)’ printline( c); z=compute (); } Arguments but no return Formal Parameters /*Functioprintline (inta,int b) */ void compute (void) inta,b,c; c=5; b=7;c=a+b; Return( c ); } Actual Parameters Arguments with return

  22. Returns multiple values Pointer s and d pointing to two memory locations /* Function Declaration */ void compute (int x,int y, int *s, int *d); main () {int x=10,y=20,s,d) compute (x, y, &s, &d ); printf (“s=%d \n d=%d \n”, s, d); } void compute (int a,int b, int *sum, int *diff) { *sum=a+b; *diff =a-b; } Addresses of the locations they are pointing to.

  23. Nesting of Functions /*Function */ float ratio (int x, int y, int z) If (difference (y , z)) return (x/(y-z)); else return (0.0); } /* Function Declaration */ float ratio (int x,int y, int z); float difference (int x,int y); main () {int a=10,b=20,c=5) printf (“%f \n”, ratio (a,b,c); } /*Function*/ float difference (int p, int q) If ( (p!=q)) return (1); else return (0); }

  24. Recursion: A function calls itself. main () { printf ( “recurring \n”); main (); } factorial (int n) { int fact; if (n==1) return (1); else fact= n* factorial (n-1); Return (fact); } It is example of recursion. It will call itself infinitely many times. In each it will print a line like shown below recurring I recurring recurring …….. Trace this program for n=5

  25. factorial (3) { if (n==1) return (1); else fact= 3* factorial (3-1); Return (fact); } factorial (2) { if (n==1) return (1); else fact= 2* factorial (2-1); Return (fact); } 2  1 3 2  1 1 6 factorial (1) { if (n==1) return (1); }

  26. Recursive function #include<stdio.h>longfibonacci(longn);intmain(){longresult;longnumber;printf("Enteraninteger:");scanf("%ld",&number);result=fibonacci(number);printf("Fibonacci(%ld)=%ld\n",number,result);return0;} longfibonacci(longn){if(n==0||n==1){returnn;}else{returnfibonacci(n-1)+fibonacci(n-2);}}

  27. longfibonacci(4){if(n==0||n==1){returnn;}else{returnfibonacci(3)+fibonacci(2);}}longfibonacci(4){if(n==0||n==1){returnn;}else{returnfibonacci(3)+fibonacci(2);}} 3=2+1 1=1+0 2=1+1 longfibonacci(3){if(n==0||n==1){returnn;}else{returnfibonacci(2)+fibonacci(1);}} longfibonacci(2 ){if(n==0||n==1){returnn;}else{returnfibonacci(1)+fibonacci(0);}} 1 1 1=1+0 0 longfibonacci(1){if(n==0||n==1){returnn;}} longfibonacci(2){if(n==0||n==1){returnn;}else{returnfibonacci(1)+fibonacci(0);}} longfibonacci(0){if(n==0||n==1){returnn;}} longfibonacci(1){if(n==0||n==1){returnn;}} 0 1 longfibonacci(0){if(n==0||n==1){returnn;}} longfibonacci(1){if(n==0||n==1){returnn;}}

  28. Passing arrays to functions One Dimensional Array array Passes the address of the first element of the array /* Function Declaration */ Int big (int a[ ] ,int n); main () { int v [ 4] = {4,6,1,3}; printf (“%f \n”, big(v, 4); } /* Function */ int big (int a[ ] ,int n) { int I,max; max = a[0]; for (i=1;i<n; i++) if (max < a[i]) max =[i]; return (max); }

  29. Tow dimensional arrays /* Function */ Int matrix_sum (int x[ ][N] ,int M, int N); { int i,j; matrix_sum=0.0; for (i=1;i<M; i++) for (j=1;j<N;ji++) matrix_sum += x[i][j]; return (matrix_sum); } Passing strings: Strings are passed as arrays Statement to call for an array a[10][20] suma= matrix_sum (a,10,20);

  30. The scope, visibility and Lifetime of variables • Storage class • Automatic: (local variable in a functions) • External: (Global variable and it is active through out the life of a program) • Static: It can be declared inside a function and it persist until the end of a program. • Register: They are kept in one of the registers . • Scope of a variable: Range over a variable is available for execution, e.g. the scope of a local variable is the function in which it is defined. • Longevity : How long a variable retains a given value. • Visibility of a variable: When a variable is available for execution. • Lifetime of a variable: Duration in which a variable is available for execution.

  31. Scope Rules • Scope: The region of a program in which a variable is available. • Visibility: The variable that can be accessed for execution is called visible. • Lifetime: Time duration during which a variable can be accessed.

  32. Scope Rule • Global variable: It exist during the life of a program and can be accessed from ever where. • Local variable: Begins and ends within the function or block where it is declared. • Auto: Declared in main and it exists during the entire program execution. • Static: Declared in a function but, once assigned, it retains its values during the entire program execution.

More Related