summaryrefslogtreecommitdiff
path: root/source/text/sbasic/shared/03090406.xhp
blob: 859d4e34b4a642592aa6d3b9d4a289de959d1e3b (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
<?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>

<paragraph id="hd_id3153346" role="heading" level="1" xml-lang="en-US"><link href="text/sbasic/shared/03090406.xhp" name="Function Statement">Function Statement</link></paragraph>
<paragraph id="par_id3159158" role="paragraph" xml-lang="en-US">Defines a subroutine that can be used as an expression to determine a return type.</paragraph>
</section>

<section id="TwinMethods">
<note id="par_id661588586825434"><literal>Sub</literal>, <literal>Function</literal> or <literal>Property</literal> statements are similar methods, without distinction. They receive parameters by reference allowing them to be modified in return. %PRODUCTNAME  Basic compiler accepts their respective argument syntax to be used interchangeably.</note>
</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" xml-lang="en-US">Sub ExampleExit</paragraph>
<paragraph id="par_idm1340980496" role="bascode" localize="false" xml-lang="en-US">Dim sReturn As String</paragraph>
<paragraph id="par_idm1340979264" role="bascode" localize="false" xml-lang="en-US">Dim sListArray(10) As String</paragraph>
<paragraph id="par_idm1340978016" role="bascode" localize="false" xml-lang="en-US">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" xml-lang="en-US">        sListArray(siStep) = chr$(siStep + 65)</paragraph>
<paragraph id="par_idm1340973776" role="bascode" localize="false" xml-lang="en-US">        MsgBox sListArray(siStep)</paragraph>
<paragraph id="par_idm1340972528" role="bascode" localize="false" xml-lang="en-US">    Next siStep</paragraph>
<paragraph id="par_idm1340971296" role="bascode" localize="false" xml-lang="en-US">    sReturn = LinSearch(sListArray(), "B")</paragraph>
<paragraph id="par_idm1340970032" role="bascode" localize="false" xml-lang="en-US">    Print sReturn</paragraph>
<paragraph id="par_idm1340968800" role="bascode" localize="false" xml-lang="en-US">End Sub</paragraph>
<paragraph id="par_idm1340967568" role="bascode" localize="false" xml-lang="en-US"> </paragraph>
<paragraph id="par_idm1340966320" role="bascode" localize="false" xml-lang="en-US">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" xml-lang="en-US">    For iCount=1 To Ubound( sList() )</paragraph>
<paragraph id="par_idm1340959056" role="bascode" localize="false" xml-lang="en-US">        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" xml-lang="en-US">        End If</paragraph>
<paragraph id="par_idm1340954848" role="bascode" localize="false" xml-lang="en-US">    Next iCount</paragraph>
<paragraph id="par_idm1340953616" role="bascode" localize="false" xml-lang="en-US">    If iCount = Ubound( sList() ) Then iCount = 0</paragraph>
<paragraph id="par_idm1340952352" role="bascode" localize="false" xml-lang="en-US">    LinSearch = iCount</paragraph>
<paragraph id="par_idm1340951120" role="bascode" localize="false" xml-lang="en-US">End Function</paragraph>
</bascode>
</section>

</body>

</helpdocument>