summaryrefslogtreecommitdiff
path: root/source/text/sbasic/shared/01020300.xhp
blob: d3d1dd08ef2997626b4c320997fd63278a7fc331 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?xml version="1.0" encoding="UTF-8"?>
<helpdocument version="1.0">
<!--
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
-->

<meta>
  <topic id="textsbasicshared01020300xml" indexer="include" status="PUBLISH">
    <title id="tit" xml-lang="en-US">Using Procedures and Functions</title>
    <filename>/text/sbasic/shared/01020300.xhp</filename>
  </topic>
</meta>

<body>


<section id="prozedur">
<bookmark xml-lang="en-US" branch="index" id="bm_id3149456">
  <bookmark_value>procedures</bookmark_value>
  <bookmark_value>functions;using</bookmark_value>
  <bookmark_value>variables;passing to procedures and functions</bookmark_value>
  <bookmark_value>parameters;for procedures and functions</bookmark_value>
  <bookmark_value>parameters;passing by reference or value</bookmark_value>
  <bookmark_value>variables;scope</bookmark_value>
  <bookmark_value>scope of variables</bookmark_value>
  <bookmark_value>GLOBAL variables</bookmark_value>
  <bookmark_value>PUBLIC variables</bookmark_value>
  <bookmark_value>PRIVATE variables</bookmark_value>
  <bookmark_value>functions;return value type</bookmark_value>
  <bookmark_value>return value type of functions</bookmark_value>
</bookmark>


<paragraph id="hd_id3149456" role="heading" level="1" xml-lang="en-US"><link href="text/sbasic/shared/01020300.xhp">Using Procedures and Functions</link></paragraph>
<paragraph id="par_id3150767" role="paragraph" xml-lang="en-US">The following describes the basic use of procedures and functions in $[officename] Basic.</paragraph>
</section>
<paragraph id="par_id3151215" role="note" xml-lang="en-US">When you create a new module, $[officename] Basic automatically inserts a SUB called "Main". This default name has nothing to do with the order or the starting point of a $[officename] Basic project. You can also safely rename this SUB.</paragraph>
<paragraph id="par_id314756320" role="note" xml-lang="en-US">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.</paragraph>
<paragraph id="par_id3154124" role="paragraph" xml-lang="en-US">Procedures (SUBS) and functions (FUNCTIONS) help you maintaining a structured overview by separating a program into logical pieces.</paragraph>
<paragraph id="par_id3153193" role="paragraph" xml-lang="en-US">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.</paragraph>

<paragraph id="hd_id3153770" role="heading" level="2" xml-lang="en-US">Passing Variables to Procedures (SUB) and Functions (FUNCTION)</paragraph>
<paragraph id="par_id3155414" role="paragraph" xml-lang="en-US">Variables can be passed to both procedures and functions. The SUB or FUNCTION must be declared to expect parameters:</paragraph>
<bascode>
<paragraph id="par_idm1340846688" role="bascode" localize="false" xml-lang="en-US">Sub SubName(Parameter1 As Type, Parameter2 As Type,...)</paragraph>
<paragraph id="par_id3151114" role="bascode" xml-lang="en-US">Program code</paragraph>
<paragraph id="par_idm1340843712" role="bascode" localize="false" xml-lang="en-US">End Sub</paragraph>
</bascode>
<paragraph id="par_id3152577" role="paragraph" xml-lang="en-US">The SUB is called using the following syntax:</paragraph>
<bascode>
<paragraph id="par_idm1341029952" role="bascode" localize="false" xml-lang="en-US">SubName(Value1, Value2,...)</paragraph>
</bascode>
<paragraph id="par_id3147124" role="paragraph" xml-lang="en-US">The parameters passed to a SUB must fit to those specified in the SUB declaration.</paragraph>
<paragraph id="par_id3147397" role="paragraph" xml-lang="en-US">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:</paragraph>
<bascode>
<paragraph id="par_idm1341024672" role="bascode" localize="false" xml-lang="en-US">Function FunctionName(Parameter1 As Type, Parameter2 As Type,...) As Type</paragraph>
<paragraph id="par_id3156284" role="bascode" xml-lang="en-US">Program code</paragraph>
<paragraph id="par_id3145799" role="bascode" xml-lang="en-US">FunctionName=Result</paragraph>
<paragraph id="par_idm1341019952" role="bascode" localize="false" xml-lang="en-US">End Function</paragraph>
</bascode>
<paragraph id="par_id3153839" role="paragraph" xml-lang="en-US">The FUNCTION is called using the following syntax:</paragraph>
<bascode>
<paragraph id="par_id3146914" role="bascode" xml-lang="en-US">Variable=FunctionName(Parameter1, Parameter2,...)</paragraph>
</bascode>
<paragraph id="par_idN107B3" role="tip" xml-lang="en-US">You can also use the fully qualified name to call a procedure or function:<br/> <item type="literal">Library.Module.Macro()</item> <br/> For example, to call the Autotext macro from the Gimmicks library, use the following command:<br/> <item type="literal">Gimmicks.AutoText.Main()</item> </paragraph>

