summaryrefslogtreecommitdiff
path: root/helpcontent2/source/text/sbasic/shared/01020300.xhp
blob: 45de1b65375c142242c7ee8db8a9b98b853ad3b2 (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
<?xml version="1.0" encoding="UTF-8"?>
<!--***********************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: 01020300.xhp,v $
 *
 *  $Revision: 1.3 $
 *
 *  last change: $Author: rt $ $Date: 2005-09-07 15:32:04 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************-->
	
<helpdocument version="1.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>
<history>
<created date="2003-10-31T00:00:00">Sun Microsystems, Inc.</created>
<lastedited date="2004-09-01T14:50:17">UFI: fixes #i24866#
YJ: checked
dedr: fixed #i30798#</lastedited>
</history>
</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 role="heading" id="hd_id3149456" xml-lang="en-US" level="1" l10n="U" oldref="1"><link href="text/sbasic/shared/01020300.xhp">Using Procedures and Functions</link></paragraph>
<paragraph role="paragraph" id="par_id3150767" xml-lang="en-US" l10n="U" oldref="2">The following describes the basic use of procedures and functions in $[officename] Basic.</paragraph>
</section>
<paragraph role="paragraph" id="par_id3154124" xml-lang="en-US" l10n="U" oldref="3">Procedures (SUBS) and functions (FUNCTIONS) help you maintaining a structured overview by separating a program into logical pieces.</paragraph>
<paragraph role="paragraph" id="par_id3153193" xml-lang="en-US" l10n="CHG" oldref="4">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 role="heading" id="hd_id3153770" xml-lang="en-US" level="2" l10n="U" oldref="26">Passing Variables to Procedures (SUB) and Functions (FUNCTION)</paragraph>
<paragraph role="paragraph" id="par_id3155414" xml-lang="en-US" l10n="U" oldref="27">Variables can be passed to both procedures and functions. The SUB or FUNCTION must be declared to expect parameters:</paragraph>
<paragraph role="code" id="par_id3163710" xml-lang="en-US" l10n="U" oldref="28">SUB SubName(<emph>Parameter1 As Type, Parameter2 As Type,...</emph>)</paragraph>
<paragraph role="code" id="par_id3151114" xml-lang="en-US" l10n="U" oldref="29">Program code</paragraph>
<paragraph role="code" id="par_id3146975" xml-lang="en-US" l10n="U" oldref="30">END SUB</paragraph>
<paragraph role="paragraph" id="par_id3152577" xml-lang="en-US" l10n="U" oldref="31">The SUB is called using the following syntax:</paragraph>
<paragraph role="code" id="par_id3159154" xml-lang="en-US" l10n="U" oldref="32">SubName(Value1, Value2,...)</paragraph>
<paragraph role="paragraph" id="par_id3147124" xml-lang="en-US" l10n="U" oldref="33">The parameters passed to a SUB must fit to those specified in the SUB declaration.</paragraph>
<paragraph role="paragraph" id="par_id3147397" xml-lang="en-US" l10n="U" oldref="34">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>
<paragraph role="code" id="par_id3149412" xml-lang="en-US" l10n="U" oldref="35">FUNCTION FunctionName(Parameter1 As Type, Parameter2 As Type,...) As Type</paragraph>
<paragraph role="code" id="par_id3156284" xml-lang="en-US" l10n="U" oldref="36">Program code</paragraph>
<paragraph role="code" id="par_id3145799" xml-lang="en-US" l10n="U" oldref="37">
<emph>FunctionName=Result</emph>
</paragraph>
<paragraph role="code" id="par_id3150716" xml-lang="en-US" l10n="U" oldref="38">End Function</paragraph>
<paragraph role="paragraph" id="par_id3153839" xml-lang="en-US" l10n="U" oldref="39">The FUNCTION is called using the following syntax:</paragraph>
<paragraph role="code" id="par_id3146914" xml-lang="en-US" l10n="U" oldref="40">Variable=FunctionName(Parameter1, Parameter2,...)</paragraph>
<paragraph role="tip" id="par_idN107B3" 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 role="heading" id="hd_id3156276" xml-lang="en-US" level="2" l10n="U" oldref="45">Passing Variables by Value or Reference</paragraph>
<paragraph role="paragraph" id="par_id3155765" xml-lang="en-US" l10n="U" oldref="47">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 role="paragraph" id="par_id3145640" xml-lang="en-US" l10n="U" oldref="53">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>
<paragraph role="code" id="par_id3150042" xml-lang="en-US" l10n="U" oldref="54">Result = Function(<emph>ByVal</emph> Parameter)</paragraph>
<paragraph role="paragraph" id="par_id3149258" xml-lang="en-US" l10n="U" oldref="55">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 role="note" id="par_id3151215" xml-lang="en-US" l10n="U" oldref="56">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 role="heading" id="hd_id3150982" xml-lang="en-US" level="2" l10n="U" oldref="57">Scope of Variables</paragraph>
<paragraph role="paragraph" id="par_id3149814" xml-lang="en-US" l10n="U" oldref="58">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. This property is controlled in a variable declaration through key words in the <emph>Dim</emph> statement.</paragraph>
<paragraph role="heading" id="hd_id3154186" xml-lang="en-US" level="3" l10n="U" oldref="59">Declaring Variables Outside a SUB or FUNCTION</paragraph>
<paragraph role="code" id="par_id3150208" xml-lang="en-US" l10n="U" oldref="111">DIM GLOBAL VarName As TYPENAME</paragraph>
<paragraph role="paragraph" id="par_id3145258" xml-lang="en-US" l10n="U" oldref="112">The variable is valid as long as the $[officename] session lasts.</paragraph>
<paragraph role="code" id="par_id3153198" xml-lang="en-US" l10n="U" oldref="60">DIM PUBLIC VarName As TYPENAME</paragraph>
<paragraph role="paragraph" id="par_id3150088" xml-lang="en-US" l10n="U" oldref="61">The variable is valid in all modules.</paragraph>
<paragraph role="code" id="par_id3158212" xml-lang="en-US" l10n="U" oldref="62">DIM PRIVATE VarName As TYPENAME</paragraph>
<paragraph role="paragraph" id="par_id3152994" xml-lang="en-US" l10n="U" oldref="63">The variable is only valid in this module.</paragraph>
<paragraph role="code" id="par_id3150886" xml-lang="en-US" l10n="U" oldref="64">DIM VarName As TYPENAME</paragraph>
<paragraph role="paragraph" id="par_id3150368" xml-lang="en-US" l10n="U" oldref="65">The variable is only valid in this module.</paragraph>
<paragraph role="heading" id="hd_id3154368" xml-lang="en-US" level="3" l10n="U" oldref="66">Saving Variable Content after Exiting a SUB or FUNCTION</paragraph>
<paragraph role="code" id="par_id3156288" xml-lang="en-US" l10n="U" oldref="67">DIM STATIC VarName As TYPENAME</paragraph>
<paragraph role="paragraph" id="par_id3154486" xml-lang="en-US" l10n="U" oldref="68">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 role="heading" id="hd_id3155809" xml-lang="en-US" level="2" l10n="U" oldref="41">Specifying the Return Value Type of a FUNCTION</paragraph>
<paragraph role="paragraph" id="par_id3149404" xml-lang="en-US" l10n="U" oldref="42">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>
<paragraph role="code" id="par_id3152899" xml-lang="en-US" l10n="U" oldref="43">Function WordCount(WordText as String) <emph>as Integer</emph>
</paragraph>
</body>
</helpdocument>