Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
v3:subroutines [2022/01/27 12:51] – [Defining Subroutines] neils | v3:subroutines [2024/07/30 22:43] (current) – [Overloading] neils | ||
---|---|---|---|
Line 7: | Line 7: | ||
Subroutines are named routines that accept zero or more arguments. The simplest syntax to define a subroutine is the following: | Subroutines are named routines that accept zero or more arguments. The simplest syntax to define a subroutine is the following: | ||
- | SUB < | + | SUB < |
< | < | ||
END SUB | END SUB | ||
Line 25: | Line 25: | ||
END SUB | END SUB | ||
| | ||
- | CALL greet(" | + | CALL greet(" |
- | CALL greet(" | + | CALL greet(" |
The '' | The '' | ||
Line 109: | Line 109: | ||
===== Overloading ===== | ===== Overloading ===== | ||
- | When passing arguments to a subroutine, the compiler will match the number of arguments | + | Subroutine overloading, commonly known as method overloading |
- | If the number | + | Overloaded subroutines have the same name but differ in the type, number, |
- | But what if the programmer desires a subroutine that behaves differently depending on the number or type of arguments that are passed? This is possible using overloading. A subroutine may have as many variations as desired. If there is more than one variation, the compiler will locate the best match among the candidates for the call. Consider the following example: | + | ==== Compile-Time Polymorphism ==== |
- | | + | The appropriate subroutine to call is determined at compile-time based on the arguments provided in the call. |
- | PRINT "a is an integer: "; a | + | |
+ | Consider the following example: | ||
+ | |||
+ | | ||
+ | PRINT msg | ||
END SUB | END SUB | ||
| | ||
- | SUB test (a AS STRING * 16) OVERLOAD | + | SUB PrintMessage(msg AS STRING * 16, num AS INT) OVERLOAD |
- | PRINT "a is a string: | + | PRINT msg; " "; |
END SUB | END SUB | ||
| | ||
- | CALL test(5) | + | CALL PrintMessage(" |
- | CALL test("hello") | + | CALL PrintMessage("The number is", 42) |
<adm warning> | <adm warning> |