<paragraph id="hd_id3156276" role="heading" level="2" xml-lang="en-US">Passing Variables by Value or Reference</paragraph>
<paragraph id="par_id3155765" role="paragraph" xml-lang="en-US">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.</paragraph>
<paragraph id="par_id3145640" role="paragraph" xml-lang="en-US">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:</paragraph>
<bascode>
<paragraph id="par_id3150042" role="bascode" xml-lang="en-US">Result = Function(ByVal Parameter)</paragraph>
</bascode>
<paragraph id="par_id3149258" role="paragraph" xml-lang="en-US">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.</paragraph>

<paragraph id="hd_id3150982" role="heading" level="2" xml-lang="en-US">Scope of Variables</paragraph>
<paragraph id="par_id3149814" role="paragraph" xml-lang="en-US">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.</paragraph>

<paragraph id="hd_id3154186" role="heading" level="3" xml-lang="en-US">Declaring Variables Outside a SUB or FUNCTION</paragraph>
<bascode>
<paragraph id="par_id3150208" role="bascode" xml-lang="en-US">Global VarName As TYPENAME</paragraph>
</bascode>
<paragraph id="par_id3145258" role="paragraph" xml-lang="en-US">The variable is valid as long as the $[officename] session lasts.</paragraph>
<bascode>
<paragraph id="par_id3153198" role="bascode" xml-lang="en-US">Public VarName As TYPENAME</paragraph>
</bascode>
<paragraph id="par_id3150088" role="paragraph" xml-lang="en-US">The variable is valid in all modules.</paragraph>
<bascode>
<paragraph id="par_id3158212" role="bascode" xml-lang="en-US">Private VarName As TYPENAME</paragraph>
</bascode>
<paragraph id="par_id3152994" role="paragraph" xml-lang="en-US">The variable is only valid in this module.</paragraph>
<bascode>
<paragraph id="par_id3150886" role="bascode" xml-lang="en-US">Dim VarName As TYPENAME</paragraph>
</bascode>
<paragraph id="par_id3150368" role="paragraph" xml-lang="en-US">The variable is only valid in this module.</paragraph>

<paragraph id="hd_id5097506" role="heading" level="3" xml-lang="en-US">Example for private variables</paragraph>
<paragraph id="par_id8738975" role="paragraph" xml-lang="en-US">Enforce private variables to be private across modules by setting CompatibilityMode(true).</paragraph><comment>from i17948, see i54894</comment>
<bascode>
<paragraph id="par_idm1340976400" role="bascode" localize="false" xml-lang="en-US">' ***** Module1 *****</paragraph>
<paragraph id="par_idm1340975168" role="bascode" localize="false" xml-lang="en-US">Private myText As String</paragraph>
<paragraph id="par_idm1340973920" role="bascode" localize="false" xml-lang="en-US">Sub initMyText</paragraph>
<paragraph id="par_id9475997" role="bascode" xml-lang="en-US">    myText = "Hello"</paragraph>
<paragraph id="par_id6933500" role="bascode" xml-lang="en-US">    Print "In module1 : ", myText</paragraph>
<paragraph id="par_idm1340970208" role="bascode" localize="false" xml-lang="en-US">End Sub</paragraph>
<paragraph id="par_idm1340968976" role="bascode" localize="false" xml-lang="en-US"> </paragraph>
<paragraph id="par_idm1340967728" role="bascode" localize="false" xml-lang="en-US">' ***** Module2 *****</paragraph>
<paragraph id="par_idm1340966496" role="bascode" localize="false" xml-lang="en-US">'Option Explicit</paragraph>
<paragraph id="par_idm1340965264" role="bascode" localize="false" xml-lang="en-US">Sub demoBug</paragraph>
<paragraph id="par_idm1340964032" role="bascode" localize="false" xml-lang="en-US">    CompatibilityMode( true )</paragraph>
<paragraph id="par_idm1340962784" role="bascode" localize="false" xml-lang="en-US">    initMyText</paragraph>
<paragraph id="par_id4104129" role="bascode" xml-lang="en-US">    ' Now returns empty string</paragraph>
<paragraph id="par_id7906125" role="bascode" xml-lang="en-US">    ' (or raises error for Option Explicit)</paragraph>
<paragraph id="par_id8055970" role="bascode" xml-lang="en-US">    Print "Now in module2 : ", myText</paragraph>
<paragraph id="par_idm1340957792" role="bascode" localize="false" xml-lang="en-US">End Sub</paragraph>
</bascode>

<paragraph id="hd_id3154368" role="heading" level="3" xml-lang="en-US">Saving Variable Content after Exiting a SUB or FUNCTION</paragraph>
<bascode>
<paragraph id="par_id3156288" role="bascode" xml-lang="en-US">Static VarName As TYPENAME</paragraph>
</bascode>
<paragraph id="par_id3154486" role="paragraph" xml-lang="en-US">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.</paragraph>

<paragraph id="hd_id3155809" role="heading" level="2" xml-lang="en-US">Specifying the Return Value Type of a FUNCTION</paragraph>
<paragraph id="par_id3149404" role="paragraph" xml-lang="en-US">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:</paragraph>
<bascode>
<paragraph id="par_idm1340946176" role="bascode" localize="false" xml-lang="en-US">Function WordCount(WordText As String) As Integer</paragraph>
</bascode>
</body>

</helpdocument>