Functions and Parameter Passing

1.3 Use functions and parameter passing

The Power of Functions

Divide and ConquerStepwise refinement is a structured programming technique that takes an initial solution to a problem and continually refines each step until the algorithm is detailed enough for implementation in a suitable high-level language, that is, it breaks a large problem into smaller problems and then the smaller problems into even smaller problems. This technique is commonly called a ‘divide and conquer’ approach to software development.

Typically, modern software has become extremely large and complex and as such requires a number of programmers in the development process.  It is therefore useful to have a way of splitting up a program into components, each of which can be handled separately.  This sub-division of a larger program into smaller modules makes use of a facility called a sub-program.

The process of developing a solution as a collection of interconnected subprograms is called modular design.  Each module has a single complete function and is implemented using the particular sub-program facilities of suitable high-level languages.

A function is a sub-program (module) that is given data in order to perform a particular task.

There are 2 types of functions found in programming languages:

  • Inbuilt – Pre-written functions available for use if required, e.g. parseInt
  • User-Defined – Written by the user to perform a unique task.

Generally inbuilt functions can be divided into 3 main areas:

  • Mathematical – Provides greater scope for complex processing requirements, e.g. Math.random() gives a  pseudo random number between 0 and 1 inclusive and Math.round(x) which will give an integer closest to x
  • Character and String – Provide a range of useful character and string functions, e.g. parseFloat.
  • Date and Time – Provide useful functions to manipulate dates and time. e.g. getDate() ;

Next: Mathematical Functions