2 single: Functions - Third Style; Introduction
4 ========================
5 Functions - Third Style
6 ========================
8 In this chapter we are going to learn about the next topics :-
27 pair: Functions - Third Style; Define Functions
33 To define new function
39 func <function_name> [parameters] ['{']
49 print("Hello from function \n")
54 pair: Functions - Third Style; Call Functions
59 To call function without parameters, we type the function name then ()
61 .. tip:: We can call the function before the function definition and the function code.
72 print("Hello from function \n")
84 func first { print("message from the first function \n") }
86 func second { print("message from the second function \n") }
89 pair: Functions - Third Style; Declare parameters
94 To declare the function parameters, after the function name type the list of parameters as a group
95 of identifiers separated by comma.
108 pair: Functions - Third Style; Send Parameters
113 To send parameters to function, type the parameters inside () after the function name
132 sum(3,5) sum(1000,2000)
134 func sum(x,y) { print(x+y) }
137 pair: Functions - Third Style; Main Function
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.
145 if no other statements comes alone, the main function will be the first `entry point <http://en.wikipedia.org/wiki/Entry_point>`_
151 # this program will print the hello world message first then execute the main function
155 print("Hello, World! \n")
158 print("Message from the main function \n")
162 pair: Functions - Third Style; Variables Scope
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.
170 Variables defined inside functions (including function parameters) are local variables.
171 Variables defined outside functions (before any function) are global variables.
173 Inside any function we can access the variables defined inside this function beside the global variables.
179 # the program will print numbers from 10 to 1
183 x = 10 # x is a global variable.
186 for t = 1 to 10 { # t is a local variable
187 mycounter() # call function
192 print("#{x}\n") # print the global variable value
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.
201 pair: Functions - Third Style; Return Value
206 The function can return a value using the Return command.
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.
217 .. note:: if the function doesn't return explicit value, it will return NULL (empty string = "" ).
225 if novalue() = NULL {
226 print("the function doesn't return a value\n")
232 pair: Functions - Third Style; Recursion
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.
246 print( fact(5) ) # output = 120
248 func fact(x) { if x = 0 { return 1 else return x * fact(x-1) } }