Functions 2
Functions 2
#include<stdio.h>
int main()
return 0;
}
int i, j;
if(i > j) {
else {
int main()
int result;
return 0;
int i, j, greaterNum;
if(i > j) {
greaterNum = i;
else {
greaterNum = j;
return greaterNum;
#include<stdio.h>
int main()
{
int i, j;
return 0;
if(x > y) {
else {
#include<stdio.h>
int main()
int i, j, result;
return 0;
if(x > y) {
return x;
else {
return y;
NESTING OF FUNCTIONS
function1()
function2();
}
If function2() also has a call for function1() inside it, then in that
case, it will lead to an infinite nesting. They will keep calling each
other and the program will never terminate.
I. RECURSION
Recursion is the process in which a function repeatedly calls itself
to perform calculations. Typical applications are games and sorting
trees and lists. Recursive algorithms are not mandatory, usually
an iterative approach can be found.
int factorial(int n)
{
int result;
if (n<=1)
result=1;
else
result=n*factorial(n-1);
return result;
}
#include<stdio.h>
void swap(int x, int y); /* function declaration
*/
int main ()
{
int a =100;int b =200; /* local
variable definition */
printf("Before swap, value of a : %d\
n", a );
printf("Before swap, value of b : %d\
n", b );
swap(a, b); /* calling a function to
swap the values */
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
return0;
}
Let us put above code in a single C file, compile and execute it, it
will produce the following result:
Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :100
After swap, value of b :200
Which shows that there is no change in the values though they had
been changed inside the function.
IV.2 Function call by reference
a and
* &b indicates pointer to b ie. address of variable
b. */
swap(&a,&b);
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
return0;
}
Let us put above code in a single C file, compile and execute it, it
will produce the following result:
Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :200
After swap, value of b :100
Which shows that there is no change in the values though they had
been changed inside the function.
EXERCISE 6
Exercise 6.1: Write a function which take a real number and
return its absolute value. Write a program which use the function
Exercise 6.2: Write two C functions that compute f(x)=2.3*x and
g(x,y)=x*y.
Exercise 6.3: Write a function that returns 1 if an integer number
is prime, 0 otherwise. Use that function to write a C program which
display the list of prime number below a given integer n.
Exercise 6.4: Write a C function facto that computes the factorial
of a number n (integer). The factorial function is a typical recursive
problem. Rewrite the equation defining the factorial function in a
recursive form. Write the recursive version of the code and call the
function facto_rec.
Exercise 6.5: Write the function called pow to calculate xn for any
real number x and integer n. Rewrite it in its recursive form and
write the associate C function that you will call pow_rec.
Exercise 6.6: The Taylor expansion of the exponential function is
∞
xn
given by e x =∑ .Using the previous exercises (function facto and
n=0 n!
pow), write a C functions that calculates the exponential for integer
values of x. Compare its results to the results of the C exponential
function defined in <math.h>.
Exercise 6.7: Write a program that repeatedly asks the user to
enter pairs of numbers until at least one of the pair is 0. For each
pair, the program should use a function to calculate the harmonic
mean of the numbers. The function should return the answer to
main(), which should report the result. The harmonic mean of the
numbers is the inverse of the average of the inverses and can be
calculated as follows: harmonic mean of x and y = 2.0 × x × y / (x +
y)
Exercise 6.8: Write a function which generate 50 integers between
1 and 100 randomly and do the follow. It calculates the factorial of
each number divisible by 5 of and the inverse of each number
divisible by 6. You must use two functions facto and inverse to
calculate the factorial and the inverse of any number. Static
variables should be used in those functions to count the number of
time each of them has been called.
Exercise 6.9: Write a function that takes three arguments: the
name of an int array, the array size, and an int value. Have the
function set each element of the array to the int value.
Exercise 6.10: Write a program that asks the user to enter up to
10 golf scores, which are to be stored in an array. You should
provide a means for the user to terminate input prior to entering 10
scores. The program should display all the scores on one line and
report the average score. Handle input, display, and the average
calculation with three separate array-processing functions.
Exercise 6.11: Write a function that takes three arguments: a
pointer to the first element of a range in an array, a pointer to the
element following the end of a range in an array, and an int value.
Have the function set each element of the array to the int value.
Exercise 6.12: Write a function that takes a double array name
and an array size as arguments and returns the largest value in
that array. Note that this function shouldn’t alter the contents of
the array.