Call a function

Revision as of 15:03, 15 July 2011 by rosettacode>Markhobley ({{header|ZX Spectrum Basic}})

The task is to demonstrate the different syntax and semantics that the language may provide for calling a function. This may include:

  • Calling a function that requires no arguments
  • Calling a function with a fixed number of arguments
  • Calling a function with a variable number of arguments (varargs)
  • Calling a function with named arguments
  • Using a function in command context
  • Using a function in first class context within an expression
  • Obtaining the return value of a function
  • Demonstrating any differences that apply to calling builtin functions rather than user defined functions
  • Demonstrating differences between calling subroutines and functions, if these are separate cases within the language.
Call a function is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Note this task is about methods of calling functions, not methods of defining functions, which is a separate topic.

ZX Spectrum Basic

On the ZX Spectrum, functions and subroutines are separate entities. A function is limited to generating a return value. A subroutine can perform input and output.

<lang zxbasic> 10 REM functions cannot be called in statement context 20 PRINT FN a(5): REM The function is used in first class context 30 PRINT FN b(): REM Here we call a function that has no arguments 40 REM parameters cannot be passed parameters, however variables are global 50 LET n=1: REM This variable will be visible to the called subroutine 60 GOSUB 1000: REM subroutines are called by line number and do not have names 70 REM subroutines do not return a value, but we can see any variables it defined 80 REM subroutines cannot be used in first class context 90 REM builtin functions are used in first class context, and do not need the FN keyword prefix 100 PRINT SIN(50): REM here we pass a parameter to a builtin function 110 PRINT RND(): REM here we use a builtin function without parameters </lang>