勇芳软件 发表于 2018-2-26 22:16:15

【VFB】程序(VFB教程2-7)

此处为VisualFreeBasic编程教程(从零开始学或VB进阶)的子章节部分,全部目录点链接。
Sub
Sub 是不返回值的过程。它们使用Declare关键字声明,并使用Sub关键字定义。声明过程引入其名称,以便可以调用它,并且过程定义列出了在调用时将执行的代码语句。A sub 简单地通过在程序中的某个地方使用它的名称。Sub MyProcedure
    Print "the body of MyProcedure"
End Sub将产生输出:
the body of MyProcedure
请注意,只需要声明来调用该过程。该过程可以在代码中稍后定义,甚至可以在不同的源文件中进行定义。
Functions
Functions 是将值返回到调用代码中的点的过程。您可以将function 调用视为对某个表达式的评估,就像变量或对象一样。它们使用Declare关键字声明,并使用Function关键字定义。在声明结尾处指定functions 返回的值的类型。' introduces and defines a procedure that returns an integer value
Function MyProcedure As Integer
    Return 10
End Function

' calls the procedure, and stores its return value in a variable
Dim i As Integer = MyProcedure
Print i将产生输出:
10
由于定义是声明,因此也可以在定义过程之后调用该过程。
调用一个过程在过程名称之后放置括号'()',以表示过程调用是一个常见的惯例。FreeBASIC不需要这个。
传递参数给程序

声明参数
程序在调用时可以以变量和对象的形式传递信息。在过程调用的上下文中,这些变量和对象被称为参数。这些参数随后在过程体中表示为所谓的参数。参数可以像任何其他变量或对象一样使用。
要指定一个过程应该在调用时传递参数,请使用参数列表声明该过程。参数列表是当引用传递给它的参数时过程将使用的一个或多个名称和类型的列表。参数列表用括号括起来。Sub Procedure (s As String, n As Integer)
    Print "The parameters have the values: " & s & " and " & n
End Sub

Procedure "abc", 123将产生以下输出:
The parameters have the values: abc and 123
有两种方式将参数传递给过程:按值和引用。默认情况下,除非另有说明,参数将通过值传递。
通过值传递参数
通过值传递的参数实际上并不传递给程序;修改和传递参数的副本。这允许过程修改副本,原始变量或对象保持不变。
当通过值将对象传递给过程时,通过调用Type或Class的复制构造函数来创建副本。
要指定一个参数应该通过值传递,在过程声明中的参数名称前面加上ByVal关键字:Sub Procedure (ByVal param As Integer)
    param *= 2
    Print "The parameter 'param' = " & param
End Sub

Dim arg As Integer = 10
Print "The variable 'arg' before the call = " & arg
Procedure(arg)
Print "The variable 'arg' after the call = " & arg将产生以下输出:
The variable 'arg' before the call = 10
The parameter 'param' = 20
The variable 'arg' after the call = 10
请注意,过程调用中括号中包含参数 - 在这种情况下只有一个arg .这些括号是可选的,但是表示过程调用的常见约定。
通过引用传递参数
与通过值传递的参数不同,通过引用传递给过程的参数真的可以被传递;没有副本。这允许过程修改传递给它的原始变量或对象。
引用就像变量或对象的别名。每当你引用一个引用,你都指的是别的实际的变量或对象。换句话说,您可以将过程的引用参数视为传递给它的参数;对参考参数所做的任何更改实际上是对其所表示的参数的更改。
要指定参数应该通过引用传递,在过程声明中的参数名称前面加上ByRef关键字:Sub Procedure (ByRef param As Integer)
    param *= 2
    Print "The parameter 'param' = " & param
End Sub

Dim arg As Integer = 10
Print "The variable 'arg' before the call = " & arg
Procedure(arg)
Print "The variable 'arg' after the call = " & arg将产生以下输出:
The variable 'arg' before the call = 10
The parameter 'param' = 20
The variable 'arg' after the call = 20
手动将指针传递给参数
通过在ByRef参数的参数前面指定Byval关键字,地址(通常存储在指针中)可以原样直接传递,强制Byref参数引用相同的内存位置,地址指向。Sub f( ByRef i As Integer )
    i = 456
End Sub

Dim i As Integer = 123
Dim pi As Integer Ptr = @i

Print i
f( ByVal pi )
Print i返回值

...指的是Function过程在函数完成时具有值,使得该值可以在表达式中使用或赋值给变量的能力。
函数的值可以通过三种方式返回:
'' Using the name of the function to set the return value and continue executing the function:
Function myfunc1() As Integer
   myfunc1 = 1
End Function

'' Using the keyword 'Function' to set the return value and continue executing the function:
Function myfunc2() As Integer
   Function = 2
End Function

'' Using the keyword 'Return' to set the return value and immediately exit the function:
Function myfunc3() As Integer
   Return 3
End Function
'' This program demonstrates a function returning a value.

Declare Function myFunction () As Integer

Dim a As Integer

'Here we take what myFunction returns and add 10.
a = myFunction() + 10

'knowing that myFunction returns 10, we get 10+10=20 and will print 20.
Print a

Function myFunction () As Integer
'Here we tell myFunction to return 10.
Function = 10
End Function
返回参考
函数结果也可以通过引用返回,而不是通过值返回。语义是完全不同的。
通过Function = variable或Return variable语句赋值Byref函数结果时,该函数不会复制并返回变量的值。相反,它返回对该变量的引用。该函数的调用者可以通过从函数返回的引用来修改变量,而无需手动使用指针。这非常像ByRef参数。

从Byref函数手动返回指针
通过在Function = variable或Return variable语句中的结果变量前面指定Byval关键字,地址(通常存储在指针中)可以原样直接传递,强制Byref函数结果引用地址指向的相同内存位置。例如:
Dim Shared i As Integer = 123

Function f( ) ByRef As Integer
    Dim pi As Integer Ptr = @i

    Function = ByVal pi

    '' or, with RETURN it would look like this:
    Return ByVal pi
End Function

Print i, f( )


xiawan 发表于 2022-5-17 10:40:00


珍惜生命,果断回帖。
页: [1]
查看完整版本: 【VFB】程序(VFB教程2-7)