- Ques 1 : What is a Function in C?
- Ques 2 : How do you declare and define a Function?
- Ques 3 : What is the return type of a function?
- Ques 4 : Explain the concept of Function parameters (Actual and Formel Arguments).
- Ques 5 :What is the scope of a variable?
- Ques 6 :What is the difference between call by value and call by reference?
- Ques 7 :What is a Recursion function?
- Ques 8 :What is a Towers of Hanoi?
- Ques 9 :Recursion Vs Iteration.
- Ques 10 : Special Note on Recursion.
Ques 1 : What is a Function in C?
A Function in C is a complete program in itself, in the sense that its structure is similar to C main() function except that the name main() is replaced by the name of the function.
The general form of function is given below: –
<type> name (arguments)
{
----
}
Ques 2 : How do you declare and define a Function?
All Function in C Must be declared before they are used in the program. C allows the declaration of functions in the calling program with the help of function prototype. The general form of prototype and declaration is here,
<type> <name> (arguments);
int sum (itn x, int y);
In a Definition, first line consists of data type of the value to be returned followed by name of the function and then the arguments in pair of parentheses.
// Function for add two numbers
float sum (x, y)
float x, y;
{
float s;
s = x+y;
return s;
}
Ques 3 : What is the return type of a function?
The Return Statement is used for serve two Purposes:
- Return Statements immediately transfers the control back to the calling program.
- It returns the value after return to the calling program.
Example:
big (int a, int b)
{
if (a > b )
return (a);
else
return (b);
}
Ques 4 : Explain the concept of Function parameters (Actual and Formel Arguments).
Whenever a function gets called by an argument or a set of arguments, these arguments are known as Actual Arguments. The Arguments that are present in the function definition are called as formal or dummy arguments.
These are called formal arguments because they represent the names of data items that are transferred into the function from the calling function of the program. The formal arguments are local to the function because they are not recognized outside the function.
Ques 5 :What is the scope of a variable?
The Range of code in a program over which a variable has a meaning is called as scope of the variable. In simple form the scope of a variable decides as to how it can be accessed by the different parts of the program.
Ques 6 :What is the difference between call by value and call by reference?
Call By Value: – In this Type of parameters passing, only the values are passed from the calling function to the called functions. Therefore this technique is accordingly called as call by value.
Call By Reference: – If the Programmer desires that the changes made to the formal parameters be reflected to the corresponding actual parameters then he should use call by reference method of parameter passing.
Ques 7 :What is a Recursion function?
Recursion is derived from the word recur which means repetition. In C, it is possible for the function to call itself. and The Recursion is the process by virtue of which a function or a procedure can call itself.
fun()
{
fun()
}
This means that function fun() is calling itself.
Ques 8 :What is a Towers of Hanoi?
The Towers of Hanoi problem is a classic case study in recursion. It is well known children’s game, played with three towers and a number of different sized disks. It involves moving a specified number of disks from one tower to another using third as an auxiliary tower.
Ques 9 :Recursion Vs Iteration.
Recursion | Iteration |
---|---|
Recursion is comparatively slower as compared to iteration. | Iteration is faster than recursion. |
Recursion takes more memory, as variables are created again and again during every function call. | It takes less memory as variables are declared only once. |
These algorithms may require extra overheads for multiple function calling. | No overheads are involved in these algorithms. |
For some problems recursion is very simple to use like tree traversal etc. Otherwise it is difficult to implement. | For Some problems it is difficult to implement. Otherwise it is easy to implement. |
It’s the system, which takes care of internal stack. | It’s the user who has to take care of stack. |
Ques 10 : Special Note on Recursion.
Recursion can be used to write simple, short and elegant programs. However a recursive algorithm requires a condition that must end the recursive calls. The absence of this condition would result in an infinite loop. For example in function fact the condition “n = = 0” is the required terminating condition.
Since recursion involves overheads such as CPU time and memory storage, it is suggested that recursion should be used with care.