Search This Blog

Monday, 5 August 2013

Introduction to Functions in C!

0 comments

WRITING FUNCTIONS IN C!
A function is a sub program that performs a specific task using the values given as input and then returns the result to the calling program.  Every function contains a set of program statements that can be processed independently. When a function is invoked or called from other part of the program, it behaves as though its code is inserted at the point of the function call.

The program, which calls a function is called as ‘Caller’, and the function being called is known as ‘Callee’.  The communication between the caller and the callee takes place through a set of variables called parameters.  Functions are independent when the variables used within its body are local to it.
COMPONENTS OF A FUNCTION:

Generally, every function has the following elements associated with it:
  1. Function declaration or prototype
  2. Function parameters (formal parameters)
  3. Combination of function declaration and its definition
  4. Return statement and
  5. Function call
Function Prototype
This component is a declarative statement, which provides the following information to the compiler:
  1. The name of the function
  2. The type of the value to be returned from the function (optional; default is integer)
  3. The number and type of the arguments that must be supplied in a call to the function
When a function call is encountered, the compiler checks the function call with its prototype in order to ensure that the arguments used in the function call are of proper data type. The function prototype is having the following syntax:
ret_val function_name (argument 1, argument 2, ……, argument n);

In this syntax, the ret_val specifies the data type of the value to be returned.  When a function does not return any value, it must be specified with a keyword void.  A void function can include a dummy return statement to return the control back to its caller without returning any value.

An example for function declaration is as follows:
int max(int x, int y);
In this example, int is the data type of the return value, max is the name of the function, and x and y are arguments of type int.  It is also noted that the function declaration always ends with a semicolon.
C++ makes prototyping mandatory if functions are defined after the function main(), i.e., after the function call.  It assumes void in case of no arguments in the argument list.
Function Definition
The function itself is referred to as function definition.  The first line of the function definition is known as function declarator or function header and is followed by the function body, which is enclosed in braces.
C++ allows the definition to be placed anywhere in the program.  If a function is defined before its invocation, its prototype (declarative part) is optional. 
Function body can have one or more return statements to return the control back to its caller.  The return statement is used to return the result of the function to its caller.  When the return type is void, the function need not return any value, i.e., there is no need for a return statement within the function body.  The following is the syntax for a function that doesn’t return any value:
void fnName( param-list )
{
      statement(s)
      return; // return is optional
                    }

Here is a presentation on "Functions in C!"


The following is an example for declaring and using function in C.  It solves the problem of finding the greatest of three integers :

#include <stdio.h>
Greatest(int a, int b, int c)
{
    int big;
    big=a;
    if(b>big)
        big=b;
    if(c>big)
        big=c;
    return big;
}
void main()
{
    int x,y,z,great;
    printf("Enter three integers (x,y,z):");
    scanf("%d%d%d",&x,&y,&z);
    great = Greatest(x,y,z);
    printf("The Greatest Number is : %d",great);
}

Leave a Reply