Page 185 - DCAP210_INTRODUCTION__TO_MICROPROCESSORS
P. 185
Unit 13: Subroutines
The search order for the target procedure, or built-in function is: Notes
1. internal procedure--unless the procedure name was enclosed in quotes
2. built-in function
3. external procedure
The mechanism for locating and activating an external procedure is implementation-dependent.
Here is an example of a subroutine invocation using the call instruction.
/* main program */
do n=1 to 5
call factorial n
say 'The factorial of' n 'is:' RESULT
end
return
factorial : procedure
n = arg(1)
if n = 1 then
return 1
return n * factorial( n - 1 )
The above program would normally use a function call instead. The following
shows how the main program would be coded in this case.
/* main program */
do n=1 to 5
say 'The factorial of' n 'is:' factorial( n )
end
return
Normally, you use the call instruction to invoke a function, when you will ignore (or not require)
a return value. The following shows how the value built-in function is invoked as a subroutine.
call VALUE 'Destination', 'Nice', 'Vacation' /* assigns new destination */
Processing state information is SAVED during function and subroutine calls
The processing state of the following information is saved when a function or subroutine call is
performed. The state of this information is restored when the function or subroutine returns
pending control structures
if ... then ... else ...
do ... end
do i=1 to n ... end [and similar do loops]
select when ... then ... ...etc otherwise ... end
numeric option settings
digits, fuzz, form
command address settings
current and saved destinations
LOVELY PROFESSIONAL UNIVERSITY 179