Fundamentos

Esta sección fornece os fundamentos para traballar con LibreOffice Basic.

LibreOffice Basic code is based on subroutines and functions that are specified between sub...end sub and function...end function sections. Each Sub or Function can call other Subs and Functions. If you take care to write generic code for a Sub or Function, you can probably re-use it in other programs. See also Procedures and Functions.

Icona Nota

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.


Que é un método?

Sub is the short form of subroutine, that is used to handle a certain task within a program. Subs are used to split a task into individual procedures. Splitting a program into procedures and sub-procedures enhances readability and reduces the error-proneness. A sub possibly takes some arguments as parameters but does not return any values back to the calling sub or function, for example:

FacerAlgoCosValores(OMeuPrimeiroValor,OMeuSegundoValor)

Que é unha función?

A function is essentially a sub, which returns a value. You may use a function at the right side of a variable declaration, or at other places where you normally use values, for example:

OMeuSegundoValor = AMiñaFunción(OMeuPrimeiroValor)

Variábeis globais e locais

As variábeis globais son válidas para os métodos e funcións dun módulo. Decláranse no inicio dun módulo, antes de comezar o primeiro método ou función.

As variábeis declaradas dentro dun método ou función só son válidas dentro dese método ou función e substitúen as variábeis globais e locais co mesmo nome provenientes de métodos ou funcións superiores.

Estrutura

After separating your program into procedures and functions (Subs and Functions), you can save these procedures and functions as files for reuse in other projects. LibreOffice Basic supports Modules and Libraries. Subs and functions are always contained in modules. You can define modules to be global or part of a document. Multiple modules can be combined to a library.

You can copy or move subs, functions, modules and libraries from one file to another by using the Macro dialog.