Procedure and function
- Procedure
Procedure is a sub-program that does not return a value. Procedure is widely used for collection of frequently used commands but do not need to return a particular value
Syntax:
Sub nmprocedure (parameters)
Statements
End Sub
Example:
Even Numbers sub print ()
For I as integer = 2 to 200 step 2
Console.writeline (i)
Next
End Sub
Dialing procedure done by writing the name of the procedure with the required parameters (if required).
Sub main ()
Console.writeline ( "Program to write even number from 2-100")
EvenNumbersprint ()
end sub
- Function
Function similar to procedure, but returns to the caller function. Function is widely used to classify the order to conduct a test or calculation of a value.
Syntax:
Nmfunction function (parameters) as [data_type]
Statements
Return value
End function
Default:
If not mentioned [as data_type] will be considered: as object
Function must specify what data type will be returned to the caller and what the value will be returned in accordance with the type of data that has been mentioned. The value written to the command return is the overall value of the function.
Example:
OddFunctionTest (ByVal x as integer) as Boolean
If x mod 2 = 0 then
Return false
Else
Return true
End if
End function
Dialing function is done by writing a function with parameters with (if needed), and performed the function value storage. Could also function values do not need to be accommodated in one variable alone, However the direct use in selection or looping.
Sub main ()
Console.writeline ( "The program determines an odd number")
Console.writeline ( "Enter a number:")
Dim input as integer = console.readline ()
If OddFunctionTest(input)
Console.writeline ( "Odd Numbers")
Else
Console.writeline ( "Number is even")
End if
end sub
- Parameters
Parameter is included in the data pemangilan function or procedure. The data will affect the performance of the procedure or function is called. How to spend (pass) parameters into the procedure there are two ways:
a. Passing by value is a way of passing parameters by copying the value of a variable to the parameter. So if the value is changed parameters will not affect the original variable values.
b. Passing by reference is a way of passing parameters by copying the memory address of the variable to the parameters. So if the value of these parameters changed, this will affect the initial value
Example:
Sub procedure1 (ByVal a as integer, byref b as integer)
A = A +2
B = B +2
End Sub
Sub main ()
Dim X, Y as integer
X = 2
Y = 2
Procedure1 (X, Y)
Console.writeline ( "The value X:" & X)
Console.writeline ( "Value Y:" & Y)
End Sub
Output:
The value X: 2
Value Y: 4
Currently have 0 komentar: