Page 168 - DCAP407_DATA_STRUCTURE
P. 168
Unit 9: Recursion
Use the function explained below to identify the following:
1. The base case of the function Sample
2. The recursive case of the function Sample
int Sample (int base, limit)
{
if (base > limit) return -1;
else
if (base == limit) return 1;
else
return base * Sample (base+1, limit);
}
1. Base case computes one or more specific numbers (usually 0 or 1) for which the
result can be attained immediately.
2. Recursive case computes result by calling recursively the function with a small
argument and using the result to obtain the final answer.
9.3 Function Call and Recursion Examples
A function is a block of statements that can be utilized to perform a particular task. A function
comprises the following parts:
1. Function prototype declaration
2. Function declarator
3. Actual and formal arguments
4. Return statement
5. Invoking or calling function
Function Prototype Declaration
The functional prototypes are provided in the beginning of the program after the # include statement.
The function prototype declaration comprises the return type, arguments list, and name of the function.
Function Call
A function is activated only when a function call is invoked. A function should be invoked by its name
along with the argument list enclosed within parenthesis and terminated with a semi-colon.
Actual and Formal Argument
The arguments defined in a caller function and provided in the function call are termed as actual
arguments. The arguments defined in the function definition are termed as formal arguments.
Return Statement
Return statement returns value to the caller function. Return statement returns just one value at a time.
When the compiler encounters a return statement, the control of the program is transferred to the caller
function.
LOVELY PROFESSIONAL UNIVERSITY 161