From 258bcb03c8a488b14413b6fb7f8bdd88309d8c69 Mon Sep 17 00:00:00 2001 From: Pierre F Date: Fri, 16 Aug 2024 17:52:52 +0200 Subject: misc. simplify sentences (Basic Function call) Change-Id: I9a597897f60642832089973926c67f1ac69ed55d Reviewed-on: https://gerrit.libreoffice.org/c/help/+/171780 Tested-by: Jenkins Reviewed-by: Olivier Hallot --- source/text/sbasic/shared/01020300.xhp | 75 +++++++++++++++++----------------- 1 file changed, 37 insertions(+), 38 deletions(-) (limited to 'source/text') diff --git a/source/text/sbasic/shared/01020300.xhp b/source/text/sbasic/shared/01020300.xhp index 999c5645eb..4d0db94853 100644 --- a/source/text/sbasic/shared/01020300.xhp +++ b/source/text/sbasic/shared/01020300.xhp @@ -19,12 +19,12 @@ --> - Using Procedures, Functions or Properties + Using Procedures, Functions or Properties /text/sbasic/shared/01020300.xhp - + procedures functions;using variables;passing to procedures, functions, properties @@ -44,59 +44,58 @@ When you create a new module, %PRODUCTNAME Basic automatically inserts a Sub called "Main". This default name has nothing to do with the order or the starting point of a %PRODUCTNAME Basic project. You can also safely rename this Subroutine. Some restrictions apply for the names of your public variables, subroutines, functions and properties. You must not use the same name as one of the modules of the same library. -Procedures (Subroutines) functions (Function) and properties (Property) help you maintaining a structured overview by separating a program into logical pieces. -One benefit of procedures, functions and properties is that, once you have developed a program code containing task components, you can use this code in another project. +Procedures (Subroutines) functions (Function) and properties (Property) help you maintaining a structured overview by separating a program into logical pieces. These pieces can be easily reused to perform similar tasks in other projects.

Passing Variables to Procedures, Functions or Properties

-Variables can be passed to both procedures, functions or properties. The Sub Function or Property must be declared to expect parameters: +Variables can be passed to both procedures, functions or properties. The Sub Function or Property must be declared to expect parameters: Sub SubName(Parameter1 As TYPENAME, Parameter2 As TYPENAME,...) - ' your code goes here + ' your code goes here End Sub -The Sub is called using the following syntax: +The Sub is called using the following syntax: - [Call] SubName( [Parameter1:=]Value1, [Parameter2:=]Value2, ...) + [Call] SubName( [Parameter1:=]Value1, [Parameter2:=]Value2, ...) -The parameters passed to a Sub must fit to those specified in the Sub declaration. -The same process applies to a Function. In addition, functions always return a function result. The result of a function is defined by assigning the return value to the function name: +The parameters passed to a Sub must fit to those specified in the Sub declaration. +The same process applies to a Function. In addition, functions always return a result. This result is defined by assigning the value to return to the function name: Function FunctionName(Parameter1 As TYPENAME, Parameter2 As TYPENAME,...) As TYPENAME - ' your code goes here + ' your code goes here FunctionName=Result End Function -The Function is called using the following syntax: +The Function is called using the following syntax: - Variable = FunctionName( [Parameter1:=]Value1, [Parameter2:=]Value2, ...) + Variable = FunctionName( [Parameter1:=]Value1, [Parameter2:=]Value2, ...) Properties combine the syntax of procedures and functions. A Property usually requires up to one parameter. Private _IsApproved As TYPENAME Property Get IsApproved As TYPENAME - ' your code goes here + ' your code goes here IsApproved = some_computation End Property Property Let IsApproved(value As TYPENAME) - ' your code goes here + ' your code goes here _IsApproved = computed_value End Property -The Property is called using the following syntax: +The Property is called using the following syntax: var = IsApproved IsApproved = some_value You can also use the fully qualified name to call a procedure, function or property:
[Call] Library.Module.Macro(), where Call is optional.
For example, to call the Autotext macro from the Gimmicks library, use the following command:
Gimmicks.AutoText.Main()

