Professional Documents
Culture Documents
TWENTYTHREE
Example:
func hello
see "Hello from function" + nl
Tip: We can call the function before the function definition and the function code.
Example:
155
Ring Documentation, Release 1.5
hello()
func hello
see "Hello from function" + nl
Example:
first() second()
To declare the function parameters, after the function name type the list of parameters as a group of identifiers separated
by comma.
Example:
func sum x,y
see x+y+nl
To send parameters to function, type the parameters inside () after the function name
Syntax:
funcname(parameters)
Example:
/* output
** 8
** 3000
*/
sum(3,5) sum(1000,2000)
Using the Ring programming language, the Main Function is optional, when its defined, it will be executed after the
end of other statements.
if no other statements comes alone, the main function will be the first entry point
Example:
# this program will print the hello world message first then execute the main function
func main
see "Message from the main function" + nl
The Ring programming language uses lexical scoping to determine the scope of a variable.
Variables defined inside functions (including function parameters) are local variables. Variables defined outside func-
tions (before any function) are global variables.
Inside any function we can access the variables defined inside this function beside the global variables.
Example:
# the program will print numbers from 10 to 1
x = 10 # x is a global variable.
func main
func mycounter
Note: Using the main function before the for loop declare the t variable as a local variable, Its recommended to use
the main functions instead of typing the instructions directly to set the scope of the new variables to local.
Tip: the Expression after the return command is optional and we can use the return command to end the function
execution without returning any value.
Note: if the function doesnt return explicit value, it will return NULL (empty string = ).
Example:
if novalue() = NULL
See "the function doesn't return a value" + nl
ok
func novalue
23.8 Recursion
The Ring programming language support Recursion and the function can call itself using different parameters.
Example:
see fact(5) # output = 120
TWENTYFOUR
Example:
def hello
put "Hello from function" + nl
end
Tip: We can call the function before the function definition and the function code.
Example:
159
Ring Documentation, Release 1.5
hello()
def hello
put "Hello from function" + nl
end
Example:
first() second()
To declare the function parameters, after the function name type the list of parameters as a group of identifiers separated
by comma.
Example:
def sum x,y
put x+y+nl
end
To send parameters to function, type the parameters inside () after the function name
Syntax:
funcname(parameters)
Example:
/* output
** 8
** 3000
*/
sum(3,5) sum(1000,2000)
Using the Ring programming language, the Main Function is optional, when its defined, it will be executed after the
end of other statements.
if no other statements comes alone, the main function will be the first entry point
Example:
# this program will print the hello world message first then execute the main function
def main
put "Message from the main function" + nl
end
The Ring programming language uses lexical scoping to determine the scope of a variable.
Variables defined inside functions (including function parameters) are local variables. Variables defined outside func-
tions (before any function) are global variables.
Inside any function we can access the variables defined inside this function beside the global variables.
Example:
# the program will print numbers from 10 to 1
x = 10 # x is a global variable.
def main
for t = 1 to 10 # t is a local variable
mycounter() # call function
end
end
def mycounter
put x + nl # print the global variable value
x-- # decrement
end
Note: Using the main function before the for loop declare the t variable as a local variable, Its recommended to use
the main functions instead of typing the instructions directly to set the scope of the new variables to local.
Tip: the Expression after the return command is optional and we can use the return command to end the function
execution without returning any value.
Note: if the function doesnt return explicit value, it will return NULL (empty string = ).
Example:
if novalue() = NULL
put "the function doesn't return a value" + nl
end
def novalue
24.8 Recursion
The Ring programming language support Recursion and the function can call itself using different parameters.
Example:
put fact(5) # output = 120
TWENTYFIVE
Example:
load "stdlib.ring"
func hello {
print("Hello from function \n")
}
Tip: We can call the function before the function definition and the function code.
Example:
163
Ring Documentation, Release 1.5
load "stdlib.ring"
hello()
func hello {
print("Hello from function \n")
}
Example:
load "stdlib.ring"
first() second()
To declare the function parameters, after the function name type the list of parameters as a group of identifiers separated
by comma.
Example:
load "stdlib.ring"
func sum(x,y) {
print(x+y)
}
To send parameters to function, type the parameters inside () after the function name
Syntax:
funcname(parameters)
Example:
/* output
** 8
** 3000
*/
load "stdlib.ring"
sum(3,5) sum(1000,2000)