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

PDF
The Ring programming language version 1.5.4 book - Part 21 of 185
PDF
The Ring programming language version 1.7 book - Part 24 of 196
PDF
The Ring programming language version 1.5 book - Part 4 of 31
PDF
The Ring programming language version 1.5.2 book - Part 20 of 181
PDF
The Ring programming language version 1.5.3 book - Part 21 of 184
PDF
The Ring programming language version 1.10 book - Part 29 of 212
PDF
The Ring programming language version 1.5.1 book - Part 19 of 180
PDF
The Ring programming language version 1.5.1 book - Part 20 of 180
The Ring programming language version 1.5.4 book - Part 21 of 185
The Ring programming language version 1.7 book - Part 24 of 196
The Ring programming language version 1.5 book - Part 4 of 31
The Ring programming language version 1.5.2 book - Part 20 of 181
The Ring programming language version 1.5.3 book - Part 21 of 184
The Ring programming language version 1.10 book - Part 29 of 212
The Ring programming language version 1.5.1 book - Part 19 of 180
The Ring programming language version 1.5.1 book - Part 20 of 180

What's hot (20)

PPT
Lecture#6 functions in c++
PPTX
functions of C++
PPTX
C++ programming function
PPTX
Call by value
PPTX
Functions in C++
PDF
The Ring programming language version 1.3 book - Part 13 of 88
PDF
M11 operator overloading and type conversion
PPTX
C and C++ functions
DOC
Functions
PPT
Functions in C++
PPTX
Chapter 4
PPTX
Learning C++ - Functions in C++ 3
PDF
4th unit full
PPT
C++ Function
PPT
16717 functions in C++
 
PPT
C++ functions presentation by DHEERAJ KATARIA
PPTX
Call by value or call by reference in C++
PPT
User Defined Functions
PPTX
3 Function & Storage Class.pptx
PDF
03 function overloading
Lecture#6 functions in c++
functions of C++
C++ programming function
Call by value
Functions in C++
The Ring programming language version 1.3 book - Part 13 of 88
M11 operator overloading and type conversion
C and C++ functions
Functions
Functions in C++
Chapter 4
Learning C++ - Functions in C++ 3
4th unit full
C++ Function
16717 functions in C++
 
C++ functions presentation by DHEERAJ KATARIA
Call by value or call by reference in C++
User Defined Functions
3 Function & Storage Class.pptx
03 function overloading
Ad

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

More from Mahmoud Samir Fayed (20)

PDF
The Ring programming language version 1.10 book - Part 212 of 212
PDF
The Ring programming language version 1.10 book - Part 211 of 212
PDF
The Ring programming language version 1.10 book - Part 210 of 212
PDF
The Ring programming language version 1.10 book - Part 208 of 212
PDF
The Ring programming language version 1.10 book - Part 207 of 212
PDF
The Ring programming language version 1.10 book - Part 205 of 212
PDF
The Ring programming language version 1.10 book - Part 206 of 212
PDF
The Ring programming language version 1.10 book - Part 204 of 212
PDF
The Ring programming language version 1.10 book - Part 203 of 212
PDF
The Ring programming language version 1.10 book - Part 202 of 212
PDF
The Ring programming language version 1.10 book - Part 201 of 212
PDF
The Ring programming language version 1.10 book - Part 200 of 212
PDF
The Ring programming language version 1.10 book - Part 199 of 212
PDF
The Ring programming language version 1.10 book - Part 198 of 212
PDF
The Ring programming language version 1.10 book - Part 197 of 212
PDF
The Ring programming language version 1.10 book - Part 196 of 212
PDF
The Ring programming language version 1.10 book - Part 195 of 212
PDF
The Ring programming language version 1.10 book - Part 194 of 212
PDF
The Ring programming language version 1.10 book - Part 193 of 212
PDF
The Ring programming language version 1.10 book - Part 192 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 210 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 205 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 203 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 200 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 197 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 194 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

Recently uploaded (20)

PDF
Nekopoi APK 2025 free lastest update
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Essential Infomation Tech presentation.pptx
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
System and Network Administraation Chapter 3
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
top salesforce developer skills in 2025.pdf
PDF
System and Network Administration Chapter 2
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
Nekopoi APK 2025 free lastest update
Wondershare Filmora 15 Crack With Activation Key [2025
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Operating system designcfffgfgggggggvggggggggg
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Navsoft: AI-Powered Business Solutions & Custom Software Development
PTS Company Brochure 2025 (1).pdf.......
Understanding Forklifts - TECH EHS Solution
Essential Infomation Tech presentation.pptx
How to Choose the Right IT Partner for Your Business in Malaysia
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Which alternative to Crystal Reports is best for small or large businesses.pdf
System and Network Administraation Chapter 3
2025 Textile ERP Trends: SAP, Odoo & Oracle
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
top salesforce developer skills in 2025.pdf
System and Network Administration Chapter 2
wealthsignaloriginal-com-DS-text-... (1).pdf

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