1<?xml version="1.0" encoding="UTF-8"?>
2
3<!--***********************************************************
4 *
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements.  See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership.  The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License.  You may obtain a copy of the License at
12 *
13 *   http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied.  See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 *
22 ***********************************************************-->
23
24<helpdocument version="1.0">
25<meta>
26<topic id="textsbasicshared01010210xml" indexer="include" status="PUBLISH">
27<title id="tit" xml-lang="en-US">Basics</title>
28<filename>/text/sbasic/shared/01010210.xhp</filename>
29</topic>
30</meta>
31<body>
32<section id="modular">
33<bookmark xml-lang="en-US" branch="index" id="bm_id4488967">
34<bookmark_value>fundamentals</bookmark_value>
35<bookmark_value>subroutines</bookmark_value>
36<bookmark_value>variables;global and local</bookmark_value>
37<bookmark_value>modules;subroutines and functions</bookmark_value>
38</bookmark><comment>mw changed "Basic;fundamentals"</comment>
39<paragraph role="heading" id="hd_id3154927" xml-lang="en-US" level="1" l10n="U" oldref="1"><link href="text/sbasic/shared/01010210.xhp" name="Basics">Basics</link></paragraph>
40<paragraph role="paragraph" id="par_id3156023" xml-lang="en-US" l10n="U" oldref="14">This section provides the fundamentals for working with $[officename] Basic.</paragraph>
41</section>
42<paragraph role="paragraph" id="par_id3147560" xml-lang="en-US" l10n="U" oldref="2">$[officename] Basic code is based on subroutines and functions that are specified between <emph>sub...end sub</emph> and <emph>function...end function</emph> 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 <link href="text/sbasic/shared/01020300.xhp" name="Procedures and Functions">Procedures and Functions</link>.</paragraph>
43<paragraph role="note" id="par_id314756320" xml-lang="en-US" l10n="NEW">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>
44<paragraph role="heading" id="hd_id3150398" xml-lang="en-US" level="2" l10n="U" oldref="3">What is a Sub?</paragraph>
45<paragraph role="paragraph" id="par_id3148797" xml-lang="en-US" l10n="U" oldref="4">
46<emph>Sub</emph> is the short form of <emph>subroutine</emph>, 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:</paragraph>
47<paragraph role="code" id="par_id3150868" xml-lang="en-US" l10n="U" oldref="15">DoSomethingWithTheValues(MyFirstValue,MySecondValue)</paragraph>
48<paragraph role="heading" id="hd_id3156282" xml-lang="en-US" level="2" l10n="U" oldref="5">What is a Function?</paragraph>
49<paragraph role="paragraph" id="par_id3156424" xml-lang="en-US" l10n="U" oldref="6">A <emph>function</emph> 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:</paragraph>
50<paragraph role="code" id="par_id3146985" xml-lang="en-US" l10n="U" oldref="7">MySecondValue = myFunction(MyFirstValue)</paragraph>
51<paragraph role="heading" id="hd_id3153364" xml-lang="en-US" level="2" l10n="U" oldref="8">Global and local variables</paragraph>
52<paragraph role="paragraph" id="par_id3151112" xml-lang="en-US" l10n="U" oldref="9">Global variables are valid for all subs and functions inside a module. They are declared at the beginning of a module before the first sub or function starts.</paragraph>
53<paragraph role="paragraph" id="par_id3154012" xml-lang="en-US" l10n="U" oldref="10">Variables that you declare within a sub or function are valid only inside this sub or function. These variables override global variables with the same name and local variables with the same name coming from superordinate subs or functions.</paragraph>
54<paragraph role="heading" id="hd_id3150010" xml-lang="en-US" level="2" l10n="U" oldref="11">Structuring</paragraph>
55<paragraph role="paragraph" id="par_id3153727" xml-lang="en-US" l10n="U" oldref="12">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. $[officename] Basic supports <link href="text/sbasic/shared/01020500.xhp" name="Modules and Libraries">Modules and Libraries</link>. 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.</paragraph>
56<paragraph role="paragraph" id="par_id3152578" xml-lang="en-US" l10n="U" oldref="13">You can copy or move subs, functions, modules and libraries from one file to another by using the <link href="text/sbasic/shared/01/06130000.xhp" name="Macro">Macro</link> dialog.</paragraph>
57</body>
58</helpdocument>
59