Professional Documents
Culture Documents
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)
Using the Ring programming language, the Main Function is optional, when it’s 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
load "stdlib.ring"
func main {
print("Message from the main function \n")
}
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
load "stdlib.ring"
x = 10 # x is a global variable.
func main {
for t = 1 to 10 { # t is a local variable
mycounter() # call function
}
}
func mycounter {
print("#{x}\n") # print the global variable value
x-- # decrement
}
Note: Using the main function before the for loop declare the t variable as a local variable, It’s recommended to use
the main functions instead of typing the instructions directly to set the scope of the new variables to local.
Return [Expression]
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 doesn’t return explicit value, it will return NULL (empty string = “” ).
Example:
load "stdlib.ring"
if novalue() = NULL {
print("the function doesn't return a value\n")
}
func novalue { }
26.8 Recursion
The Ring programming language support Recursion and the function can call itself using different parameters.
Example:
load "stdlib.ring"
TWENTYSEVEN
PROGRAM STRUCTURE
In this chapter we will learn about using many source code files in the same project.
Each source code file may contains the next sections (in the same order).
Source Code File Sections
Load Files
Statements and Global Variables
Functions
Packages and Classes
The application maybe one or more of files.
To include another source file in the project, just use the load command.
Syntax:
Load "filename.ring"
Note: The Load command is executed directly by the compiler in the parsing stage
Tip: if you don’t know the file name until the runtime, or you need to use functions to get the file path, just use eval().
Example:
# File : Start.ring
Load "sub.ring"
sayhello("Mahmoud")
# File : sub.ring
215
Ring Documentation, Release 1.7
Using the ‘load’ command we can use many ring source files in the same project
But all of these files will share the same global scope
We have also the “Load Package” command
Using “Load Package” we can load a library (*.ring file) in new global scope
This is very useful to create libraries that avoid conflicts in global variables
Example:
File: loadpackage.ring
x = 100
? "Hello, World!"
load package "testloadpackage.ring"
? x
test()
File: testloadpackage.ring
? "Hello from testloadpackage.ring"
x = 1000
test()
func test
? x
Output:
Hello, World!
Hello from testloadpackage.ring
1000
100
1000
TWENTYEIGHT
LISTS
We can create new lists by defining the list items inside square bracts.
Example:
aList = [1,2,3,4,5]
Example:
aList = 5:1
aList2 = "z":"a"
Example
aList = list(10) # aList contains 10 items
To add new items to the list, we can use the Add() function.
Syntax:
Add(List,Item)
217
Ring Documentation, Release 1.7
Example:
aList = ["one","two"]
add(aList,"three")
see aList
Example:
aList = 1:10 # create list contains numbers from 1 to 10
aList + 11 # add number 11 to the list
see aList # print the list
Example:
aList = 1:20 see len(aList) # print 20
To delete an item from the list, we can use the del() function
Syntax:
del(list,index)
Example:
aList = ["one","two","other","three"]
Del(aList,3) # delete item number three
see aList # print one two three
Example:
aList = ["Cairo","Riyadh"]
see "Egypt : " + aList[1] + nl +
"KSA : " + aList[2] + nl
To set the value of an item inside the list, we can use the next syntax
List[Index] = Expression
Example:
aList = list(3) # create list contains three items
aList[1] = "one" aList[2] = "two" aList[3] = "three"
see aList
28.7 Search
To find an item inside the list we can use the find() function
Syntax:
Find(List,ItemValue) ---> Item Index
Find(List,ItemValue,nColumn) ---> Search in nColumn, returns the Item Index
Find(List,ItemValue,nColumn,cAttribute) ---> Item Index
Example:
aList = ["one","two","three","four","five"]
see find(aList,"three") # print 3
Example:
mylist = [["one",1],
["two",2],
["three",3]]
Example:
aList = ["one","two","three","four","five"]
aList = sort(aList)
see binarysearch(aList,"three")
Output:
five
four
one
three
two
4
28.8 Sort
Example:
aList = [10,12,3,5,31,15]
aList = sort(aList) see aList # print 3 5 10 12 15 31
aList2 = sort(aList,1)
see aList2
Output:
ahmed
14000
ibrahim
11000
mahmoud
15000
mohammed
12000
samir
16000
28.9 Reverse
Example:
aList = [10,20,30,40,50]
aList = reverse(aList)
see aList # print 50 40 30 20 10
We can copy lists (including nested lists) using the Assignment operator.
Example: