SlideShare a Scribd company logo
CHAPTER
TWENTYTHREE
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
23.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
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:
193
Ring Documentation, Release 1.6
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
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:
func sum x,y
see x+y+nl
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)
func sum x,y see 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 194
Ring Documentation, Release 1.6
# 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
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.
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.
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 195
Ring Documentation, Release 1.6
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
func fact x if x = 0 return 1 else return x * fact(x-1) ok
23.8. Recursion 196
CHAPTER
TWENTYFOUR
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
24.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
24.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:
197
Ring Documentation, Release 1.6
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
24.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
24.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
24.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:
24.3. Declare parameters 198
Ring Documentation, Release 1.6
# 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
24.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.
24.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:
24.6. Variables Scope 199
Ring Documentation, Release 1.6
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
def fact x if x = 0 return 1 else return x * fact(x-1) end
24.8. Recursion 200
CHAPTER
TWENTYFIVE
FUNCTIONS - THIRD 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
25.1 Define Functions
To define new function
Syntax:
func <function_name> [parameters] ['{']
Block of statements
['}']
Example:
load "stdlib.ring"
func hello {
print("Hello from function n")
}
25.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:
201
Ring Documentation, Release 1.6
load "stdlib.ring"
hello()
func hello {
print("Hello from function n")
}
Example:
load "stdlib.ring"
first() second()
func first { print("message from the first function n") }
func second { print("message from the second function n") }
25.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:
load "stdlib.ring"
func sum(x,y) {
print(x+y)
}
25.4 Send Parameters
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)
func sum(x,y) { print(x+y) }
25.3. Declare parameters 202

More Related Content

What's hot (20)

PPT
Lecture#6 functions in c++
NUST Stuff
 
PPTX
functions of C++
tarandeep_kaur
 
PPTX
C++ programming function
Vishalini Mugunen
 
PPTX
Call by value
Dharani G
 
PPTX
Functions in C++
home
 
PDF
The Ring programming language version 1.3 book - Part 13 of 88
Mahmoud Samir Fayed
 
PDF
M11 operator overloading and type conversion
NabeelaNousheen
 
PPTX
C and C++ functions
kavitha muneeshwaran
 
DOC
Functions
zeeshan841
 
PPT
Functions in C++
Sachin Sharma
 
PPTX
Chapter 4
temkin abdlkader
 
PPTX
Learning C++ - Functions in C++ 3
Ali Aminian
 
PDF
4th unit full
Murali Saktheeswaran
 
PPT
C++ Function
Hajar
 
PPT
16717 functions in C++
LPU
 
PPT
C++ functions presentation by DHEERAJ KATARIA
Dheeraj Kataria
 
PPTX
Call by value or call by reference in C++
Sachin Yadav
 
PPT
User Defined Functions
Praveen M Jigajinni
 
PPTX
3 Function & Storage Class.pptx
aarockiaabinsAPIICSE
 
PDF
03 function overloading
Jasleen Kaur (Chandigarh University)
 
Lecture#6 functions in c++
NUST Stuff
 
functions of C++
tarandeep_kaur
 
C++ programming function
Vishalini Mugunen
 
Call by value
Dharani G
 
Functions in C++
home
 
The Ring programming language version 1.3 book - Part 13 of 88
Mahmoud Samir Fayed
 
M11 operator overloading and type conversion
NabeelaNousheen
 
C and C++ functions
kavitha muneeshwaran
 
Functions
zeeshan841
 
Functions in C++
Sachin Sharma
 
Chapter 4
temkin abdlkader
 
Learning C++ - Functions in C++ 3
Ali Aminian
 
4th unit full
Murali Saktheeswaran
 
C++ Function
Hajar
 
16717 functions in C++
LPU
 
C++ functions presentation by DHEERAJ KATARIA
Dheeraj Kataria
 
Call by value or call by reference in C++
Sachin Yadav
 
User Defined Functions
Praveen M Jigajinni
 
3 Function & Storage Class.pptx
aarockiaabinsAPIICSE
 
03 function overloading
Jasleen Kaur (Chandigarh University)
 

Similar to The Ring programming language version 1.6 book - Part 23 of 189 (20)

PDF
The Ring programming language version 1.8 book - Part 26 of 202
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.9 book - Part 28 of 210
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.9 book - Part 27 of 210
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.8 book - Part 25 of 202
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.2 book - Part 21 of 181
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 11 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.4 book - Part 22 of 185
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.7 book - Part 25 of 196
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.9 book - Part 40 of 210
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.1 book - Part 30 of 180
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.8 book - Part 37 of 202
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 21 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.6 book - Part 24 of 189
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 7 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 41 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.2 book - Part 31 of 181
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.7 book - Part 35 of 196
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.3 book - Part 57 of 88
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.4 book - Part 5 of 30
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 54 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 26 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 28 of 210
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 27 of 210
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 25 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 21 of 181
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 11 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 22 of 185
Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 25 of 196
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 40 of 210
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 30 of 180
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 37 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 21 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 24 of 189
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 7 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 41 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 31 of 181
Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 35 of 196
Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 57 of 88
Mahmoud Samir Fayed
 
The Ring programming language version 1.4 book - Part 5 of 30
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 54 of 84
Mahmoud Samir Fayed
 
Ad

More from Mahmoud Samir Fayed (20)

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

Recently uploaded (20)

PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PDF
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PDF
Adobe Premiere Pro Crack / Full Version / Free Download
hashhshs786
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PPTX
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
PDF
IObit Driver Booster Pro 12.4.0.585 Crack Free Download
henryc1122g
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
Adobe Premiere Pro Crack / Full Version / Free Download
hashhshs786
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
IObit Driver Booster Pro 12.4.0.585 Crack Free Download
henryc1122g
 

The Ring programming language version 1.6 book - Part 23 of 189

  • 1. CHAPTER TWENTYTHREE 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 23.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 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: 193
  • 2. Ring Documentation, Release 1.6 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 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: func sum x,y see x+y+nl 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) func sum x,y see 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 194
  • 3. Ring Documentation, Release 1.6 # 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 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. 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. 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 195
  • 4. Ring Documentation, Release 1.6 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 func fact x if x = 0 return 1 else return x * fact(x-1) ok 23.8. Recursion 196
  • 5. CHAPTER TWENTYFOUR 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 24.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 24.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: 197
  • 6. Ring Documentation, Release 1.6 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 24.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 24.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 24.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: 24.3. Declare parameters 198
  • 7. Ring Documentation, Release 1.6 # 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 24.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. 24.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: 24.6. Variables Scope 199
  • 8. Ring Documentation, Release 1.6 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 def fact x if x = 0 return 1 else return x * fact(x-1) end 24.8. Recursion 200
  • 9. CHAPTER TWENTYFIVE FUNCTIONS - THIRD 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 25.1 Define Functions To define new function Syntax: func <function_name> [parameters] ['{'] Block of statements ['}'] Example: load "stdlib.ring" func hello { print("Hello from function n") } 25.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: 201
  • 10. Ring Documentation, Release 1.6 load "stdlib.ring" hello() func hello { print("Hello from function n") } Example: load "stdlib.ring" first() second() func first { print("message from the first function n") } func second { print("message from the second function n") } 25.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: load "stdlib.ring" func sum(x,y) { print(x+y) } 25.4 Send Parameters 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) func sum(x,y) { print(x+y) } 25.3. Declare parameters 202