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
Functions II-219
11. What are the void functions?
Ans: The void functions are the functions that may or may not have arguments, but these functions
do not return any value.
12. How to invoke a function from the main() function?
Ans: Table 10.1 describes how to invoke a function from a main() function. The main() is a user
defined function and user can invoke a function called as abc(x,y,z).
Table 10.1 Invoking a function from a main() function
void main()
{
----------
----------
----------
abc(x,y,z); Function Call
Actual argument
----------
----------
}
abc(data type l, data type k, data type j) Function definition
{
Formal arguments
----------
----------
return(); return value
}
1. Actual argument: The arguments of calling functions are actual arguments. In Table 10.1, the
variables x, y and z are actual arguments.
2. Formal argument: The arguments of called function are formal arguments. In Table 10.1, the vari-
able i, k and j are formal arguments.
3. Function name:A function must follow the same rule as we follow for variable naming in C.
Example: sum (int a, int b);
Function  call: A compiler executes a function when a semicolon (;) is followed by function name.
A function can be called simply using its name like other ‘C’ statement, terminated by semicolon (;).
Following example shows invoking a function from the main().
int a,b;
sum (a, b);
where, sum() is a user-defined function and ‘a’ and ‘b’ are actual integer variable arguments. The
function name must be ended by a semicolon (;).
M10_ITL-ESL4791_02_SE_C10.indd 219 12/22/2012 5:01:42 PM
II-220 Programming Concepts
4. Argument/parameter list
The argument list means variable names enclosed within the parentheses. They must be separated by
comma (,). These formal arguments (consignment) are to be received values from actual argument
and for performing this, a communication between consignee and consignor functions is made.
13. Should we use the same variable names for actual and formal arguments?
Ans: No, we should not use the same variable names for actual and formal arguments .This is because
the scope of actual and formal variables is different in different functions. Therefore, we should try to
avoid using the same variable name for formal and actual arguments.
14. How does a function work?
1. Once a function is defined and called, it takes some data from the calling function and returns a
value to the called function.
2. The detail of inner working of a function is unknown to the rest of the program. Whenever a func-
tion is called, the control passes to the called function and the working of calling function is paused.
When the execution of called function is completed, the control returns back to the calling function
and executes the next statement.
3. The values of actual arguments passed by the calling function are received by the formal argu-
ments of called function. The number of actual and formal arguments should be same. Extra
arguments are discarded if they are defined. If the formal arguments are more than the actual argu-
ments, then the extra arguments appear as garbage. Any mismatch in the data type will produce
the unexpected result.
4. The function operates on formal arguments and sends back the result to calling function. The
return() statement performs this task.
15. What are the uses of the return() statements?
Ans: The return statement is used to return a value from the function to the invoking function.
We can also use the return statement to transfer the control to the invoking function without any return
value. Every function can return almost one value.
16. What does it mean if there is no return statement in the function?
Ans: If a function does not have return statement, then it means that the return type of the function is
void, that is, the function processes the input and returns the control without any return value.
17. Explain the different formats of return() statements. How many values does a return 
statement return at each call?
Ans: The return statement may or may not return value; therefore, there are two formats of return
statement:
return(argument);
In the above format, the return statement returns the value to the function from which the function is
invoked.
return();
In this format, as the return statement does not have any argument, it will only return the control back
to the invoking function.
M10_ITL-ESL4791_02_SE_C10.indd 220 12/22/2012 5:01:42 PM
Functions II-221
A return statement can return only one value. We may include an expression within return statement.
In that case, first, the expression is evaluated, then the value is returned.
18. What is a global pointer? Illustrate with a suitable example.
Ans: If we define a pointer above main() function, it will become a global pointer. The scope of
this pointer is throughout the program. We can use this pointer in any part of the program.
19. Why is the return statement not necessary when a function is called by a reference?
Ans: When we pass the arguments by reference, any changes in formal arguments are reflected in
actual arguments; therefore, we need not to return any value from the called function. The function can
be called by reference by passing the address of the arguments to the function.
20. Distinguish between function prototype and function definition.
Ans: The function prototype informs the compiler about the function name, its argument and return
type. The function prototype can be given globally or local to main(). However, the function prototype
must precede function call if the function is not defined before the function call.
The function definition is the actual definition of the function; it also contains function prototype
below which the function body is given.
21. Does the function prototype match with the function definition?
Ans: The function prototype is declared to inform the compiler about function. The function proto-
type and function definition may have different variable names, but their types must match with each
other.
22. Can we define a user-defined function with the same library function name?
Ans: In C language, we cannot define the function with the same name as library function, because
on doing so, we will get error due function redeclaration.
23. What is recursion? Explain its advantages.
Ans: When a function invokes itself from its own function body, it is called recursion. The recursions
are of two types, which are direct recursion and indirect recursion. In the direct recursion, the same func-
tion calls itself from its own body. In indirect recursion, the function invokes some other function, which
again invokes the function that has invoked it. That is, it creates a cycle of function calls. Recursion can
be used to reduce the code length in some special types of problems. These problems become simpler
when coded with recursion than iterative approach.
24. Explain the types of recursions.
Ans: The recursions are of two types, which are direct recursion and indirect recursion. In the direct
recursion, the same function calls itself from its own body. In indirect recursion, the function invokes
some other functions, which again invokes the function that has invoked it. That is, it creates a cycle of
function calls.
25. Is it possible to call library function recursively?
Ans: As we know that we cannot edit the code of the library functions; the library function are not
recursive previously, so we cannot call them recursively.
M10_ITL-ESL4791_02_SE_C10.indd 221 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