Functions 351
EXERC ISES
A)Answer the following questions.
1) Write the definition of a function. Indicate types of functions available in C.
2) How does a function help to reduce the program size?
3) Differentiate between library and user-defined functions.
4) How does a function work? Explain how arguments are passed and results are returned?
5) List any five library functions and illustrate them with suitable examples.
6) What are actual and formal arguments?
7) What are the uses of re t u rn () statements?
8) What does it mean if there is no return statement in the function?
9) What are void functions?
10) Why is it possible to use same variable names for actual and formal arguments?
11) What is the main () function in C? Why is it necessary in each program?
12) Explain the different formats of re tu rn () statements. How many values return statement
returns at each call?
13) What is a global pointer? Illustrate with a suitable example.
14) W hy is the return statement not necessary when a function is called by reference?
15) Distinguish between function prototype and function definition
16) Should the function prototype should match with the function definition?
17) Can we define a user-defined function with the same library function name?
18) What is recursion? Explain its advantages.
19) Explain types of recursions.
20) Is it possible to call a library function recursively?
B) Answer the following by selecting the appropriate option.
1) Arrays are passed as arguments to a function by
a) value
b) reference
c) both (a) and (b)
d) of the above
2) It is necessary to declare the type of a function in the calling program if
a) the function returns a non-integer value
b) the function returns an integer
c) function is not defined in the same file
d) none of the above
3) Recursion is a process in which a function calls
a) itself
b) another function
c) main () function
d) none of the above
352 Programming and Data Structures
4) By default the function returns
a) integer value
b) float value
c) char value
d) none of the above
5) The meaning of keyword void before a 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
6) The function name itself is
a) an address
b) value
c) definition
d) none of the above
7) A global pointer can access the variable of
a) all user-defined functions
b) only main () function
c) only library functions
d) none of the above
8) What will be the values of x and s on execution?
int x, s ;
void main(int);
void main(x)
{
printf ("\n x = %d s = %d,//x /s);
}
a) x=l, s=0
b) x=0, s=0
c) x=l, s=l
d) none of the above
9) The main () is a
a) user-defined function
b) library function
c) keyword
d) none of the above
10) What will be the value of x after execution?
# include <stdio.h>
void main()
{
float .x = 2 .2,sqr(float),y;
Functions 353
y=(int)sqr(x);
printf ("\n x=%g",y);
}
float sqr(float m)
{ return (m*m); }
a) x=4
b) x=4.84
c) x=4.50
d) none of the above
11) What is the data type of variable m?
void main()
{
int x = 2 ;
q r (x)
/
}
sq r (
m)
{ re t u rn
a)
int
b)
float
c)
char
d)
void
C) Attempt the following programs.
1) Write a program to display "Hello" 10 times. Create a user-defined function message().
2) Write a user-defined function for the following tasks.
i) Clearing the screen.
ii) Display a character at a specified row and column number.
iii) Display a horizontal line with a given symbol.
3) Write a user-defined function for performing the following tasks.
a) Square of a number.
b) Area of a square.
c) Conversion of a decimal number to binary.
d) Reverse the number.
4) Write a program to calculate the average temperature of five days. Create temp() function.
5) Write a program to count how many times a function is called. Create a user-defined function.
6) Write a program to add return values of three functions.
7) Write a program to display odd numbers from 1 to 99 using a user-defined function.
8) Write a program to calculate square of float values and display 'it.

Get Programming and Data Structures now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.