Tuesday 30 January 2018

Functions in C

Functions in C

       A function is a block of statements which perform a specific task. A program can be divided into functions. This enhances the modularity and maintainability of a program. Functions eliminates redundant code i.e it helps us to avoid rewriting the same logic again and again. Functions are the means to support code reusabilty in C.

Function Declaration and Definition

           Every C program has at least one function i.e main( ) where the program execution begins.

 Consider the following C program in which we use a function to add two numbers : 

----------------------------------------------------------------------------------------------------------------------------

 

----------------------------------------------------------------------------------------------------------------------------

Output :

Sum : 8
 
----------------------------------------------------------------------------------------------------------------------------
 
Function Declaration / Prototype describes the function interface to the compiler giving information about the function name, return types and parameters (arguments).
 
 In the above program,   
               Function Name : add
                    Return Type : int
Parameters / Arguments : int a, int b

Function Definition provides the code which constitutes the body of the function. 
 
A general form of C function definition is shown below : 
 
 
Function Call invokes the function body to perform a specific task.

Please note that argument list may consists of 0 or more no. of arguments and the return type of the function may be void also in case the function doesn't return any value. Following program uses two functions to generate first 'n' terms of fibonacci series. In fibonacci series, ith term of the series is equal to the sum of (i - 1)th and
(i - 2)th term with first two terms as 0 and 1.
Fibonacci series : 0 1 1 2 3 5 8 13 21 ...
----------------------------------------------------------------------------------------------------------------------------

 
 
----------------------------------------------------------------------------------------------------------------------------

Output :
 
Fibonacci Series till 8 terms :-
0 1 1 2 3 5 8 13
 
----------------------------------------------------------------------------------------------------------------------------

           Another sample program which finds the maximum of three numbers using a function max (int a, int b) which finds the maximum of two numbers. 
 
----------------------------------------------------------------------------------------------------------------------------


 
----------------------------------------------------------------------------------------------------------------------------

Output :
 
Maximum Value : 9

----------------------------------------------------------------------------------------------------------------------------

Parameter Passing
  
         We have seen above that while calling a function we pass parameters/arguments.
There are two ways of passing parameters :
 
Call by Value :
          This method copies the argument value into the formal parameters of the function. Apparently, all the programs we have seen till now uses call by value. In this method, changes made to the argument values in the called function is not reflected in the calling function. Consider the program which finds the maximum value out of three numbers. In the function call maxThreeNum(x, y, z) , values in x, y and z are copied into variables a, b and c respectively.
 
Call by Reference :
           This method copies the address of the arguments into the formal parameters of the function. In this method, changes made to the argument values in the called function is reflected in the calling function.
Following program illustrates the difference between call by value and call by reference :

----------------------------------------------------------------------------------------------------------------------------
 


----------------------------------------------------------------------------------------------------------------------------

Output :
 
Before calling changeValues() :
a : 2 b : 5
After calling changeValues() :
a : 2 b : 50

----------------------------------------------------------------------------------------------------------------------------

A typical program in which call by reference is used is swapping of two numbers :

----------------------------------------------------------------------------------------------------------------------------

 

----------------------------------------------------------------------------------------------------------------------------

Output :
 
Before swapping :-
a : 5 b : 2
After swapping :-
a : 2 b : 5

----------------------------------------------------------------------------------------------------------------------------

 Variable Length Argument List

           Consider a scenario in which a function is expected to accept any number of arguments i.e the no. of arguments is not known at compile time. To handle this kind of situation C offers an advanced concept of variable length arguments. We need to include stdarg.h header file to use this feature. Following program computes the average of the values passed to a function. The number of values to be passed is not known previously. So, we use a function which accepts variable length arguments.

----------------------------------------------------------------------------------------------------------------------------



----------------------------------------------------------------------------------------------------------------------------

Output :
 
avg1 = 4.500000 avg2 = 5.333333

----------------------------------------------------------------------------------------------------------------------------

 

No comments:

Post a Comment