Using Procedures, Functions and Properties

The following describes the basic use of procedures, functions and properties in LibreOffice Basic.

note

When you create a new module, LibreOffice Basic automatically inserts a Sub called "Main". This default name has nothing to do with the order or the starting point of a LibreOffice Basic project. You can also safely rename this Subroutine.


note

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.

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:


  Sub SubName(Parameter1 As TYPENAME, Parameter2 As TYPENAME,...)
      ' your code goes here
  End Sub

The Sub is called using the following syntax:


  [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:


  Function FunctionName(Parameter1 As TYPENAME, Parameter2 As TYPENAME,...) As TYPENAME
      ' your code goes here
      FunctionName=Result
  End Function

The Function is called using the following syntax:


  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
      IsApproved = some_computation
  End Property
  Property Let IsApproved(value As TYPENAME)
      ' your code goes here
      _IsApproved = computed_value
  End Property

The Property is called using the following syntax:


  var = IsApproved
  IsApproved = some_value
tip

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()


Változók átadása értékként vagy hivatkozásként

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.

Defining Optional Parameters

Functions, procedures or properties can be defined with optional parameters, for example:


  Sub Rounding(number, Optional decimals, Optional format)
      ' your code goes here
  End Sub

Positional or Keyword Arguments

Amikor egy függvényt vagy eljárást hív, az argumentumokat pozíció vagy név szerint adhatja át. A pozíció szerinti átadás azt jelenti, hogy az argumentumokat abban a sorrendben sorolja fel, ahogyan a paraméterek a függvényben vagy eljárásban definiálva vannak. A név szerinti átadáshoz az argumentum elé kell írni a megfelelő paraméter nevét, amelyet egy kettőspont és egy egyenlőségjel követ (:=). A kulcsszavas argumentumok tetszőleges sorrendben szerepelhetnek. Az ilyen példákat lásd a Basic Replace() függvényben.

Ha kevesebb paramétert kell átadni, használjon kulcsszavas argumentumokat. Kevesebb paraméter értékének pozíció szerinti átadása megköveteli, hogy az összes paraméter értékét megadjuk előttük, akár opcionálisak, akár nem. Ez biztosítja, hogy az értékek a megfelelő pozícióban legyenek. Ha a paramétereket név szerint adja át - kulcsszavas argumentumok használatával -, akkor minden más közbenső argumentumot elhagyhat.

Változók hatóköre

Egy Sub, egy Function vagy egy Property procedúrán belül definiált változó csak az eljárás kilépéséig marad érvényben. Ezt nevezzük "lokális" változónak. Sok esetben szükség van arra, hogy egy változó minden eljárásban, minden könyvtár minden moduljában, vagy egy Sub, egy Function vagy egy Property procedúra kilépése után is érvényes legyen.

Declaring Variables Outside a Sub a Function or a Property


Global VarName As TYPENAME

The variable is valid as long as the LibreOffice session lasts.


Public VarName As TYPENAME

A változó minden modulban érvényes.


Private VarName As TYPENAME

A változó csak ebben a modulban érvényes.


Dim VarName As TYPENAME

A változó csak ebben a modulban érvényes.

Példa privát változókra

Enforce private variables to be private across modules by setting CompatibilityMode(True).


  ' ***** Module1 *****
  Private myText As String
  Sub initMyText
      myText = "Hello"
      Print "1. modulban: ", myText
  End Sub
   
  ' ***** Module2 *****
  'Option Explicit
  Sub demoBug
      CompatibilityMode( True )
      initMyText
      ' Üres karakterláncot ad vissza
      ' (or raises error for Option Explicit)
      Print "2. modulban: ", myText
  End Sub

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


  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.

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:


  Function WordCount(WordText As String) As Integer

Támogasson minket!