Professional Documents
Culture Documents
Example:
load "stdlib.ring"
For in Loop
Syntax:
for identifier in List/String [step expression] {
Block of statements
}
Example:
load "stdlib.ring"
Example:
load "stdlib.ring"
17.3 Exceptions
17.3. Exceptions 80
Ring Documentation, Release 1.2
try {
Block of statements
catch
Block of statements
}
17.3. Exceptions 81
CHAPTER
EIGHTEEN
GETTING INPUT
Syntax:
Give VariableName
Example:
See "Enter the first number : " Give nNum1
See "Enter the second number : " Give nNum2
See "Sum : " + ( 0 + nNum1 + nNum2 )
Output:
Enter the first number : 3
Enter the second number : 4
Sum : 7
We can get one character from the standard input using the GetChar() function
Syntax:
GetChar() ---> Character
Example:
While True
See "
Main Menu
(1) Say Hello
(2) Exit
"
Option = GetChar()
82
Ring Documentation, Release 1.2
# the previous two lines can be replaced with the next line
# Give Option
if Option = 1
see "Enter your name : " give cName
see "Hello " + cName
else
bye
ok
End
We can get input from the keyboard using the Input() function
Syntax:
Input(nCount) ---> string
The function will wait until nCount characters (at least) are read
Example:
See "Enter message (30 characters) : " cMsg = input(30)
See "Message : " + cMsg
NINETEEN
FUNCTIONS
Example:
func hello
see "Hello from function" + nl
Tip: We can call the function before the function definition and the function code.
Example:
84
Ring Documentation, Release 1.2
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
19.8 Recursion
The Ring programming language support Recursion and the function can call itself using different parameters.
Example:
see fact(5) # output = 120
19.8. Recursion 87
CHAPTER
TWENTY
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 dont 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
88
CHAPTER
TWENTYONE
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)
89