Switch the screen on/off Go back to last page Go forward one page Find out more details about the advertisement
John Moores University. Main CWIS Site
  

 

Function Calling.

When we write a routine, quite often we need to incorporate, within that routine, other routines we have already written. This is called Function Calling. When we call a function, the function being called has one or more arguments: 

function(arg1, arg2, ..., argn,).

Both user defined and internal functions can be called from other functions.  An example of an internal function call can be found in the COUNT program.

Here we will write a program that will calculate the Cartesian equation of a plane that passes through three points in .

Before we program the main function, we must define any sub-functions that we intend to call.  We will define the sub function NORMVECT(p,q,r)that finds a vector which is perpendicular to the plane containing the points p, q and r.

NORMVECT(p,q,r):=CROSS(p-q,r-q)

CROSS(v,w) computes the cross product of two vectors v and w.

The main function we will call EqnPlane(M,n), where M is a 3x3 matrix of the 3 points in the plane and  n is a vector perpendicular to the plane.

EqnPlane(M, n, x, y, z) := 
 Prog 
   n := NORMVECT(M™1, M™2, M™3) 
   SOLVE(([x, y, z] - M™1) • n = 0, z)

As one can see, the vector perpendicular to the plane which contains the three points is calculated from a call of the function NORMVECT(p,q,r). Clearly, there was no real need to perform a function call here as we could have replaced the first line of the PROG with  n := CROSS(M™1-M™2, M™3-M™1).  However, this example is used to illustrate the calling of a function.  

So when would one ideally be calling functions? In general if a routine needs to be performed many times within a PROG or  is particularly lengthy and complicated it may be a good idea to  a write sub routine to make the main program more readable and understandable.