SUHAIL T A
Functions A number of statements grouped into a single logical unit C pgm is a collection of functions Self contained pgm segment that carries out a specific task main is a special recognized function
Functions-Advantages Facilitates top-down modular pgming- high level logic is solved first while the details of lower level fns are addressed later Avoids complexity of a pgm-avoiding rewriting the same code over and over Each fn can be individually tested, so debugging and testing are easy Can be used by many other pgms, so save time and reduce the length of the pgm Pgms with fns are easy to maintain because modifications can be done to certain fns within the pgm
Functions- modular pgmming Main Pgm Function A Function B Function C B1 B2
Functions Two Types 1. Library Functions 2. User defined functions 1. Library Functions Built in functions, used to perform well defined tasks Eg. printf(), scanf(),gets(),puts(), gechar(),putchar() Can be either input or output task or some mathematical and scientific tasks 2. User defined functions Written by the programmer according to his need
Concepts associated with Functions 1. Function declaration/function prototype A function declaration provides the following information to the compiler 1. The name of the fn 2. The type of the value returned 3. The number and the type of arguments that must be supplied in a call to the fn
Concepts associated with Functions Syntax return-data-type function-name(type,type,type.type); Eg. int cube(int); The fn cube has one argument of type int. It returns an int value
Concepts associated with Functions 2. Function Definition Similar to fn declaration but does not have semicolon Consists of 3 main components a. Fn header b. Declaration of arguments c. Body of the fn
Concepts associated with Functions Syntax return-data-type function name(formal argument 1, formal argumantn) declaration of variables; statements; return (exp); Eg. double cube(double num) return num*num*num; Body of the fn
Concepts associated with Functions 3. Elimination of fn declaration If the fns are defined before they are called, then the declarations are necessary Eg. #include<stdio.h> int max(int a,int b) return a>b?a:b; void main(void) int i,j,imax; printf( Enter two numbers ); scanf( %d%d,&i,&j); imax=max(i,j); printf( the max of %d & %d is %d,i,j,imax);
Concepts associated with Functions 4. return statement Used to return any value from the fn called Terminates the fn and returns a value to its caller Also used to exit from any fn without any returning any value Syntax return; return(exp);
Concepts associated with Functions Eg. #include<stdio.h> int cube(int); void main() int i; for(i=0;i<=10;i++) printf( %d,cube(i)); int cube(int y) return y*y*y;
Concepts associated with Functions 5. Function Call A fn Comes to life when a call is made to the fn Specified by the fn name followed by the values of parameters enclosed within the parenthesis, terminated by semi colon. Rules for fn call 1. Number of actual and formal parameters are same 2. Type of actual and formal parameters are same 3. Sequence of actual and formal parameters are same
Concepts associated with Functions Eg. #include<stdio.h> void main() void text(void); void text(void) printf( welcome );
Function Parameters/ Actual and formal Function Parameters Arguments Means of commnbtnthe calling and the called fn Classified into Actual Parameters and formal Parameters Formal Parameters Given in the fn declaration and fn definition Dummy Parameters Actual Parameters Parameters/variables defined in the main pgm
Function Parameters/ Actual and formal Eg. #include<stdio.h> int sum(int,int); void main() int a,b; Arguments printf( Enter two numbers ); scanf( %d%d,&a,&b); printf( %d,sum(a,b)); int sum(int x,int y) return x+y;
Categories of Functions 1. Function with no arguments and no return values or empty parameter list An empty parameter list refers to the list with no arguments or parameters A fn can be defined with empty parameter list Specified by writing either voidor nothing at all in the parenthesis of fn definition
Categories of Functions Eg. #include<stdio.h> void text1(); void text2(); void main() text1(); text2(); void text1(); printf( Function text1 takes no arguments ); void text2() printf( Function text2 also takes no arguments );
2. Function with arguments and no return values Eg. #include<stdio.h> void largest(int,int); void main() int a,b; printf( Enter two numbers ); scanf( %d%d,&a,&b); largest(a,b); getch(); void largest(int a,int b) if (a>b) printf( largest element is %d,a); else printf( largest element is %d,b);
Categories of Functions 3. Function with arguments and return values Eg. #include<stdio.h> int add(int,int,int); void main() int a,b,c,sum; printf( Enter three numbers ); scanf( %d%d%d,&a,&b,&c); sum=add(a,b,c); printf( sum=%d,sum); int add (int x,int y,int z) int s; s=x+y+z; return(s);
A fn calls itself Eg. main() Recursion printf( this is an example of recursion\n ); main(); o/p- execution will continue indefinitely this is an example of recursion this is an example of recursion this is an example of recursion
Recursion Evaluation of factorials of a given no Factorial of n=n*(n-1)*(n-2).*1 Factorial of 4=4*3*2*1 factorial (int n) int fact; if (n==1) return(1); else fact=n*factorial(n-1); return(fact);
Here fact=n*factorial(n-1) Assume n=3 fact=3*factorial(2) =3*2*factorial(1) =3*2*1 =6 Recursion
Passing Arguments An argument can be passed from main() to the fn in two ways 1. Call by value 2. Call by reference 1. Call by value Refers to sending value of arguments into the fn Value of actual arguments is copied onto the corresponding formal arguments whenever a fn is called Operations performed on the formal arguments in the fn has no effect on the value of actual arguments in the main fn
main() int i=10; print(); Passing Arguments print (int i) printf( %d,i); return;
2. Call by Reference Passing Arguments Method to access any fn through the main pgm using pointers Operations performed on formal arguments directly effect the value of actual arguments Formal contains the address of actual arguments, so any changes on that address effect the value
Keep in touch through suhailta@gmail.com