Procedures Overview
 
不同FB程序类型的概述。

程序是可以在任何程序中任意次数执行或调用的代码块。执行的代码称为过程体。FreeBASIC中有两种类型的过程:不返回值和过程的过程。

替补

Subs 是不返回值的过程。它们使用Declare关键字声明,并使用Sub关键字定义。声明过程引入其名称,以便可以调用它,并且过程定义列出了在调用时将执行的代码语句。A sub 简单地通过在程序中的某个地方使用它的名称。

' introduces the sub 'MyProcedure'
Declare Sub MyProcedure

' calls the procedure 'MyProcedure'
MyProcedure

' defines the procedure body for 'MyProcedure'
Sub MyProcedure
    Print "the body of MyProcedure"
End Sub

将产生输出:

the body of MyProcedure

请注意,只需要声明来调用该过程。该过程可以在代码中稍后定义,甚至可以在不同的源文件中进行定义。

功能

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不需要这个。

参考