Jun 30, 2014

C Programming #32: Function

Following article will discuss about function. It is one of the important part of C which helps in making abstractions, reuse and manage large code efficiently.

Lets first look into need of function with an example


Program: find factorial of 4, 6 and 8.


#include <stdio.h>

int main()
{
   int n = 4;
   int counter;
   int fact = 1;

   for(counter = 1, fact = 1; counter <= n; counter++) {
      fact = fact * counter;
   }
   printf("Factorial of %d is %d\n", n, fact);

   n = 6;
   for(counter = 1, fact = 1; counter <= n; counter++) {
      fact = fact * counter;
   }
   printf("Factorial of %d is %d\n", n, fact);

   n = 8;
   for(counter = 1, fact = 1; counter <= n; counter++) {
      fact = fact * counter;
   }
   printf("Factorial of %d is %d\n", n, fact);

   return 0;
}
Output of the above program is


Factorial of 4 is 24
Factorial of 6 is 720
Factorial of 8 is 40320

Now you can see directly that there is lot of repetition in the code. Reason is logic of finding factorial does not change with input number n. Hence it makes sense to logically abstract finding factorial under a function.

Function has 3 parts to it -
  1. Function Prototype
  2. Function Definition
  3. Function Call
It is always good to have all 3 parts. (Function prototype is not always necessary).

Block diagram of the generic function is as follows

As you see function takes lot of input and gives one output. (In some way quite similar to mathematical function!). Inputs are referred to as arguments and output is referred to as result. Above function func take a1, a2 and an.r is the result of the function. Same block diagram is represented in code as follows.

Syntax -

Function Prototype
return_t func(argument1_t, argument2_t, argumentn_t);

Function Definition
return_t func(argument1_t a1, argument2_t a2, argumentn_t an)
{
   // Body of function
   return ret_value;
}

Function Call
x = func(v1,v2,vn);

Explanation -
  1. Function Prototype gives the outer description of the block diagram.
    1. Name of function
    2. It tells what are types of arguments it can take.
    3. What is return type you can expect.
  2. Function prototype helps compiler to understand function call and it will help to verify if the function call is correct.
  3. Function Definition gives complete description of the block diagram.
    1. Name of function
    2. It tells what are types of arguments it can take.
    3. What is return type you can expect.
    4. What is operation carried out in function
    5. What is the value that is returned.
  4. Function Call gives the instance of use of function.
    1. What are the values of the arguments.
    2. Where should the return value assigned into.
Here a1, a2 and a3 are called formal parameter/ formal argument. And v1, v2 and v3 are called actual parameter/ actual argument.

Hence Factorial function can be re-written as follows

#include <stdio.h>
int factorial(int); /* Function prototype */
int main()
{
   int f;
   f = factorial(4);
   printf("Factorial of 4 is %d\n", f);
   f = factorial(6);
   printf("Factorial of 6 is %d\n", f);
   f = factorial(8);
   printf("Factorial of 8 is %d\n", f);
   
   return 0;
}
int factorial(int n)
{
   int counter, ret_fact;
   for(counter = 1, ret_fact =1; counter <= n; counter++) {
      ret_fact *= counter;
   }
   return ret_fact;
}

Note that
  1. n, counter and ret_fact are variables local to function they cannot be accessed outside function. (More in future article : C Programming #33: Global Variable)
  2. Internally C uses stack to establish function. (More in future article: C Programming #37: Function - Stack)
There is possibility that some function do not take any arguments then -
Function Prototype
return_t func(void);
void signifies that func does not take any arguments here.

Function Definition
return_t func(void)
{
   // Body of function
   return ret_value;
}

Function Call
x = func();

There is possibility that some function do not return anything then -
Function Prototype
void func(argument1_t, argument2_t, argumentn_t);

Function Definition
void func(argument1_t a1, argument2_t a2, argumentn_t an)
{
   // Body of function
   return ret_value;
}

Function Call
func(v1,v2,vn);

You can of course guess how would it look if some function takes neither arguments nor returns value.

Links

Next Article - C Programming #33: Global Variable
Previous Article - C Programming #31: - Typedef part1
All Articles - C Programming

No comments :

Post a Comment