Saturday, December 31, 2011

Functions And Program Structures


Functions And Program Structures 
·        Function definition
·        Return statement
·        Function types
·        Actual and formal arguments
·        Local and global arguments
·        Scope of variables
             Automatic variables
             Register variable
             Static variable
              External variable
·        Recursive functions
·        Preprocessors
·        Macros
·        Headers
·        Standard Functions

This chapter deals with the method of declaration of the user defined function its use in C++. The scope of the variables such as automatic, register external and static, is explained. The recursive function is described in this chapter with numerous illustrations and the additional details of the functions such as preprocessors, macros, headers and standard functions are also explained in this chapter.

 INTRODUCTION
A complex problem may be decomposed into a small or easily manageable parts or modules called functions. Functions are very useful to read, write, debug and modify complex program. They can be easily incorporated in the main program. In C++, the main ( ) itself is a function that means the main function is invoking the other functions to perform various tasks.
The main advantages of using a function are :
  • easy to write a correct small function
  • easy to read, write and debug a function
  • easier to maintain or modify such a function
  • small functions tend to be self documenting and highly readable.
  • it can be called any number of times in any place with different parameters

  Most of the high level languages such as BASIC, FORTRAN, or Pascal allow both subroutines or procedures and functions. In these languages, the formal arguments may be passed and returned only by the subroutines or procedures. The functions may take some formal arguments and can only return one value and then only through the function name itself.

        In C++, there is no difference between a function and subroutine. The functions may or may not take some formal arguments for calling portion of the program. The function may or may not transfer back, values to a called function block. It may behave like a traditional subroutine or as a function or as both. Secondly, most high level languages difference between a main program and subprograms. In C++, all modules are called functions and they all have the same structure.

1 comment: