SlideShare a Scribd company logo
1 of 10
Download to read offline
Ring Documentation, Release 1.5.2
try {
Block of statements
catch
Block of statements
}
20.3. Exceptions 165
CHAPTER
TWENTYONE
GETTING INPUT
We can get input from the keyboard using
• The Give Command
• The GetChar() Function
• The Input() Function
21.1 Give Command
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
21.2 GetChar() Function
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()
166
Ring Documentation, Release 1.5.2
GetChar() GetChar() # End of line
# 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
21.3 Input() Function
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
21.3. Input() Function 167
CHAPTER
TWENTYTWO
FUNCTIONS - FIRST STYLE
In this chapter we are going to learn about the next topics :-
• Define functions
• Call functions
• Declare parameters
• Send parameters
• Main Function
• Variables Scope
• Return Value
• Recursion
22.1 Define Functions
To define new function
Syntax:
func <function_name> [parameters]
Block of statements
Note: No keyword is required to end the function definition.
Example:
func hello
see "Hello from function" + nl
22.2 Call Functions
To call function without parameters, we type the function name then ()
Tip: We can call the function before the function definition and the function code.
Example:
168
Ring Documentation, Release 1.5.2
hello()
func hello
see "Hello from function" + nl
Example:
first() second()
func first see "message from the first function" + nl
func second see "message from the second function" + nl
22.3 Declare parameters
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
22.4 Send Parameters
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)
func sum x,y see x+y+nl
22.5 Main Function
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:
22.3. Declare parameters 169
Ring Documentation, Release 1.5.2
# this program will print the hello world message first then execute the main function
See "Hello World!" + nl
func main
see "Message from the main function" + nl
22.6 Variables Scope
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
for t = 1 to 10 # t is a local variable
mycounter() # call function
next
func mycounter
see x + nl # 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.
22.7 Return Value
The function can return a value using the Return command.
Syntax:
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:
22.6. Variables Scope 170
Ring Documentation, Release 1.5.2
if novalue() = NULL
See "the function doesn't return a value" + nl
ok
func novalue
22.8 Recursion
The Ring programming language support Recursion and the function can call itself using different parameters.
Example:
see fact(5) # output = 120
func fact x if x = 0 return 1 else return x * fact(x-1) ok
22.8. Recursion 171
CHAPTER
TWENTYTHREE
FUNCTIONS - SECOND STYLE
In this chapter we are going to learn about the next topics :-
• Define functions
• Call functions
• Declare parameters
• Send parameters
• Main Function
• Variables Scope
• Return Value
• Recursion
23.1 Define Functions
To define new function
Syntax:
def <function_name> [parameters]
Block of statements
[end]
Note: the keyword ‘end’ is optional.
Example:
def hello
put "Hello from function" + nl
end
23.2 Call Functions
To call function without parameters, we type the function name then ()
Tip: We can call the function before the function definition and the function code.
Example:
172
Ring Documentation, Release 1.5.2
hello()
def hello
put "Hello from function" + nl
end
Example:
first() second()
def first put "message from the first function" + nl
def second put "message from the second function" + nl
23.3 Declare parameters
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
23.4 Send Parameters
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)
def sum x,y put x+y+nl
23.5 Main Function
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:
23.3. Declare parameters 173
Ring Documentation, Release 1.5.2
# this program will print the hello world message first then execute the main function
put "Hello World!" + nl
def main
put "Message from the main function" + nl
end
23.6 Variables Scope
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, It’s recommended to use
the main functions instead of typing the instructions directly to set the scope of the new variables to local.
23.7 Return Value
The function can return a value using the Return command.
Syntax:
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:
23.6. Variables Scope 174

More Related Content

What's hot

Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
TAlha MAlik
 
Call by value
Call by valueCall by value
Call by value
Dharani G
 
C++ Function
C++ FunctionC++ Function
C++ Function
Hajar
 

What's hot (20)

The Ring programming language version 1.9 book - Part 27 of 210
The Ring programming language version 1.9 book - Part 27 of 210The Ring programming language version 1.9 book - Part 27 of 210
The Ring programming language version 1.9 book - Part 27 of 210
 
functions of C++
functions of C++functions of C++
functions of C++
 
The Ring programming language version 1.10 book - Part 29 of 212
The Ring programming language version 1.10 book - Part 29 of 212The Ring programming language version 1.10 book - Part 29 of 212
The Ring programming language version 1.10 book - Part 29 of 212
 
The Ring programming language version 1.5.1 book - Part 20 of 180
The Ring programming language version 1.5.1 book - Part 20 of 180The Ring programming language version 1.5.1 book - Part 20 of 180
The Ring programming language version 1.5.1 book - Part 20 of 180
 
The Ring programming language version 1.5.2 book - Part 31 of 181
The Ring programming language version 1.5.2 book - Part 31 of 181The Ring programming language version 1.5.2 book - Part 31 of 181
The Ring programming language version 1.5.2 book - Part 31 of 181
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
The Ring programming language version 1.3 book - Part 23 of 88
The Ring programming language version 1.3 book - Part 23 of 88The Ring programming language version 1.3 book - Part 23 of 88
The Ring programming language version 1.3 book - Part 23 of 88
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
 
Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++
 
Call by value or call by reference in C++
Call by value or call by reference in C++Call by value or call by reference in C++
Call by value or call by reference in C++
 
Call by value
Call by valueCall by value
Call by value
 
03 function overloading
03 function overloading03 function overloading
03 function overloading
 
Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++
 
Pre defined Functions in C
Pre defined Functions in CPre defined Functions in C
Pre defined Functions in C
 
The Ring programming language version 1.5.4 book - Part 32 of 185
The Ring programming language version 1.5.4 book - Part 32 of 185The Ring programming language version 1.5.4 book - Part 32 of 185
The Ring programming language version 1.5.4 book - Part 32 of 185
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 

Similar to The Ring programming language version 1.5.2 book - Part 20 of 181

Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
TeshaleSiyum
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
TeshaleSiyum
 
functions notes.pdf python functions and opp
functions notes.pdf python functions and oppfunctions notes.pdf python functions and opp
functions notes.pdf python functions and opp
KirtiGarg71
 

Similar to The Ring programming language version 1.5.2 book - Part 20 of 181 (20)

The Ring programming language version 1.8 book - Part 25 of 202
The Ring programming language version 1.8 book - Part 25 of 202The Ring programming language version 1.8 book - Part 25 of 202
The Ring programming language version 1.8 book - Part 25 of 202
 
The Ring programming language version 1.2 book - Part 11 of 84
The Ring programming language version 1.2 book - Part 11 of 84The Ring programming language version 1.2 book - Part 11 of 84
The Ring programming language version 1.2 book - Part 11 of 84
 
The Ring programming language version 1.3 book - Part 13 of 88
The Ring programming language version 1.3 book - Part 13 of 88The Ring programming language version 1.3 book - Part 13 of 88
The Ring programming language version 1.3 book - Part 13 of 88
 
python_function.pdf
python_function.pdfpython_function.pdf
python_function.pdf
 
Pro
ProPro
Pro
 
functioninpython-1.pptx
functioninpython-1.pptxfunctioninpython-1.pptx
functioninpython-1.pptx
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Python_Functions_Unit1.pptx
Python_Functions_Unit1.pptxPython_Functions_Unit1.pptx
Python_Functions_Unit1.pptx
 
Function
FunctionFunction
Function
 
3. functions modules_programs (1)
3. functions modules_programs (1)3. functions modules_programs (1)
3. functions modules_programs (1)
 
Functions
FunctionsFunctions
Functions
 
functions modules and exceptions handlings.ppt
functions modules and exceptions handlings.pptfunctions modules and exceptions handlings.ppt
functions modules and exceptions handlings.ppt
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
 
Cpp functions
Cpp functionsCpp functions
Cpp functions
 
The Ring programming language version 1.5.2 book - Part 21 of 181
The Ring programming language version 1.5.2 book - Part 21 of 181The Ring programming language version 1.5.2 book - Part 21 of 181
The Ring programming language version 1.5.2 book - Part 21 of 181
 
Unit iv functions
Unit  iv functionsUnit  iv functions
Unit iv functions
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdf
 
functions notes.pdf python functions and opp
functions notes.pdf python functions and oppfunctions notes.pdf python functions and opp
functions notes.pdf python functions and opp
 

More from Mahmoud Samir Fayed

