3. Calling Modules or Subroutines

Modules added to software projects will contain one or more subroutines. 
  • These subroutines can be either procedures or functions. 
  • In most languages the syntax used to call or execute subroutines written from scratch, and subroutines included from outside sources, is similar or in most cases identical. 
  • However, there are differences in the way a procedure call and a function call are implemented within the source code of all languages.

Procedure calls
  • The name given to a procedure is a special kind of identifier. 
  • This identifier does not have a data type, rather it is used by the programming language to locate the subprogram within the source code. 
  • In most languages, the use of a procedure’s identifier causes control to execute the associated procedure. 
  • In essence, the procedure’s name, together with any parameters, is the statement used to execute the procedure. 
  • For example, if a Visual Basic procedure has been written called DoThis it can be called using the statement Call DoThis or more simply by the statement DoThis.
Function calls
  • Functions are similar in most respects to procedures. 
  • However, the identifier used to name a function also has a data type. 
  • This data type defines the type of data that will be returned by the function
  • Functions should be designed to return a single data item via this return value. 
  • This is why they are called functions. In mathematics, a function is defined to be a relationship where each set of inputs returns a single unique output. 
  • Subroutines written as functions should adhere to this definition. 
  • Unfortunately many programming languages do not enforce this rule. 
  • Function calls form part of expressions. 
  • For example, 5 + Cos(34) is an expression that calls the Cosine function using the value 34 as its parameter. 
  • The result is returned via the Cos identifier.
Comments