1. What is the need for user-defined functions? (or) 2. (a) Explain different categories of functions in C with simple illustrative examples? (b) Write a C program using functions to calculate the factorial of a given number? (or) 3. (a) Define actual parameter and formal parameter. What is meant by global and local variable? Explain with example. (or) 4. Write briefly about the category of functions in C Explain about call by value with an example. Functions: Defination: A Function is a block of code that has a name and it has a property that it is reusable i.e. it can be executed from as many different points in a C Program as required. A function is a module or block of program code which deals with a particular task. Structure of a Function C functions are classified into two categories .They are 1. Library functions or pre-define function. 2. User define functions. Library functions: This are predefine functions which have their own functionality and cloud not be changed by the user. Example: printf (), scanf (), sqrt () etc... User define functions: This are the functions which are defined by the user based up on the his own requirements. Example: main () Why we use Functions: Two reasons 1. Writing functions avoids rewriting the same code again and again. 2. Another reason to use functions is to reduce program size. Any sequence of instructions that appears in program more than once is a candidate for being made into a function. Function Declaration Syntax: function name (parameter list) Arguments declaration; { Local variable declaration; Statements; Return (expression); } It has three main parts The name of the function i.e. sum The parameters of the function enclosed in parenthesis Return value type i.e. int Example: int sum(int x, int y) { int ans = 0; //holds the answer that will be returned ans = x + y; //calculate the sum return ans //return the answer } Any C program contains at least one function. If a program contains only one function, it must be main( ). In a C program if there are more than one functional present then one of these functional must be main( ) because program execution always begins with main( ). There is no limit on the number of functions that might be present in a C program. Each function in a program is called in the sequence specified by the function calls in main( ) After each function has done its thing, control returns to the main( ), when main( ) runs out of function calls, the program ends. Calling function: The function which is declared, to call another function for the purpose of performing a required task is called as a Calling function Called function: The function which actually performs the prescribed task is called and returns the value to the calling function is known as the called function. Actual arguments: The arguments or information passed from calling function to the called function are called actual arguments. Formal arguments: The arguments on which the called function works with are known as formal arguments. Category of functions: User defined functions are divided in to three different categories. They are as follows. (i) Functions with no arguments and no return values. (ii) Functions with arguments and no return values. (iii) Functions with arguments and return values. Functions with no arguments and no return values: These are the functions which does not have any arguments received from the calling function and which does not return any values to the calling function. Example : Main() { Printf(“\n I am in main function:”); Fun(); Getch(); } Fun() { Printf(“\n I am in fun() function:”); } Output : I am in main function: I am in fun() function. Functions with arguments and no return values. These are the functions which obtains the arguments from the calling function but does not returns any values to the calling function. Example : Main() { Int a,b; a=5, b=5; sum(a,b); getch(); } Void sum( int x, int y) { Int z; Z=x+y; Printf(“\n sum of digits given:%d”,z); } Functions with arguments and with return values: These are the functions which obtains arguments from the calling function and returns a value to the calling function. Main() { Int a,b,c; a=5, b=6; clrscr(); c=add(a,b); printf(“\n sum of values is:%d”,c); getch(); } Int add( int x,int y) { Return x+y; } Recursive functions: A function can call itself such a process is called recursion and those functions are called recursive functions. Example: Main() { Int n, result; Printf(“\n enter n value:”); Scanf(“%d”,&n); Result=Factorial( n); Printf(“\n the factorial of %d is:%d”,n,result); Getch(); } Int Factorial(n) { Int fact; If(n==1) Return (1); Else{ Fact=fact*factorial(n-1) } Return fact; } CALL BY VALUE: In this method the value of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function. With this method the changes made to the formal arguments in the called function have no effect on the values of actual argument in the calling function. The following program illustrates this: main ( ) { int a = 10, b=20; swap (a,b); printf ("\na = % d b = % d", a,b); } swap (int x, int y) { int t; t = x; x = y; y = t; printf ( "\n x = % d y = % d" , x, y); } The output of the above program would be; x = 20 y = 10 a =10 b =20 CALL BY REFERENCE In the second method the addresses of actual arguments in the calling function are copied in to formal arguments of the called function. This means that using these addresses we would have an access to the actual arguments and hence we would be able to manipulate them the following program illustrates this. main ( ) { int a = 10, b =20, swapv (&a, &b); printf ("\n a = %d b= %d", a, b); } swapr (int **, int * y) { int t; t = *x *x = *y; *y = t; } The output of the above program would be a = 20 b =10