More from Mahmoud Samir Fayed (20)

The Ring programming language version 1.10 book - Part 212 of 212
The Ring programming language version 1.10 book - Part 212 of 212The Ring programming language version 1.10 book - Part 212 of 212
The Ring programming language version 1.10 book - Part 212 of 212
 
The Ring programming language version 1.10 book - Part 211 of 212
The Ring programming language version 1.10 book - Part 211 of 212The Ring programming language version 1.10 book - Part 211 of 212
The Ring programming language version 1.10 book - Part 211 of 212
 
The Ring programming language version 1.10 book - Part 210 of 212
The Ring programming language version 1.10 book - Part 210 of 212The Ring programming language version 1.10 book - Part 210 of 212
The Ring programming language version 1.10 book - Part 210 of 212
 
The Ring programming language version 1.10 book - Part 208 of 212
The Ring programming language version 1.10 book - Part 208 of 212The Ring programming language version 1.10 book - Part 208 of 212
The Ring programming language version 1.10 book - Part 208 of 212
 
The Ring programming language version 1.10 book - Part 207 of 212
The Ring programming language version 1.10 book - Part 207 of 212The Ring programming language version 1.10 book - Part 207 of 212
The Ring programming language version 1.10 book - Part 207 of 212
 
The Ring programming language version 1.10 book - Part 205 of 212
The Ring programming language version 1.10 book - Part 205 of 212The Ring programming language version 1.10 book - Part 205 of 212
The Ring programming language version 1.10 book - Part 205 of 212
 
The Ring programming language version 1.10 book - Part 206 of 212
The Ring programming language version 1.10 book - Part 206 of 212The Ring programming language version 1.10 book - Part 206 of 212
The Ring programming language version 1.10 book - Part 206 of 212
 
The Ring programming language version 1.10 book - Part 204 of 212
The Ring programming language version 1.10 book - Part 204 of 212The Ring programming language version 1.10 book - Part 204 of 212
The Ring programming language version 1.10 book - Part 204 of 212
 
The Ring programming language version 1.10 book - Part 203 of 212
The Ring programming language version 1.10 book - Part 203 of 212The Ring programming language version 1.10 book - Part 203 of 212
The Ring programming language version 1.10 book - Part 203 of 212
 
The Ring programming language version 1.10 book - Part 202 of 212
The Ring programming language version 1.10 book - Part 202 of 212The Ring programming language version 1.10 book - Part 202 of 212
The Ring programming language version 1.10 book - Part 202 of 212
 
The Ring programming language version 1.10 book - Part 201 of 212
The Ring programming language version 1.10 book - Part 201 of 212The Ring programming language version 1.10 book - Part 201 of 212
The Ring programming language version 1.10 book - Part 201 of 212
 
The Ring programming language version 1.10 book - Part 200 of 212
The Ring programming language version 1.10 book - Part 200 of 212The Ring programming language version 1.10 book - Part 200 of 212
The Ring programming language version 1.10 book - Part 200 of 212
 
The Ring programming language version 1.10 book - Part 199 of 212
The Ring programming language version 1.10 book - Part 199 of 212The Ring programming language version 1.10 book - Part 199 of 212
The Ring programming language version 1.10 book - Part 199 of 212
 
The Ring programming language version 1.10 book - Part 198 of 212
The Ring programming language version 1.10 book - Part 198 of 212The Ring programming language version 1.10 book - Part 198 of 212
The Ring programming language version 1.10 book - Part 198 of 212
 
The Ring programming language version 1.10 book - Part 197 of 212
The Ring programming language version 1.10 book - Part 197 of 212The Ring programming language version 1.10 book - Part 197 of 212
The Ring programming language version 1.10 book - Part 197 of 212
 
The Ring programming language version 1.10 book - Part 196 of 212
The Ring programming language version 1.10 book - Part 196 of 212The Ring programming language version 1.10 book - Part 196 of 212
The Ring programming language version 1.10 book - Part 196 of 212
 
The Ring programming language version 1.10 book - Part 195 of 212
The Ring programming language version 1.10 book - Part 195 of 212The Ring programming language version 1.10 book - Part 195 of 212
The Ring programming language version 1.10 book - Part 195 of 212
 
