Functions
1. What is a function?
Ans: A function is a self-contained block or a sub-program containing one or more statements that
perform a particular task.
2. How a function is invoked?
Ans: The desired function is called from the main() function. The statement(s) of a called function
are written outside the main() function.
3. List the types or classify the functions supported by the C language?
Ans: C language supports the following two types of functions:
1. Library functions and
2. User-defined functions.
4. What do you mean by library functions? Briefly explain it by giving its example.
Ans: The library functions are pre-defined set of functions. Their task is limited. User should not
worry to understand the internal working of these functions. They can only use the functions but cannot
change or modify them. Source code cannot be modified.
Example: sqrt (81 ) gives result 9. Here, the user need not worry about its source code, but the result
is provided by the function.
5. List any five library functions and illustrate them with suitable examples.
Ans: There are a number of library functions are available such as printf(), which is used to
display the message, scanf(), which is used to take the input from user, clrscr(), which is used
to clear the output screen, pow() function, which is used to calculate power of the argument and
getch(), which is used take a single character as input.
For example:
printf("Hello");
prints message Hello on output screen.
10
M10_ITL-ESL4791_02_SE_C10.indd 217 12/22/2012 5:01:42 PM
II-218 Programming Concepts
scanf("%d",&n);
takes the integer input from user and stores it into variable n.
clrscr();
after execution, it will clear the screen.
pow(4,2);
will calculate square of the number 4; therefore, the value returned will be 16.
getch();
is used to take a character as input from user and returns it to the variable.
6. What do you mean by user-defined functions? Briefly explain it by giving its example.
Ans: The functions defined by the user according to their requirement are called as user-defined
functions. The user can modify the function according to their requirement. They certainly understand
the internal working of the function. They have full scope to implement their own ideas in the function.
Thus, the set of such user-defined functions can be useful to another programmer. One should include
the file in which the user-defined functions are stored to call the function in the program.
Example: Let the square(9) is a user-defined function that gives the result 81. Here, user knows the
internal working of the square() function, as its source code is visible.
7. Is the main() a user-defined or library function?
Ans: Yes, the main() is a user-defined function. In the main(), required code is written. The code
may be statements or functions.
8. Why to use functions?
Ans:
1. If we want to perform a task repetitively, then it is not necessary to re-write the particular block of
the program repeatedly. Shift the particular block of statements in user-defined function. The func-
tion defined can be called any number of times to perform the task.
2. Using functions, large programs are reduced to smaller ones. It is easy to debug and find out the
errors in it. It also increases readability.
9. How do functions help to reduce the program size?
Ans: By writing the functions, we can reduce the space required for the part of the program that is
repeated a number of times, by including it into a function. On the function call instead of writing the
whole code repeatedly, the control is transferred to the function.
10. Write the syntax of function definition.
Ans: Function definition begins as per format given below:
function-type function_name (argument/parameter list)
{
local variable declaration;
Statement1;
Statement2;
return(value);
}
The function type indicates the return type of the value returned by the function. It may be void(),
int, float etc. The function name is the name of the function.
M10_ITL-ESL4791_02_SE_C10.indd 218 12/22/2012 5:01:42 PM

Get Express Learning - Computer Fundamentals and Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.