Procedūru un funkciju lietošana

Sekojošais apraksta procedūru un funkciju pamata lietojumu iekš LibreOffice Basic.

Piezīmes ikona

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 SUB.


Piezīmes ikona

Some restrictions apply for the names of your public variables, subs, and functions. You must not use the same name as one of the modules of the same library.


Procedures (SUBS) and functions (FUNCTIONS) help you maintaining a structured overview by separating a program into logical pieces.

One benefit of procedures and functions is that, once you have developed a program code containing task components, you can use this code in another project.

Mainīgo padošana procedūrām (SUB) un funkcijām (FUNCTION)

Variables can be passed to both procedures and functions. The SUB or FUNCTION must be declared to expect parameters:


Sub SubName(Parameter1 As Type, Parameter2 As Type,...)
Programmas kods
End Sub

SUB tiek izsaukts, lietojot sekojošu sintaksi:


SubName(Value1, Value2,...)

The parameters passed to a SUB must fit to those specified in the SUB declaration.

The same process applies to FUNCTIONS. 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 Type, Parameter2 As Type,...) As Type
Programmas kods
FunkcijasNosaukums=Rezultāts
End Function

FUNCTION tiek izsaukts, lietojot sekojošu sintaksi:


Mainīgais=FunkcijasNosaukums(Parametrs1, Parametrs2,...)
Padoma ikona

You can also use the fully qualified name to call a procedure or function:
Library.Module.Macro()
For example, to call the Autotext macro from the Gimmicks library, use the following command:
Gimmicks.AutoText.Main()


Mainīgo padošana ar vērtību vai atsauci

Parameters can be passed to a SUB or a FUNCTION either by reference or by value. Unless otherwise specified, a parameter is always passed by reference. That means that a SUB or a FUNCTION 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 or FUNCTION, for example:


Rezultāts = Funkcija(ByVal Parametrs)

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.

Mainīgo darbības apgabals

A variable defined within a SUB or FUNCTION, 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 or FUNCTION is exited.

Mainīgo deklarēšana ārpus SUB vai FUNCTION


Global MainīgāNosaukums As TIPANOSAUKUMS

Mainīgais ir derīgs tikmēr, kamēr ilgst LibreOffice sesija.


Public MainīgāNosaukums As TIPANOSAUKUMS

Mainīgais ir derīgs visos moduļos.


Private MainīgāNosaukums As TIPANOSAUKUMS

Mainīgais ir derīgs tikai šajā modulī.


Dim MainīgāNosaukums As TIPANOSAUKUMS

Mainīgais ir derīgs tikai šajā modulī.

Piemēri privātajiem mainīgajiem

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


' ***** Module1 *****
Private myText As String
Sub initMyText
    mansTeksts = "Sveicināti"
    Print "In module1 : ", myText
End Sub
 
' ***** Module2 *****
'Option Explicit
Sub demoBug
    CompatibilityMode( true )
    initMyText
    ' Tagad atgriež tukšu virkni
    ' (or raises error for Option Explicit)
    Print "Now in module2 : ", myText
End Sub

Mainīgo satura saglabāšana pēc iziešanas no SUB vai FUNCTION


Static MainīgāNosaukums As TIPANOSAUKUMS

The variable retains its value until the next time the FUNCTION or SUB is entered. The declaration must exist inside a SUB or a FUNCTION.

FUNCTION atgrieztās vērtības tipa norādīšana

As with variables, include a type-declaration character after the function name, or the type indicated by "As" and the corresponding key word at the end of the parameter list to define the type of the function's return value, for example:


Function WordCount(WordText As String) As Integer

Please support us!