Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
v3:subroutines [2022/01/27 12:51] – [Defining Subroutines] neilsv3:subroutines [2024/07/30 22:43] (current) – [Overloading] neils
Line 25: Line 25:
   END SUB   END SUB
      
-  CALL greet("Emily": REM will display: Hello, Emily +  CALL greet("Emily"will display: Hello, Emily 
-  CALL greet("Mark": REM will display: Hello, Mark+  CALL greet("Mark"will display: Hello, Mark
  
 The ''CALL'' command will evaluate the argument list in the parentheses, pass all arguments to the subroutine and then instruct the computer to continue the program at the top of the subroutine. The ''CALL'' command will evaluate the argument list in the parentheses, pass all arguments to the subroutine and then instruct the computer to continue the program at the top of the subroutine.
Line 109: Line 109:
 ===== Overloading ===== ===== Overloading =====
  
-When passing arguments to a subroutinethe compiler will match the number of arguments in the ''CALL'' statement to the number of arguments in the subroutine declaration. If the number or arguments do not match, a compile-time error is emitted.+Subroutine overloadingcommonly known as method overloading in object-oriented programming languages, refers to the ability to create multiple subroutines with the same name but different parameters. This feature allows a programmer to define different ways to call a subroutine based on the types and number of arguments passed.
  
-If the number of arguments matchthe compiler will compare each passed argument'type to the variable type the subroutine accepts. If the passed type can be converted to the accepted type, it will be silently converted. If however the types are not convertible (for example the subroutine accepts a numeric argument and the calling statement attempts to pass a string), compilation will fail with an error.+Overloaded subroutines have the same name but differ in the type, number, or both type and number of parameters.
  
-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 ====
  
-  SUB test (AS INTSTATIC +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: 
 + 
 +  SUB PrintMessage(msg AS STRING * 16
 +      PRINT msg
   END SUB   END SUB
      
-  SUB test (AS STRING * 16) OVERLOAD STATIC +  SUB PrintMessage(msg AS STRING * 16, num AS INT) OVERLOAD 
-    PRINT "a is a string: "; a+      PRINT msg; " "; num
   END SUB   END SUB
      
-  CALL test(5+  CALL PrintMessage("Hello, XC=BASIC!"
-  CALL test("hello")+  CALL PrintMessage("The number is", 42)
  
 <adm warning> <adm warning>