已经有一段时间了,但我认为问题实际上是您无法返回 UDT(用户定义的类型,又名“任何非内置类型”)。您需要做的是将第三个参数传递给Vector2Add 并将其设为SUB。例如:
SUB Vector2Add (r AS Vector2, a AS Vector2, b AS Vector2)
r.x = a.x + b.x
r.y = a.y + b.y
END SUB
除了语法差异之外,SUB 几乎是与等效 C 代码的精确翻译。我的理由是,您通常在 QB 中为 FUNCTION 的名称添加类型后缀,否则它将使用其默认类型,该类型可能已被 DEFxxx M-N(或 QB64 中的 _DEFINE 覆盖;不,你不能) t 将 _DEFINE 与 UDT 一起使用)。例如,返回一个字符串:
'Default type for all identifiers beginning with 'S' is STRING.
' Type suffixes, "AS xxxx" clauses and future "DEFxxx" items override the behavior.
DEFSTR S-S
FUNCTION swapFirstLast$ (s)
swapFirstLast$ = RIGHT$(s, 1) + MID$(s, 2, LEN(s) - 2) + LEFT$(s, 1)
END FUNCTION
QB64 在这方面有点受限,因为它旨在尽可能与 QuickBASIC 4.5 使用的语法兼容。 FreeBASIC,另一种基于QB的语言,has no such restriction:
'Notice the "AS Vector2" at the end of this next line and the RETURN statement
' that FB introduced for FUNCTIONs (originally it was used to return from an
' "old-style" subroutine that was invoked via the GOSUB statement).
FUNCTION Vector2Add (a AS Vector2, b AS Vector2) AS Vector2
DIM r AS Vector2
r.x = a.x + b.x
r.y = a.y + b.y
RETURN r
END FUNCTION
要记住的重要一点是,QB64 基本上仍然是 QB,除了它将编译代码以在现代操作系统(而不是 DOS)上运行。另一方面,FreeBASIC 选择牺牲一些兼容性来支持创建一种保留 QB 语法的更“现代”的语言。