I. Definition 1. Function Definition:
function f_add(num_in) num_in = num_in + 1 f_add = num_inend function
Return parameters using the function name
2. subroutine Definition
sub s_add(num_in) num_in = num_in + 1end sub
No return value for the subroutine
Ii. Differences between functions and child routines in parameter passing
You can specify parameters in brackets or use a format that does not contain parentheses.
When a parameter is specified in parentheses, the parameter is passed by value. Modifications to the parameter in a function or subroutine are not reflected in the parameter. When the parameter format is not in parentheses, changes to the parameter values in the function or subroutine are actually made.
The following code demonstrates the differences between the two formats:
num_out = 0wscript.echo "num before sub:", num_out‘用不带括号的格式调用子例程s_add num_outwscript.echo "num after sub:", num_outwscript.echo ""num_out = 0wscript.echo "num before sub():", num_out‘用带括号的格式调用子例程s_add(num_out)wscript.echo "num after sub():", num_outwscript.echo ""num_out = 0wscript.echo "num before function:", num_out‘用不带括号的格式调用函数f_add num_outwscript.echo "num after function:", num_outwscript.echo ""num_out = 0wscript.echo "num before function():", num_out‘用带括号的格式调用函数f_add(num_out)wscript.echo "num after function():", num_outwscript.echo ""sub s_add(num_in) wscript.echo "num in sub", num_in num_in = num_in + 1 wscript.echo "num in sub", num_inend subfunction f_add(num_in) wscript.echo "num in function", num_in num_in = num_in + 1 wscript.echo "num in function", num_in f_add = num_inend function
The output result is as follows:
Num before Sub: 0
Num in sub 0
Num in sub 1
Num after Sub: 1
Num before sub (): 0
Num in sub 0
Num in sub 1
Num after sub (): 0
Num before function: 0
Num in function 0
Num in function 1
Num after function: 1
Num before function (): 0
Num in function 0
Num in function 1
Num after function (): 0
VBScript learning notes-functions and subroutines