(Function with output parameter and Recursive
function)
© Manzur Ashraf
Objective:
Learn how to write user defined functions.
Scope:
The student should know the following:
1.
void
Functions with Input/Output Parameters
2.
Recursive
functions
Functions with output parameters using pointers :
Till now we used only the
functions that returned zero or one value through the return statement. To return more than one value, we need to use the
output parameters, which are pointers.
Note that when we use output parameters in functions we declare those functions
as void returning functions because
they are not returning single value by return statement.
Solved Example (i) :
Consider the example of a
function that accepts the radius of a circle from the main program and returns
the area and circumference of a circle by using the output parameters:
#include
<stdio.h>
void area_circum (float radius, float *area, float
* circum) ; / function prototype */
void
main (void)
{
float r,
a, c ;
printf (“Enter the radius of the circle
\n”) ;
scanf (“%f”, &r) ;
area_circum (r, &a, &c) ; /* function call that passes the address of the variables a
and c */
printf (“The area is %f and
circumference is %f\n”, a, c) ; /* output results */
} //end of main
void area_circum (float radius, float *area, float
* circum) // function
{
*area = 3.14 * radius * radius ; /* indirect
reference to variable a of main program */
*circum = 2 * 3.14 * radius ; /* indirect
reference to variable c of main program */
} // end of
function area_circum()
Here
the variables area of function area_circum and a of main both
refer to the same memory location. Similarly, the variables circum of function
area circum and c of main both refer to the same memory location.
Note
that when the function is called in the main program, the output parameters
have & attached to them before their name. When the output parameters are
used in the function, they are used with * attached to them before their name.
Recursion:
What is recursion?:
·
So far, we have seen that a C program consists of the
function main() and a zero or more other functions.
·
That each of the other functions can be called by main() or
by other functions.
·
In addition, C also allows a function to call itself. Such a function is called a recursive
function.
·
Recursion is essentially another way of performing
iteration.
·
It is a very powerful tool for simplifying algorithms and
coding of functions that involve repetitions
·
It generally involves using the divide and conquer approach
to problem solving.
·
In this approach, a large problem is solved by breaking it
into two or more sub-problems and solving the sub-problems. The sub-problems are solved by further
sub-dividing them into more simpler problems.
This sub-division continue until the simples form of the problem is
obtained, the solution to which is obvious.
Examples
of recursive functions:
Base Case: Which provides a solution to the simplest
form of a problem.
Recursive Case: Which solves a
general form of the problem in terms of the next simpler
version of the problem.
Example
1:
Iterative Solution |
Recursive Solution |
int
sumton(int n) { int i, sum=0; for (i=1; i<=n; i++) sum+=i*i; return sum; } |
int
sumton(int n) { if (n ==1) return 1; else return (n*n+sumton(n-1)); } |
Example2:
Fibonacci
Sequence:
This
is defined recursively as:
f0
= 0, f1 =
1 fi+1 = fi
+ fi-1 for i=1,2, ..
i.e.
except for f0 and f1, every element is the sum of its
previous two elements:
0, 1, 1, 3, 5, . . .
Iterative Solution |
Recursive Solution |
int fibonacci (int n) { int i,sum1=0, sum2=1,
sum; if (n<=1) return n; else {
for (i=2; i<=n; i++) { sum=sum1+sum2; sum1=sum2; sum2=sum; } return sum; } } |
int fibonacci(int n) { if (n<=1) return n; else return (fibonacci(n-1) + fibonacci(n-2)); } |
Exercises:
Problem # 1:
By taking help from Solved Example(i) Solve the following problem:
Write a
function that takes 3 integers a, b , c from main program as input parameter
and returns
the sum of the three, the product of the three, and the average by using
output parameter. Write a main program to test
the function.
Problem
#2 :
By
taking help from above solved examples
of recursive functions Solve the following problem:
Write
a recursive function power that takes a float variable x,
and an integer variable n, and returns the value of xn
. Write a main program to test the
function.
Evaluation:
Your grade will depend on yours
individual efforts in solving lab problems , your active participation and
seriousness during the lab.