File: Functions_%28subs%29.html

package info (click to toggle)
sludge 2.2.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,852 kB
  • sloc: cpp: 32,432; sh: 1,237; makefile: 634; xml: 284
file content (100 lines) | stat: -rwxr-xr-x 2,778 bytes parent folder | download | duplicates (6)
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<TITLE>Functions (subs)</TITLE>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<BODY>
<HR>
<div align="center"><img id="headerGraphic" src="images/sludge300.png" alt="SLUDGE"/></div>
<h2>Functions (subs)</h2>
<HR>


<P>
In SLUDGE, a function or &quot;sub&quot; is a group of instructions which make up a particular task. Each function has a name, a set of parameters (values which can be passed in) and a list of commands. A function is defined in the following way:
</P>

<P>
<pre>sub nameGoesHere (parameter1, parameter2, andSoOn) {
   # Some code
}</pre>
</P>

<P>
It is also possible to return values from a function. Functions can return any of the data types which can be stored in a <a href="Variables.html">variable</a> (you can therefore use a <a href="The_Multi-Purpose_Stack___Array___Queue_Type.html">stack</a> if you need to return more than one value from a function).
</P>

<P>
<pre>sub addTwoNumbers (a, b) {
   return a + b;
}

sub introducePeople (aBod, anotherBod) {

   # Let's return a stack containing &quot;A, I'd like you to meet B.&quot;
   # and &quot;B, this is A!&quot;.

   return newStack (aBod + &quot;, I'd like you to meet &quot; + anotherBod + &quot;.&quot;,
                    anotherBod + &quot;, this is &quot; + aBod + &quot;!&quot;);
}</pre>
</P>

<P>
When calling a function, simply type the name of the function followed by the parameters in brackets. For example:
</P>

<P>
<pre>sub sayNumber (number) {
   say (ego, &quot;The number is &quot; + number + &quot;.&quot;);
}

sub init () {

   # Let's call our addTwoNumbers function
   var total = addTwoNumbers (100, 451);

   # Now let's call our new sayNumber function
   sayNumber (total);
}</pre>
</P>

<P>
If you are calling a function which needs no parameters, make sure you still include an empty set of brackets, otherwise the function will not be called!
</P>

<P>
<pre>sub reallyImportantFunction () {
   say (ego, &quot;It's vital that I say this!&quot;);
} 

sub init () {

   # This won't call the reallyImportantFunction...
   reallyImportantFunction;

   # But this will...
   reallyImportantFunction ();
}</pre>
</P>

<H3>More on this subject:</H3>

<P>
<a href="Passing_Functions_as_Variables.html">Passing Functions as Variables</a>
</P>

<P>
<a href="Unfreezable_Functions.html">Unfreezable Functions</a>
</P>

<P>
<a href="spawnSub.html">spawnSub()</a>
</P>

<P class="copyright-notice">SLUDGE and this SLUDGE documentation are <A HREF="Copyright.html">copyright</A> Hungry Software and contributors 2000-2012
</P>

<HR>
</BODY>
</html>