7.1.1 How to write itSubroutine «nombre» ( «parámetros» ) ⟦ As «tipo» ⟧
«instrucciones»
EndSubroutine
Function «resultado» <- «nombre» ( «parámetros» )
Define «resultado» As «tipo» ;
«instrucciones»
EndFunction Parts
Subroutine opens a subprogram. The parentheses are required even with no parameters.As «type» is optional: with it, the subprogram returns a value; without it, it only does things.Function declares a function with a result variable : the value «result» holds at the end is what is returned.EndSubroutine and EndFunction close each form.7.1.2 How it worksSubprograms are declared next to the main algorithm, never inside it nor inside another subprogram. Order does not matter: you can call one that is written further down.
Program A procedure and a function Subroutine Saludar ( nombre As String )
Write "Hola, " , nombre ;
EndSubroutine
Subroutine Doble ( n As Integer ) As Integer
Return n * 2 ;
EndSubroutine
Algorithm Principal
Saludar ( "Ana" ) ;
Write Doble ( 21 ) ;
EndAlgorithm The program finishes normally.
Figure 7.1-1. A subprogram seen from outside: the arguments go in, the result comes out. What happens inside is hidden.A box called Doble. The argument 21 goes in on the left and the result 42 comes out on the right.
The Function form is handy when the result is built up little by little. It has no Return with a value: it returns whatever its result variable holds.
Program Function with a result variable Function area <- AreaRectangulo ( base As Real , alto As Real )
Define area As Real ;
area <- base * alto ;
EndFunction
Algorithm Principal
Write AreaRectangulo ( 3.0 , 4.5 ) ;
EndAlgorithm The program finishes normally.
Inside a subprogram everything that works in the algorithm works too: declarations, Read , Write , decisions and loops. The variables it declares are local : nobody else sees them.
Limit
The return type can be a primitive or a class, never an array. A Function is pure : it cannot write, read or call anything that does. Common mistakes
A subprogram with a return type needs at least one Return :
Program A return type without Return Subroutine Siete ( ) As Integer
Write "sin retorno" ;
EndSubroutine
Algorithm Principal
Write Siete ( ) ;
EndAlgorithm Diagnostics
subroutineWithoutReturn · error · 1:1 · Subroutine Siete declares a return type but its body contains no return statementThe program finishes with an error.
Under the hood
Every subprogram is a function of the intermediate representation with its own blocks. A call is a call instruction that names the target function.
Intermediate representation
module Principal en
func @Saludar(%0: str) -> unit {
b0() mass 1:
stmt.enter @2:3-2:25
%1 = const.str "Hola, " @2:9-2:17
out.write %1, %0, newline @2:3-2:25
stmt.exit @2:3-2:25
return @3:1-3:14
}
func @Doble(%0: i64) -> i64 {
b0() mass 1:
stmt.enter @6:3-6:15
%1 = const.i64 2 @6:14-6:15
%2 = int.mul %0, %1 @6:12-6:13
stmt.exit @6:3-6:15
return %2 @6:3-6:15
}
func suspendible @main() -> unit {
b0() mass 2:
stmt.enter @10:3-10:17
%0 = const.str "Ana" @10:11-10:16
%1 = call @Saludar(%0) @10:3-10:17
stmt.exit @10:3-10:17
stmt.enter @11:3-11:18
%2 = const.i64 21 @11:15-11:17
%3 = call @Doble(%2) @11:9-11:18
out.write %3, newline @11:3-11:18
stmt.exit @11:3-11:18
return @12:1-12:13
} Note
Before writing subprograms, find two almost identical pieces in a long program. That is the first candidate for a subprogram.
In short
Subroutine declares a subprogram; with As type it returns a value.Function returns what its result variable holds and is pure.They are declared next to the algorithm, in any order. Their variables are local. See also