summaryrefslogtreecommitdiffstats
path: root/helpcontent2/source/text/sbasic/shared/03090406.xhp
blob: 7ecfbe73c1ebaa493177154147c2e67a9de959df (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
<?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="textsbasicshared03090406xml" indexer="include" status="PUBLISH">
    <title id="tit" xml-lang="en-US">Function Statement</title>
    <filename>/text/sbasic/shared/03090406.xhp</filename>
  </topic>
</meta>
<body>
<section id="function">
<bookmark xml-lang="en-US" branch="index" id="bm_id3153346">
  <bookmark_value>Function statement</bookmark_value>
</bookmark>
<h1 id="hd_id3153346" xml-lang="en-US"><variable id="Function_h1"><link href="text/sbasic/shared/03090406.xhp" name="Function Statement">Function Statement</link></variable></h1>
<paragraph id="par_id3159158" role="paragraph" xml-lang="en-US">A function is a block of code which runs when it is called. A function is usually called in an expression.</paragraph>
<paragraph role="paragraph" id="par_id451647246564161" xml-lang="en-US">You can pass data, known as parameters or arguments, into a function. You may pass a parameter by value or by reference. When by reference, modifications applied to the parameter in the function will be sent back to the calling code.</paragraph>
<paragraph role="paragraph" id="par_id101647247154065" xml-lang="en-US">A function usually returns data as a result.</paragraph>
</section>

<embed href="text/sbasic/shared/00000003.xhp#functsyntax"/>
<paragraph role="paragraph" id="par_id971588473588701">
  <image src="media/helpimg/sbasic/Function_statement.svg" id="img_id4156296484514"><alt xml-lang="en-US" id="alt_id15152796484514">Function Statement diagram</alt></image></paragraph>
<bascode>
  <paragraph role="bascode" id="bas_id151588427099190" localize="false">[Private | Public] Function Name[char] (argument1 [As Type][, argument2[char][,...]]) [As typename]</paragraph>
  <paragraph role="bascode" id="bas_id541588427401158" xml-lang="en-US">        statements</paragraph>
  <paragraph role="bascode" id="bas_id981588427406211" localize="false">    [Exit Function]</paragraph>
  <paragraph role="bascode" id="bas_id631588427410583" xml-lang="en-US">        statements</paragraph>
  <paragraph role="bascode" id="bas_id581588427417085" localize="false">End Function</paragraph>
</bascode>
<embed href="text/sbasic/shared/00000003.xhp#functparameters"/>
<section id="RoutineScope">
   <paragraph role="paragraph" id="par_id81588429476557"> <emph>scope:</emph> Function default scope is <literal>Public</literal>. A <literal>Private</literal> scope denotes a module internal routine, not intended to be used from other modules.</paragraph>
</section>
<paragraph id="par_id3153193" role="paragraph" xml-lang="en-US"> <emph>name:</emph> Name of the subroutine to contain the value returned by the function.</paragraph>
<paragraph id="par_id3147229" role="paragraph" xml-lang="en-US"> <emph>arguments:</emph> Parameters to be passed to the subroutine.</paragraph>
<embed href="text/sbasic/shared/fragments.xhp#argument"/>
<embed href="text/sbasic/shared/fragments.xhp#typename"/>
<embed href="text/sbasic/shared/fragments.xhp#char"/>
<section id="BasicExamples">
<h2 id="hd_id3163710" xml-lang="en-US">Examples:</h2>
<bascode>
<paragraph id="par_idm1340981728" role="bascode" localize="false">Sub ExampleExit</paragraph>
<paragraph id="par_idm1340980496" role="bascode" localize="false">Dim sReturn As String</paragraph>
<paragraph id="par_idm1340979264" role="bascode" localize="false">Dim sListArray(10) As String</paragraph>
<paragraph id="par_idm1340978016" role="bascode" localize="false">Dim siStep As Single</paragraph>
<paragraph id="par_id3152939" role="bascode" xml-lang="en-US">    For siStep = 0 To 10 ' Fill array with test data</paragraph>
<paragraph id="par_idm1340975040" role="bascode" localize="false">        sListArray(siStep) = chr$(siStep + 65)</paragraph>
<paragraph id="par_idm1340973776" role="bascode" localize="false">        MsgBox sListArray(siStep)</paragraph>
<paragraph id="par_idm1340972528" role="bascode" localize="false">    Next siStep</paragraph>
<paragraph id="par_idm1340971296" role="bascode" localize="false">    sReturn = LinSearch(sListArray(), "B")</paragraph>
<paragraph id="par_idm1340970032" role="bascode" localize="false">    Print sReturn</paragraph>
<paragraph id="par_idm1340968800" role="bascode" localize="false">End Sub</paragraph>
<paragraph id="par_idm1340967568" role="bascode" localize="false"> </paragraph>
<paragraph id="par_idm1340966320" role="bascode" localize="false">Function LinSearch( sList(), sItem As String ) As Integer</paragraph>
<paragraph id="par_idm1340965040" role="bascode" localize="false" xml-lang="en-US">Dim iCount As Integer</paragraph>
<paragraph id="par_id3154943" role="bascode" xml-lang="en-US">' Linsearch searches a TextArray:sList() for a TextEntry:</paragraph>
<paragraph id="par_id3155601" role="bascode" xml-lang="en-US">' Return value Is the index of the entry Or 0 (Null)</paragraph>
<paragraph id="par_idm1340960304" role="bascode" localize="false">    For iCount=1 To Ubound( sList() )</paragraph>
<paragraph id="par_idm1340959056" role="bascode" localize="false">        If sList( iCount ) = sItem Then</paragraph>
<paragraph id="par_id3153707" role="bascode" xml-lang="en-US">            Exit For ' sItem found</paragraph>
<paragraph id="par_idm1340956080" role="bascode" localize="false">        End If</paragraph>
<paragraph id="par_idm1340954848" role="bascode" localize="false">    Next iCount</paragraph>
<paragraph id="par_idm1340953616" role="bascode" localize="false">    If iCount = Ubound( sList() ) Then iCount = 0</paragraph>
<paragraph id="par_idm1340952352" role="bascode" localize="false">    LinSearch = iCount</paragraph>
<paragraph id="par_idm1340951120" role="bascode" localize="false">End Function</paragraph>
</bascode>
</section>

<section id="relatedtopics">
  <paragraph role="paragraph" id="par_id181647247913872"><link href="text/sbasic/shared/01010210.xhp" name="Subroutine basics">Subroutines basics</link></paragraph>
  <embed href="text/sbasic/shared/01020300.xhp#UsingSubs_h1"/>
  <embed href="text/sbasic/shared/03090409.xhp#Sub_h1"/>
  <embed href="text/sbasic/shared/property.xhp#Property_h1"/>
</section>
</body>
</helpdocument>