Theoritical questions of function of c programming

Function: Function is block of program. When any program is very long then we try to cut the program in different parts( or blocks) so that whole program is more understandable and easier to debug (error checking).This part of program is called function. It is also useful when set of program uses many times in the program then instead of writing whole program in many time we only call the function. There are two type of function. (1) Library function (Predefined function) (2) User defined function Syntax: Return_type Function_name ( Parmeter1,parametre2,parameter3,…..) { Statement 1; Statement 2; …………… …………… …………… return < expression> } Fuction_name: It can be any valid identifier like India,sum,_world etc. Return_type: In can be any valid data type like int,char,float ,void etc (default int) Returnt type is required when you want function should return any value and if you don’t want that function return any value then write void as a return type. Parameter list: It is set of value which you want to give the function and they are seprated by comma. (default void) Simple example : int sum (int,int) //function declaration void main() { int p; p=sum(3,4); //function call printf(“%d”,sum); } int sum( int a,int b) //function definition { int s; //function body s=a+b; return s; //function returning value } Output 7 Since definition of sum function is written by some user. So it is called user defined function. This function can calculate the sum of any two integers. You are also using printf() function but you are not writing definition of printf() function because printf is predefined function. Your TURBO C know about printf function but he doesn’t know about your sum () function; How to make user defined function as library function: How to make user defined function as library function: Step:1 (3) Different type of function ? Ans: (a) Function with no parameter and not returning any value ? Example: void world1(); //function declaration void world2(); //function declaration char c='*'; int i,j; void main() { char *msg="I KNOWN FUNCTION"; clrscr(); world1(); //function call printf("%c%22s%7c",c,msg,c); world2(); //function call getch(); } void world1() //function definition { int a=-1; for(i=0;i<15;i++) { a++; for(j=0;j<30;j++) { if(j>=(15-a) && j<(15+a)) { printf(" "); } else { printf("%c",c); } } printf("\n"); } } void world2() //function definition { int a=-1; for(i=0;i<16;i++) { a++; for(j=30;j>0;j--) { if(j>a && j<=(30-a)) { printf(" "); } else { printf("%c",c); } } printf("\n"); } } Output: (b)Function has parameter but not returning any value : void swap(int,int); void main() { int a=10,b=15; clrscr(); printf("Befor swap a=%d ,b=%d",a,b); swap(a,b); printf("\nAfter swap a=%d ,b=%d",a,b); getch(); } void swap(int a,int b) { int c; c=a; a=b; b=c; } Output: Before swap a=10,b=15 After swap a=10,b=20 Explanation: a and b are auto variable (default storage class is auto) .Here its scope is only within main function.After the main function both a and b will die.We are swaping outside the main function so value of a and b is not changing. (b) Function with parameter and returning a value. void main() { int a=1,b=5,c; clrscr(); c=operation(a,b); printf("%d",c); getch(); } int operation(int a,int b) { int c; c=++a * ++a * b++; return c; } Output: 45 Note. Any function can return only one value at a time. If return type is int then there is not necessity of function declaration. (4) What is Ellipsis(…)? Ans: Ellipsis is there consecutive period (.) with no white space. With the help of ellipsis we can write a function of variable number of parameter. Example: int number(int a,...) { return a; } void main() { int i; i=number(5,4,3,2,1); clrscr(); printf("%d",i); getch(); } Output: 5 In header file stdarg.h has three macro va_arg,va_end,va_start ,with help of this macro we can know other parameter of a function of variable number of parameter. Syntax: void va_start (va_list ap,lastfix) type va_arg(va_list ap,type) void va_end(va_list ap); va_list is data . lastfix is a last fixed parameter used in a function of variable parameter. type is used to know which data type are using in function of variable parameter . va_arg always return next parameter from right to left. va_start set the pointer ap to the first parameter from right side of a function of variable parameter. va_end help in the normal return. Example. #include #include void sum(char *msg, ...) { int total = 0; va_list p; int arg; va_start(p, msg); while ((arg = va_arg(p,int)) != 0) { total += arg; } printf(msg, total); va_end(p); } void main() { sum("The total sum is %d\n", 5,7,11,8); getch(); } Output: 31 (3) What is main? Ans: main is user defined function because main is defined by user but it is different from other user defined function. Every c program starts from main function and end at the null statement. In any program there must be only one main function otherwise operating system will not able to decide from which main program should start. Main function has three parameter 1. Argument counter 2. Argument vector 3. Environment vector (For more detail go to the link “Advance c” and read command line argument) Parameter passing convention: There are two convention for parameter passing : (1) Pascal style (2) C style (by default we are use this style) Pascal style: In this style function name should (not necessary ) in uppercase .First parameter of function call is passed to the first parameter of function definition and so on. For this we use keyword pascal. Syntax: Pascal C style: In this style function name can be both case. First parameter of function call is passed to the last parameter of function definition. For this we have keyword cdecl. Syntax: cdecl Example: void main() { static int a=25; void cdecl conv1() ; void pascal conv2(); conv1(a); conv2(a); getch(); } void cdecl conv1(int a,int b) { printf("%d %d",a,b); } void pascal conv2(int a,int b) { printf("\n%d %d",a,b); } Output: 25 0 0 25 Function recursion :
Post a Comment