APznzaZH92nboTd_8Avv5CT_g1jhFprbtKiyKb4BGWHGyvZ9e-cSK57ZtINGFv2KgzulQPDOlBmPdVheJQLPrDfTUAOZ9CSk7pKtAPu9AJ...JQqpqwgB6sIf9mWsRWRfIyFU_JyMbrpvIZCc7yGeHC6eL2K4Mlrp-QGOkx-8GxWzZR4kyRO04ieAHkDEQ3nQbwJsVK49AJwlp67BOIr6puUZYpCRM4qBUtZtw=.pdf
Document Details
Uploaded by SupportingVolcano
Tags
Full Transcript
Q&A for Previous Year Questions Subject: Computer Programming(B.Tech. I Year) ------------------------------------------------------------------------------------------------------------------------------------------...
Q&A for Previous Year Questions Subject: Computer Programming(B.Tech. I Year) ------------------------------------------------------------------------------------------------------------------------------------------ UNIT–IV: Functions:: Introduction, Function Definition , Function Declaration, Function calls, Return values and their types, Categories of functions, Recursion, Storage classes, Passing arrays to functions. Pointers: Pointers and addresses, Pointer expressions and Pointer arithmetic, Pointers and Functions, void pointer, Pointers and arrays, Pointers and strings, Array of pointers, Pointers to pointers. Dynamic memory allocation: malloc, calloc, realloc, free 1. What is a function? Why we use functions in C language? Give an example. Ans: Function in C: A function is a block of code that performs a specific task. It has a name and it is reusable.It can be executed from as many different parts in a program as required, it can also return a value to calling program. All executable code resides within a function. It takes input, does something with it, then give the answer. A C program consists of one or more functions. A computer program cannot handle all the tasks by itself. It requests other program like entities called functions in C. We pass information to the function called arguments which specified when the function is called. A function either can return a value or returns nothing. Function is a subprogram that helps reduce coding. Simple Example of Function in C #include #include int adition (int, int); //Function Declaration int addition (int a, int b) //Function Definition { int r; r=a + b; return (r); } int main() 1 Q&A for Previous Year Questions Subject: Computer Programming(B.Tech. I Year) ------------------------------------------------------------------------------------------------------------------------------------------ { int z; z= addion(10,3); //Function Call printf ("The Result is %d", z); return 0; } Output: The Result is 13 Why use function: Basically there are two reasons because of which we use functions 1. Writing functions avoids rewriting the same code over and over. For example - if you have a section of code in a program which calculates the area of triangle. Again you want to calculate the area of different triangle then you would not want to write the same code again and again for triangle then you would prefer to jump a "section of code" which calculate the area of the triangle and then jump back to the place where you left off. That section of code is called „function'. 2. Using function it becomes easier to write a program and keep track of what they are doing. If the operation of a program can be divided into separate activities, and each activity placed in a different function, then each could be written and checked more or less independently. Separating the code into modular functions also makes the program easier to design and understand. 2. Distinguish between Library functions and User defined functions in C and Explain with examples. Ans: Types of Function in C: (i). Library Functions in C C provides library functions for performing some operations. These functions are present in the c library and they are predefined. For example sqrt() is a mathematical library function which is used for finding the square root of any number.The function scanf and printf() are input and output library function similarly we have strcmp() and strlen() for string manipulations. To use a library function we have to include some header file using the preprocessor directive #include. 2 Q&A for Previous Year Questions Subject: Computer Programming(B.Tech. I Year) ------------------------------------------------------------------------------------------------------------------------------------------ For example to use input and output function like printf() and scanf() we have to include stdio.h, for math library function we have to include math.h for string library string.h should be included. (ii). User Defined Functions in C A user can create their own functions for performing any specific task of program are called user defined functions. To create and use these function we have to know these 3 elements. 1. Function Declaration 2. Function Definition 3. Function Call 1. Function declaration The program or a function that calls a function is referred to as the calling program or calling function. The calling program should declare any function that is to be used later in the program this is known as the function declaration or function prototype. 2. Function Definition The function definition consists of the whole description and code of a function. It tells that what the function is doing and what are the input outputs for that. A function is called by simply writing the name of the function followed by the argument list inside the parenthesis. Function definitions have two parts: Function Header The first line of code is called Function Header. int sum( int x, int y) It has three parts (i). The name of the function i.e. sum (ii). The parameters of the function enclosed in parenthesis (iii). Return value type i.e. int Function Body Whatever is written with in { } is the body of the function. 3. Function Call In order to use the function we need to invoke it at a required place in the program. This is known as the function call. 3. Write some properties and advantages of user defined functions in C? Ans: Properties of Functions - Every function has a unique name. This name is used to call function from “main()” function. - A function performs a specific task. 3 Q&A for Previous Year Questions Subject: Computer Programming(B.Tech. I Year) ------------------------------------------------------------------------------------------------------------------------------------------ - A function returns a value to the calling program. Advantages of Functions in C - Functions has top down programming model. In this style of programming, the high level logic of the overall problem is solved first while the details of each lower level functions is solved later. - A C programmer can use function written by others - Debugging is easier in function - It is easier to understand the logic involved in the program - Testing is easier 4. Explain the various categories of user defined functions in C with examples? Ans: A function depending on whether arguments are present or not and whether a value is returned or not may belong to any one of the following categories: (i ) Functions with no arguments and no return values. (ii) Functions with arguments and no return values. (iii) Functions with arguments and return values. (iv) Functions with no arguments and return values. (i) Functions with no arguments and no return values:- When a function has no arguments, it does not return any data from calling function. When a function does not return a value, the calling function does not receive any data from the called function. That is there is no data transfer between the calling function and the called function. Example #include #include void printmsg() { printf ("Hello ! I Am A Function."); } 4 Q&A for Previous Year Questions Subject: Computer Programming(B.Tech. I Year) ------------------------------------------------------------------------------------------------------------------------------------------ int main() { printmsg(); return 0; } Output : Hello ! I Am A Function. (ii) Functions with arguments and no return values:- When a function has arguments data is transferred from calling function to called function. The called function receives data from calling function and does not send back any values to calling function. Because it doesn‟t have return value. Example #include #include void add(int,int); void main() { int a, b; printf(“enter value”); scanf(“%d%d”,&a,&b); add(a,b); } void add (intx, inty) { int z ; z=x+y; 5 Q&A for Previous Year Questions Subject: Computer Programming(B.Tech. I Year) ------------------------------------------------------------------------------------------------------------------------------------------ printf ("The sum =%d",z); } output : enter values 2 3 The sum = 5 (iii) Functions with arguments and return values:- In this data is transferred between calling and called function. That means called function receives data from calling function and called function also sends the return value to the calling function. Example #include #include int add(int, int); main() { int a,b,c; printf(“enter value”); scanf(“%d%d”,&a,&b); c=add(a,b); printf ("The sum =%d",c); } int add (int x, int y) { int z; z=x+y; return z; } 6 Q&A for Previous Year Questions Subject: Computer Programming(B.Tech. I Year) ------------------------------------------------------------------------------------------------------------------------------------------ output : enter values 2 3 The sum = 5 (iv) Function with no arguments and return type:- When function has no arguments data cannot be transferred to called function. But the called function can send some return value to the calling function. Example #include #include int add( ); main() { int c; c=add(); printf ("The sum =%d",c); } int add () { int x,y,z; printf(“enter value”); scanf(“%d%d”,&a,&b); z=x+y; return z; } Output: enter values 2 3 The sum = 5 7 Q&A for Previous Year Questions Subject: Computer Programming(B.Tech. I Year) ------------------------------------------------------------------------------------------------------------------------------------------ 5. Explain the Parameter Passing Mechanisms in C-Language with examples. Ans: Most programming languages have 2 strategies to pass parameters. They are (i) pass by value (ii) pass by reference (i) Pass by value (or) call by value :- In this method calling function sends a copy of actual values to called function, but the changes in called function does not reflect the original values of calling function. Example program: #include void fun1(int, int); void main( ) { int a=10, b=15; fun1(a,b); printf(“a=%d,b=%d”, a,b); } void fun1(int x, int y) { x=x+10; y= y+20; } 8 Q&A for Previous Year Questions Subject: Computer Programming(B.Tech. I Year) ------------------------------------------------------------------------------------------------------------------------------------------ Output: a=10 b=15 The result clearly shown that the called function does not reflect the original values in main function. (ii) Pass by reference (or) call by address :- In this method calling function sends address of actual values as a parameter to called function, called function performs its task and sends the result back to calling function. Thus, the changes in called function reflect the original values of calling function. To return multiple values from called to calling function we use pointer variables. Calling function needs to pass „&‟ operator along with actual arguments and called function need to use „*‟ operator along with formal arguments. Changing data through an address variable is known as indirect access and „*‟ is represented as indirection operator. Example program: #include void fun1(int,int); void main( ) { int a=10, b=15; fun1(&a,&b); printf(“a=%d,b=%d”, a,b); } void fun1(int *x, int *y) { *x = *x + 10; 9 Q&A for Previous Year Questions Subject: Computer Programming(B.Tech. I Year) ------------------------------------------------------------------------------------------------------------------------------------------ *y = *y + 20; } Output: a=20 b=35 The result clearly shown that the called function reflect the original values in main function. So that it changes original values. 6. Differentiate actual parameters and formal parameters. Ans: Actual parameters Formal parameters The list of variables in calling function is The list of variables in called function is known as known as actual parameters. formal parameters. Actual parameters are variables that are Formal parameters are variables that are declared in declared in function call. the header of the function definition. Actual parameters are passed without Formal parameters have type preceeding with them. using type main( ) return_type function_name(formal {..… parameters) function_name (actual parameters); {..… ….. function body; } ….. } Formal and actual parameters must match exactly in type, order, and number. Formal and actual parameters need not match for their names. 7. Explain in detail about nesting of functions with example. Ans: Nesting of functions The process of calling a function within another function is called nesting of function 10 Q&A for Previous Year Questions Subject: Computer Programming(B.Tech. I Year) ------------------------------------------------------------------------------------------------------------------------------------------ Syntax:- main() { ……….. Function1(); ………. } Function1(); { ………… Function2(); ………… } Function2(); { ………… Function3(); ………… } Function3(); { …………. } main () can call Function 1() where Function1 calls Function2() which calls Function3() and so on 11 Q&A for Previous Year Questions Subject: Computer Programming(B.Tech. I Year) ------------------------------------------------------------------------------------------------------------------------------------------ Ex: float ratio (int, int, int); int difference (int, int); void main() { int a,b,c,d; printf(“ enter three numbers”); scanf(“%d%d%d”, &a, &b, &c ); d= ratio(a,b,c); printf (“%f\n” ,d); } float ratio(int x,int y,int z) { int u ; u=difference(y,z); if (u) return(x/(y- z)); else return (0.0); } int difference (int p, int q) { if(p!=q) 12 Q&A for Previous Year Questions Subject: Computer Programming(B.Tech. I Year) ------------------------------------------------------------------------------------------------------------------------------------------ return 1; else return 0; } main reads values a,b,c and calls ratio() to calculate a/(b-c) ratio calls another function difference to test whether (b-c) is zero or not this is called nesting of function 8. What is recursive function? Write syntax for recursive functions. Ans: Recursion Recursion is a process by which a function calls itself repeatedly, until some specified condition has been satisfied. When a function calls itself, a new set of local variables and parameters are allocated storage on the stack, and the function code is executed from the top with these new variables. A recursive call does not make a new copy of the function. Only the values being operated upon are new. As each recursive call returns, the old local variables and parameters are removed from the stack, and execution resumes immediately after the recursive call inside the function. The main advantage of recursive functions is that we can use them to create clearer and simpler versions of several programs. Syntax:- A function is recursive if it can call itself; either directly: void f( ) { f( ); } (or) indirectly: void f( ) { g( ); } void g( ) 13 Q&A for Previous Year Questions Subject: Computer Programming(B.Tech. I Year) ------------------------------------------------------------------------------------------------------------------------------------------ { f( ); } Recursion rule 1: Every recursive method must have a base case -- a condition under which no recursive call is made -- to prevent infinite recursion. Recursion rule 2: Every recursive method must make progress toward the base case to prevent infinite recursion 9. Write a program to find factorial of a number using recursion. Ans: #include int fact(int); main() { int n,f; printf(“\n Enter any number:”); scanf(“%d”,&n); f=fact(n); printf(“\n Factorial of %d is %d”,n,f); } int fact(int n) { int f; if(n==0||n==1) //base case f=1; else f=n*fact(n-1); //recursive case return f; } Output:- Enter any number: 5 Factorial of 5 is 120 10. Differentiate between recursion and non- recursion. Ans: Recursion:- Recursion is a process by which a function calls itself repeatedly, until some specified condition has been satisfied. 14 Q&A for Previous Year Questions Subject: Computer Programming(B.Tech. I Year) ------------------------------------------------------------------------------------------------------------------------------------------ When a function calls itself, a new set of local variables and parameters are allocated storage on the stack, and the function code is executed from the top with these new variables. A recursive call does not make a new copy of the function. Only the values being operated upon are new. As each recursive call returns, the old local variables and parameters are removed from the stack, and execution resumes immediately after the recursive call inside the function. Ex:- void main( ) { int n=5; fact( n); } int fact( ) { if(n==0 || n==1) return 1; else return(n*fact(n-1)); } Non-Recursion:- Using looping statements we can handle repeated statements in „C‟. The example of non recursion is given below. Syntax:- void main( ) { int n=5; res = fact(n); printf(“%d”,res); } int fact( ) { for(i=1;i