Passing Variables by Value or Reference

-Parameters can be passed to a procedure, a function or a property either by reference or by value. Unless otherwise specified, a parameter is always passed by reference. That means that a Sub, a Function or a Property gets the parameter and can read and modify its value. -If you want to pass a parameter by value insert the key word ByVal in front of the parameter when you call a Sub, a Function or a Property, for example: +Parameters can be passed to a procedure, a function or a property either by reference or by value. Unless otherwise specified, a parameter is always passed by reference. That means that a Sub, a Function or a Property gets the parameter and can read and modify its value. +If you want to pass a parameter by value insert the key word ByVal in front of the parameter when you call a Sub, a Function or a Property, for example: Function ReadOnlyParms(ByVal p2, ByVal p2) ' your code goes here End Function result = ReadOnlyParms(parm1, parm2) -In this case, the original content of the parameter will not be modified by the Function since it only gets the value and not the parameter itself. +In this case, the original content of the parameter will not be modified by the Function since it only gets the value and not the parameter itself.

Defining Optional Parameters

Functions, procedures or properties can be defined with optional parameters, for example: @@ -110,32 +109,32 @@ When needing to pass less parameters, use keywords arguments. Passing values for fewer parameters by position requires to supply values for all parameters before them, optional or not. This ensures that the values are in the correct positions. If you pass the parameters by name - using keyword arguments - you may omit all other intermediate arguments.

Scope of Variables

-A variable defined within a Sub, a Function or a Property, only remains valid until the procedure is exited. This is known as a "local" variable. In many cases, you need a variable to be valid in all procedures, in every module of all libraries, or after a Sub, a Function or a Property is exited. +A variable defined within a Sub, a Function or a Property, only remains valid until the procedure is exited. This is known as a "local" variable. In many cases, you need a variable to be valid in all procedures, in every module of all libraries, or after a Sub, a Function or a Property is exited.

Declaring Variables Outside a Sub a Function or a Property

-Global VarName As TYPENAME +Global VarName As TYPENAME -The variable is valid as long as the %PRODUCTNAME session lasts. +The variable is valid as long as the %PRODUCTNAME session lasts. -Public VarName As TYPENAME +Public VarName As TYPENAME -The variable is valid in all modules. +The variable is valid in all modules. -Private VarName As TYPENAME +Private VarName As TYPENAME -The variable is only valid in this module. +The variable is only valid in this module. -Dim VarName As TYPENAME +Dim VarName As TYPENAME -The variable is only valid in this module. +The variable is only valid in this module.

Example for private variables

-Enforce private variables to be private across modules by setting CompatibilityMode(True).from i17948, see i54894 +Enforce private variables to be private across modules by setting CompatibilityMode(True).from i17948, see i54894 ' ***** Module1 ***** Private myText As String Sub initMyText - myText = "Hello" - Print "In module1 : ", myText + myText = "Hello" + Print "In module1 : ", myText End Sub ' ***** Module2 ***** @@ -143,20 +142,20 @@ Sub demoBug CompatibilityMode( True ) initMyText - ' Now returns empty string - ' (or raises error for Option Explicit) - Print "Now in module2 : ", myText + ' Now returns empty string + ' (or raises error for Option Explicit) + Print "Now in module2 : ", myText End Sub

Saving Variable Content after Exiting a Sub a Function or a Property

- Static VarName As TYPENAME + Static VarName As TYPENAME -The variable retains its value until the next time the a Function, Sub or Property is entered. The declaration must exist inside a Sub, a Function or a Property. +The variable retains its value until the next time the a Function, Sub or Property is entered. The declaration must exist inside a Sub, a Function or a Property.

Specifying the Return Value Type of a Function or a Property

-As with variables, include a type-declaration character after the function name, or the type indicated by As and the corresponding data type at the end of the parameter list to define the type of the function or property's return value, for example: +As with variables, include a type-declaration character after the function name, or the type indicated by As and the corresponding data type at the end of the parameter list to define the type of the function or property's return value, for example: - Function WordCount(WordText As String) As Integer + Function WordCount(WordText As String) As Integer
-- cgit