II-222 Programming Concepts
26. Explain call by value and call by address with programming example of each one.
Ans: When value is passed in a function, it is known as call by value. Value is changed only inside
the function. The changed value is not reflected in the original value.
For example,
#include<stdio.h>
#include<conio.h>
void swap(int,int);
void main()
{
int a,b;
clrscr();
printf("enter two integer number ");
scanf("%d%d",&a,&b);
printf(" before calling swap function ");
printf("a=%d b=%d ",a,b);
swap(a,b);
printf("after calling swap function");
printf("a=%d b=%d ",a,b);
getch( );
}
void swap(int p,int q)
{
int t;
t=p;
p=q;
q=t;
}
Call by reference: In call by reference, in the following example, addresses of the variables/argu-
ments a and b are supplied to the called function swap().Whatever changes we perform inside
the function through pointers p and q, the same are reflected back in the original actual parameters
a and b in the calling function.
In call by reference, one can manipulate with physical address in the memory and can make changes
as per requirement. The call by reference can be implemented by passing pointers to the variables as
arguments to the function. Ultimately, these pointers are used by the called function to acees the argu-
mentsvariables and make changes as per requirement. Thus, with this method whatever changes are
made in the arguments in the called function through addresses, the same are reflected in the actual
arguments even if control returns to the calling method.
#include<stdio.h>
#include<conio.h>
void swap(int*,int*);
void main()
{
int a,b;
M10_ITL-ESL4791_02_SE_C10.indd 222 12/22/2012 5:01:42 PM
Functions II-223
clrscr();
printf("enter two integer number ");
scanf("%d%d",&a,&b);
printf("before calling swap function ");
printf("a=%d b=%d ",a,b);
swap(&a, &b);
printf("after calling swap function");
printf("a=%d b=%d ",a,b);
getch( );
}
void swap(int *p, int *q)
{
int t;
t=*p;
*p=*q;
*q=t;
}
27. Write a program to reverse a number using function.
#include<stdio.h>
#include<conio.h>
long int reverse(long int);
void main()
{
int n,rev;
clrscr();
printf("enter a number");
scanf("%d",&n);
rev=reverse(n);
printf("reverse number of %d is %d",n,rev);
getch();
}
long int reverse(long int a)
{
int r,rev=0;
while(a!=0)
{
r=a%10;
rev=rev*10+r;
a/=10;
}
return rev;
}
M10_ITL-ESL4791_02_SE_C10.indd 223 12/22/2012 5:01:42 PM
II-224 Programming Concepts
Multiple-choice Questions
1. The arguments of caller function are called by .
(a) Actual arguments (b) Formal arguments (c) Memory variable
2. Which keyword is used for a function not returning any value?
(a) void (b) int (c) auto
3. Function prototype is not compulsory when function definition given .
(a) Before caller function (b) After caller function (c) At the end of program
4. The limitation of return statement .
(a) Can return only one value (b) Is critical to use (c) Consumes more memory space
5. The recursion of the function can be stopped .
(a) Using conditional statement (b) By pressing Ctrl + Break (c) Using keyword return
6. In C functions, return statement is not compulsory because return address is .
(a) Stored on the stack
(b) Stored into the pointer
(c) Stored in register variable of the CPU
7. The use of return statement is to .
(a) Return value
(b) Inform compiler end of program
(c) To push back the control of the program
8. When a user-defined function is invoked, values are passed .
(a) On the stack (b) Into ROM (c) To memory variables
9. When a function returns value 0 (zero), it indicates .
(a) Normal termination (b) Erroneous termination (c) None of the above
10. Arrays are passed to function by .
(a) Value (b) Address (c) None of the above
11. It is necessary to declare void type in the function definition when a function does not return
.
(a) Any value (b) Integer (c) Float
12. By default function returns .
(a) Integer (b) Float (c) Null
13. The main is a .
(a) Keyword (b) User-defined function (c) Library function
14. Which is the correct statement for finding the cube of 5?
(a) pow(5,3); (b) pow(3,5);
(c) pow(3); (d) None of the above
15. The floor() function returns .
(a) Round down value (b) Round up value
(c) Absolute value (d) Zero value
M10_ITL-ESL4791_02_SE_C10.indd 224 12/22/2012 5:01:42 PM
Functions II-225
16. Recursion is a process in which a function calls .
(a) Itself (b) Another function
(c) main() function (d) None of the above
17. The meaning of keyword void before function name means .
(a) Function should not return any value (b) Function should return any value
(c) No arguments are passed (d) None of the above
18. A global pointer can access variable of .
(a) All user-defined functions (b) Only main() function
(c) Only library functions (d) None of the above
19. The if (fun()) transfers the control to .
(a) The function fun()
(b) The condition specified followed by if(fun) statements
(c) The statements specified followed by if(fun)
(d) None of the above
20. The arguments in a function are provided .
(a) Within the parentheses (b) Within the braces (c) Within angle bracket angles
21. Global variables appear .
(a) Outside the main() (b) Within the main(_)
(c) Above the header file (d) None of the above
22. The library function returns absolute value.
(a) pow() (b) abs() (c) sum()
23. The header file contains all the mathematical functions.
(a) stdio.h (b) math.h (c) conio.h
24. The functions defined by user are .
(a) User-defined functions (b) Standard functions (c) Library functions
25. The user cannot change the definition of .
(a) User-defined functions (b) System functions (c) Library functions
26. When a function executes itself, the process is called .
(a) Iteration (b) Recursion (c) Looping
27. The function cannot be used for recursion.
(a) User-defined functions (b) System functions (c) Library functions
28. While defining recursion function, a provision should be made to .
(a) Store the value (b) Tracing the program (c) Stop the program
29. The result obtained by function is sent back to the calling function through the statement.
(a) Exit (b) Return (c) Array
30. Whenever a function is called, the passes to the called function.
(a) Arguments (b) Control (c) Data
M10_ITL-ESL4791_02_SE_C10.indd 225 12/22/2012 5:01:42 PM
II-226 Programming Concepts
31. void sum(void) means .
(a) Function neither sends any argument nor returns any result
(b) Function sends argument but not returns result
(c) Function returns results but not required any input
32. float sum() means .
(a) It returns float value (b) It returns int value (c) It is a void
33. int avg (float, float, float) means .
(a) Returns float value and passes three float values
(b) Returns int value and passes three float values
(c) Returns long int value and passes three float values
34. int sum(int *) means .
(a) Address is passed (b) Value is passed (c) No value is passed
35. In call by address call formal arguments are pointer to .
(a) main() function (b) Actual arguments (c) Itself
36. A function can return more values by the method called .
(a) Call by value (b) Call by address (c) Call by parameter
37. In , addresses of the array elements are passed to function.
(a) Call by value (b) Call by reference (c) None of the above
38. Function by default returns value
(a) Integer (b) Float
(c) Character (d) None
39. In main function when we write return(0) it refers to .
(a) Compiler (b) Operating System (c) None of this
40. In function calling the values are .
(a) passed from right to left
(b) passed in the sequence in which they are returned
(c) passed from left to right
41. The useful data structure in function calling is .
(a) heap (b) queue
(c) stack (d) link-list
42. The stack is .
(a) FIFO data structure (b) LIFO data structure
(c) Random data structure (d) Two way data structure
43. When a function is called .
(a) the caller functions variables are pushed on to the stack
(b) the function to be called is pushed on to the stack
(c) control is transferred to that function
(d) All of these
M10_ITL-ESL4791_02_SE_C10.indd 226 12/22/2012 5:01:42 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset