1
1
Fork 0
mirror of https://github.com/QB64-Phoenix-Edition/QB64pe.git synced 2024-07-01 13:50:36 +00:00
QB64-PE/internal/help/BYVAL.txt
2017-10-10 11:55:21 -03:00

65 lines
3.1 KiB
Plaintext

The [[BYVAL]] statement is used to pass a numerical parameter's value with procedures made in other programming languages.
{{PageSyntax}}
: SUB ProcedureName ([[BYVAL]] {{Parameter|variable1}}, [[BYVAL]] {{Parameter|variable2}})
{{PageDescription}}
* '''QB64''' can only use BYVAL in [[DECLARE LIBRARY]] procedures that add DLL or Operating System API functions.
* Supported with [[DECLARE LIBRARY]] [[SUB]] and [[FUNCTION]] procedure declarations when passing '''numerical values only'''.
* Passing numerical values BYVAL assures that the original numerical variable value is not changed by another procedure.
* Use [[parenthesis]] around program [[SUB]] or [[FUNCTION]] parameters passed '''by value'''. Ex: ''CALL Procedure ((x&), (y&))''
{{PageErrors}}
* '''Do not use BYVAL before [[STRING]] or [[TYPE]] variables or in regular prograam [[SUB]] or [[FUNCTION]] parameters.'''
* '''NOTE: Many QBasic keywords can be used as variable names if they are created as [[STRING]]s using the suffix '''$'''. You cannot use them without the suffix, use a numerical suffix or use [[DIM]], [[REDIM]], [[_DEFINE]], [[BYVAL]] or [[TYPE]] variable [[AS]] statements.'''
==QBasic/QuickBASIC==
* QBasic versions below 7 do not use BYVAL unless the [[SUB]] program referred to is from a different programming language.
* PDS versions can use BYVAL as it is intended in any [[SUB]] or [[FUNCTION]] parameters.
* BYVAL could also be used with [[ABSOLUTE]] in QBasic.
{{PageExamples}}
''Example 1:'' BYVAL is used to preserve the values sent to an external procedure so they remain the same after they are used:
{{CodeStart}} '' ''
{{Cl|DECLARE LIBRARY}} "SDL"
{{Cl|SUB}} MouseMove {{Cl|ALIAS}} SDL_WarpMouse ({{Cl|BYVAL}} xoffset&, {{Cl|BYVAL}} yoffset&)
{{Cl|DECLARE LIBRARY|END DECLARE}}
{{CodeEnd}}
: ''Note:'' The DLL call above uses the SDL library, which was included with QB64 up to version 0.954. Won't work with '''version 1.000 and up'''.
''Example 2:'' Passing parameters "by value" using [[parenthesis|brackets]] when calling a [[SUB]] or [[FUNCTION]] in QBasic or QB64.
{{CodeStart}} '' ''
{{Cl|CALL}} MySUB (a%, (b%), (c%)) 'CALL SUB b and c stay 0 after sub
MySUB a%, b%, (c%) 'call SUB again without CALL only c stays 0 after sub
{{Cl|PRINT}} "After procedures: "; a%, b%, c%
{{Cl|SUB}} MySUB (a%, b%, c%)
a% = a% + 1: b% = b% + 1: c% = c% + 1
{{Cl|PRINT}} "Inside procedure: "; a%, b%, c%
{{Cl|END SUB}} '' ''
{{CodeEnd}}
{{OutputStart}}
Inside procedure: 1 1 1
Inside procedure: 2 1 1
After procedures: 2 1 0
{{OutputEnd}}
:''Explanation:'' Both SUB calls pass just the '''values''' of b% and c% to the procedure. The first variable, a%, is passed by '''reference''' (the default) so the value was changed by the SUB procedure. Brackets can only be used in the [[CALL]] or function reference.
{{PageSeeAlso}}
* [[DECLARE LIBRARY]]
* [[SUB]], [[FUNCTION]]
* [[CALL]], [[ALIAS]]
* [[DECLARE]], [[DECLARE (non-BASIC statement)]] {{text|(not used by QB64)}}
* [[Libraries#C++_Variable_Types|C++ Variable Types]]
{{PageNavigation}}