Functions in c


 INTRODUCTION

There are six derived types in C: arrays, functions, pointer, structure, union and enumerated types.  The function type is derived from its return type.

                                                     


3.1 DESIGNING STRUCTURED PROGRAMS

Whenever we are solving large programs first, we must understand the problem as a whole, then we must break it in to simpler, understandable parts. We call each of these parts of a program a module and the process of sub dividing a problem into manageable parts top-down design.

    The principles of top-don design and structured programming dictate that a program should be divided into a main module and its related modules.
    
      The division of modules proceeds until the module consists only of elementary process that is intrinsically understood and cannot be further subdivided. This process is known as factoring.
    
       Top-down design is usually done using a visual representation of the modules known as a structured chart.
                

FUNCTIONS IN C

A function is a self-contained block of code that carries out some specific and well-defined task.

C functions are classified into two categories

        1. Library Functions
        2. User Defined Functions

Library Functions

These are the built in functions available in standard library of C.The standard C library is collection various types of functions which perform some standard and predefined tasks.

Example: abs (a) function gives the absolute value of a, available in <math.h> header file

               pow (x, y) function computes x power y. available in <math.h> header file

               printf ()/scanf () performs I/O functions. Etc..,

User Defined Functions

These functions are written by the programmer to perform some specific tasks.

Example: main (), sum (), fact () etc.

The Major distinction between these two categories is that library functions are not required to be written by us whereas a user defined function has to be developed by the user at the time of writing a program.

3.3  USER-DEFINED FUNCTIONS

The basic philosophy of function is divide and conquer by which a complicated tasks are successively divided into simpler and more manageable tasks which can be easily handled. A program can be divided into smaller subprograms that can be developed and tested successfully.

A function is a complete and independent program which is used (or invoked) by the main program or other subprograms. A subprogram receives values called arguments from a calling program, performs calculations and returns the results to the calling program.

3.3.1 ADVANTAGES OF USER-DEFINED FUNCTIONS

  1. Modular Programming It facilitates top down modular programming. In this programming style, the high level logic of the overall problem is solved first while the details of each lower level functions is addressed later.
  2. Reduction of source code The length of the source program can be reduced by using functions at appropriate places. This factor is critical with microcomputers where memory space is limited.
  3. Easier Debugging It is easy to locate and isolate a faulty function for further investigation.
  4. Code Reusability a program can be used to avoid rewriting the same sequence of code at two or more locations in a program. This is especially useful if the code involved is long or complicated.
  5. Function sharing Programming teams does a large percentage of programming. If the program is divided into subprograms, each subprogram can be written by one or two team members of the team rather than having the whole team to work on the complex program

3.3.2 THE GENERAL FORM OF A C FUNCTION



return_type function_name (argument declaration)
{
     //local declarations
       ……    
       ……
     //statements
        ……
     return (expression);
 }

 
 









Figure: 3.2 General Form of A C Function




return-type

Specifies the type of value that a function returns using the return statement. It can be any valid data type. If no data type is specified the function is assumed to return an integer result.

function-name

Must follow same rules of variable names in C. No two functions have the same name in a C program.

argument declaration

Is a comma-separated list of variables that receive the values of the argument when function is called. If there are no argument declaration the bracket consists of keyword void.

A  C function name is used three times in a program
                        1. for function declaration
                        2. in a function call
                3. for function definition.

3.3.3 FUNCTION DECLARATION (OR) PROTOTYPE

The ANSI C standard expands the concept of forward function declaration. This expanded declaration is called a function prototype.

A function prototype performs two special tasks.
  1. First it identifies the return type of the function so that the compile can generate the correct code for the return data.
  2. Second, it specifies the type and number of arguments used by the function.

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post