OSDN Git Service

【更新内容】
[ring-lang-081/ring.git] / docs / en / target / functions3.txt
1 .. index:: 
2         single: Functions - Third Style; Introduction
3
4 ========================
5 Functions  - Third Style
6 ========================
7
8 In this chapter we are going to learn about the next topics :-
9
10 * Define functions
11
12 * Call functions
13
14 * Declare parameters
15
16 * Send parameters
17
18 * Main Function
19
20 * Variables Scope
21
22 * Return Value
23
24 * Recursion
25
26 .. index:: 
27         pair: Functions - Third Style; Define Functions
28
29
30 Define Functions
31 ================
32
33 To define new function 
34
35 Syntax:
36
37 .. code-block:: ring
38
39         func <function_name> [parameters] ['{']
40                 Block of statements
41         ['}']
42
43 Example:
44
45 .. code-block:: ring
46
47         load "stdlib.ring"
48         func hello {
49                 print("Hello from function \n")
50         }
51
52
53 .. index:: 
54         pair: Functions - Third Style; Call Functions
55
56 Call Functions
57 ==============
58
59 To call function without parameters, we type the function name then ()
60
61 .. tip:: We can call the function before the function definition and the function code.
62
63 Example:
64
65 .. code-block:: ring
66
67         load "stdlib.ring"
68
69         hello()
70
71         func hello {
72                 print("Hello from function \n")
73         }
74
75
76 Example:
77
78 .. code-block:: ring
79
80         load "stdlib.ring"
81
82         first()  second()
83
84         func first {  print("message from the first function \n")  }
85
86         func second { print("message from the second function \n") }
87
88 .. index:: 
89         pair: Functions - Third Style; Declare parameters
90
91 Declare parameters
92 ==================
93
94 To declare the function parameters, after the function name type the list of parameters as a group
95 of identifiers separated by comma.
96
97 Example:
98
99 .. code-block:: ring
100
101         load "stdlib.ring"
102
103         func sum(x,y) {
104                 print(x+y)
105         }
106
107 .. index:: 
108         pair: Functions - Third Style; Send Parameters
109
110 Send Parameters
111 ===============
112
113 To send parameters to function, type the parameters inside () after the function name
114
115 Syntax:
116
117 .. code-block:: ring
118
119         funcname(parameters)
120
121 Example:
122
123 .. code-block:: ring
124
125         /* output
126         ** 8
127         ** 3000
128         */
129
130         load "stdlib.ring"
131
132         sum(3,5) sum(1000,2000)
133
134         func sum(x,y) { print(x+y) } 
135
136 .. index:: 
137         pair: Functions - Third Style; Main Function
138
139 Main Function
140 =============
141
142 Using the Ring programming language, the Main Function is optional, 
143 when it's defined, it will be executed after the end of other statements.
144
145 if no other statements comes alone, the main function will be the first `entry point <http://en.wikipedia.org/wiki/Entry_point>`_ 
146
147 Example:
148
149 .. code-block:: ring
150
151         # this program will print the hello world message first then execute the main function
152
153         load "stdlib.ring"
154
155         print("Hello, World! \n")
156
157         func main {
158                 print("Message from the main function \n")
159         }
160
161 .. index:: 
162         pair: Functions - Third Style; Variables Scope
163
164 Variables Scope
165 ===============
166
167 The Ring programming language uses `lexical scoping <http://en.wikipedia.org/wiki/Scope_%28computer_science%29#Lexical_scope_vs._dynamic_scope>`_ to
168 determine the scope of a variable.
169  
170 Variables defined inside functions (including function parameters) are local variables.
171 Variables defined outside functions (before any function) are global variables.
172
173 Inside any function we can access the variables defined inside this function beside the global variables.
174
175 Example:
176
177 .. code-block:: ring
178
179         # the program will print numbers from 10 to 1
180         
181         load "stdlib.ring"
182
183         x = 10                          # x is a global variable.
184
185         func main {
186                 for t = 1 to 10 {       # t is a local variable
187                         mycounter()     # call function
188                 }
189         }
190
191         func mycounter {
192                 print("#{x}\n")         # print the global variable value
193                 x--                     # decrement
194         }
195
196 .. note:: Using the main function before the for loop declare the t variable as a local variable,
197           It's recommended to use the main functions instead of typing the instructions directly to set the scope
198           of the new variables to local.
199
200 .. index:: 
201         pair: Functions - Third Style; Return Value
202
203 Return Value
204 ============
205
206 The function can return a value using the Return command.
207
208 Syntax:
209
210 .. code-block:: ring
211
212         Return [Expression]
213
214 .. tip:: the Expression after the return command is optional and we can use the return command
215          to end the function execution without returning any value.
216          
217 .. note:: if the function doesn't return explicit value, it will return NULL (empty string = "" ).
218
219 Example:
220
221 .. code-block:: ring
222
223         load "stdlib.ring"
224
225         if novalue() = NULL {
226                 print("the function doesn't return a value\n")
227         }
228
229         func novalue { }
230
231 .. index:: 
232         pair: Functions - Third Style; Recursion
233
234 Recursion
235 =========
236
237 The Ring programming language support `Recursion <http://en.wikipedia.org/wiki/Recursion_%28computer_science%29>`_
238 and the function can call itself using different parameters.
239
240 Example:
241
242 .. code-block:: ring
243
244         load "stdlib.ring"
245
246         print( fact(5) )        # output = 120
247
248         func fact(x) { if x = 0 { return 1 else return x * fact(x-1) } }