• VBScript学习笔记


    一、定义

    1. 函数定义:

    function f_add(num_in)
        num_in = num_in + 1
        f_add = num_in
    end function

    用函数名来返回参数

    2. 子例程定义

    sub s_add(num_in)
        num_in = num_in + 1
    end sub

    子例程没有返回值

    二、函数与子例程在参数传递上的差别

    函数和子例程调用时可以有两种方式:在括号中指定参数,或使用不带括号的格式。

    使用带括号的方式指定参数时,参数是按值传递的,函数或子例程中对参数的修改不会反映到参数中。而用不带括号的格式时,则函数或子例程中对参数值的改动都是实际发生的。

    以下代码示范了这两种格式的差别:

    num_out = 0
    wscript.echo "num before sub:", num_out
    '用不带括号的格式调用子例程
    s_add num_out
    wscript.echo "num after sub:", num_out
    wscript.echo ""
    
    num_out = 0
    wscript.echo "num before sub():", num_out
    '用带括号的格式调用子例程
    s_add(num_out)
    wscript.echo "num after sub():", num_out
    wscript.echo ""
    
    num_out = 0
    wscript.echo "num before function:", num_out
    '用不带括号的格式调用函数
    f_add num_out
    wscript.echo "num after function:", num_out
    wscript.echo ""
    
    num_out = 0
    wscript.echo "num before function():", num_out
    '用带括号的格式调用函数
    f_add(num_out)
    wscript.echo "num after function():", num_out
    wscript.echo ""
    
    sub s_add(num_in)
        wscript.echo "num in sub", num_in
        num_in = num_in + 1
        wscript.echo "num in sub", num_in
    end sub
    
    function 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_in
    end function

    输出结果如下


    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

  • 相关阅读:
    算法
    数据结构
    BZOJ 3514 GERALD07加强版 (LCT+主席树)
    扩展莫队小总结 (树上/带修改莫队)
    HDU 5729 Rigid Frameworks (联通块计数问题)
    博弈论题目总结(三)——组合游戏进阶
    CF135E Weak Subsequence (计数问题)
    luogu 2483 K短路 (可持久化左偏树)
    博弈论题目总结(二)——SG组合游戏及变形
    BZOJ 2006 [NOI2010]超级钢琴 (堆+主席树)
  • 原文地址:https://www.cnblogs.com/DigiK0ne/p/4015145.html
Copyright © 2020-2023  润新知