The Ring programming language version 1.10 book - Part 194 of 212
The Ring programming language version 1.10 book - Part 194 of 212The Ring programming language version 1.10 book - Part 194 of 212
The Ring programming language version 1.10 book - Part 194 of 212
 
The Ring programming language version 1.10 book - Part 193 of 212
The Ring programming language version 1.10 book - Part 193 of 212The Ring programming language version 1.10 book - Part 193 of 212
The Ring programming language version 1.10 book - Part 193 of 212
 
The Ring programming language version 1.10 book - Part 192 of 212
The Ring programming language version 1.10 book - Part 192 of 212The Ring programming language version 1.10 book - Part 192 of 212
The Ring programming language version 1.10 book - Part 192 of 212
 

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Recently uploaded (20)

API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governance
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

The Ring programming language version 1.5.2 book - Part 20 of 181

  • 1. Ring Documentation, Release 1.5.2 try { Block of statements catch Block of statements } 20.3. Exceptions 165
  • 2. CHAPTER TWENTYONE GETTING INPUT We can get input from the keyboard using • The Give Command • The GetChar() Function • The Input() Function 21.1 Give Command 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 21.2 GetChar() Function 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() 166
  • 3. Ring Documentation, Release 1.5.2 GetChar() GetChar() # End of line # 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 21.3 Input() Function 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 21.3. Input() Function 167
  • 4. CHAPTER TWENTYTWO FUNCTIONS - FIRST STYLE In this chapter we are going to learn about the next topics :- • Define functions • Call functions • Declare parameters • Send parameters • Main Function • Variables Scope • Return Value • Recursion 22.1 Define Functions To define new function Syntax: func <function_name> [parameters] Block of statements Note: No keyword is required to end the function definition. Example: func hello see "Hello from function" + nl 22.2 Call Functions To call function without parameters, we type the function name then () Tip: We can call the function before the function definition and the function code. Example: 168
  • 5. Ring Documentation, Release 1.5.2 hello() func hello see "Hello from function" + nl Example: first() second() func first see "message from the first function" + nl func second see "message from the second function" + nl 22.3 Declare parameters 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 22.4 Send Parameters 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) func sum x,y see x+y+nl 22.5 Main Function 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: 22.3. Declare parameters 169
  • 6. Ring Documentation, Release 1.5.2 # this program will print the hello world message first then execute the main function See "Hello World!" + nl func main see "Message from the main function" + nl 22.6 Variables Scope 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 for t = 1 to 10 # t is a local variable mycounter() # call function next func mycounter see x + nl # 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. 22.7 Return Value The function can return a value using the Return command. Syntax: 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: 22.6. Variables Scope 170
  • 7. Ring Documentation, Release 1.5.2 if novalue() = NULL See "the function doesn't return a value" + nl ok func novalue 22.8 Recursion The Ring programming language support Recursion and the function can call itself using different parameters. Example: see fact(5) # output = 120 func fact x if x = 0 return 1 else return x * fact(x-1) ok 22.8. Recursion 171
  • 8. CHAPTER TWENTYTHREE FUNCTIONS - SECOND STYLE In this chapter we are going to learn about the next topics :- • Define functions • Call functions • Declare parameters • Send parameters • Main Function • Variables Scope • Return Value • Recursion 23.1 Define Functions To define new function Syntax: def <function_name> [parameters] Block of statements [end] Note: the keyword ‘end’ is optional. Example: def hello put "Hello from function" + nl end 23.2 Call Functions To call function without parameters, we type the function name then () Tip: We can call the function before the function definition and the function code. Example: 172
  • 9. Ring Documentation, Release 1.5.2 hello() def hello put "Hello from function" + nl end Example: first() second() def first put "message from the first function" + nl def second put "message from the second function" + nl 23.3 Declare parameters 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 23.4 Send Parameters 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) def sum x,y put x+y+nl 23.5 Main Function 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: 23.3. Declare parameters 173
  • 10. Ring Documentation, Release 1.5.2 # this program will print the hello world message first then execute the main function put "Hello World!" + nl def main put "Message from the main function" + nl end 23.6 Variables Scope 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, It’s recommended to use the main functions instead of typing the instructions directly to set the scope of the new variables to local. 23.7 Return Value The function can return a value using the Return command. Syntax: 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: 23.6. Variables Scope 174