找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 1928|回复: 1

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

[复制链接]

51

主题

31

回帖

648

积分

用户组: 大·技术宅

UID
3260
精华
7
威望
12 点
宅币
506 个
贡献
1 次
宅之契约
0 份
在线时间
23 小时
注册时间
2017-12-26
发表于 2018-2-26 22:16:15 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

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

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

传递参数给程序

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

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

  5. Dim arg As Integer = 10
  6. Print "The variable 'arg' before the call = " & arg
  7. Procedure(arg)
  8. 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关键字:
  1. Sub Procedure (ByRef param As Integer)
  2.     param *= 2
  3.     Print "The parameter 'param' = " & param
  4. End Sub

  5. Dim arg As Integer = 10
  6. Print "The variable 'arg' before the call = " & arg
  7. Procedure(arg)
  8. 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参数引用相同的内存位置,地址指向。
  1. Sub f( ByRef i As Integer )
  2.     i = 456
  3. End Sub

  4. Dim i As Integer = 123
  5. Dim pi As Integer Ptr = @i

  6. Print i
  7. f( ByVal pi )
  8. Print i
复制代码

返回值

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

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

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

  14. Declare Function myFunction () As Integer

  15. Dim a As Integer

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

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

  20. Function myFunction () As Integer
  21.   'Here we tell myFunction to return 10.
  22.   Function = 10
  23. End Function
复制代码

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

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

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

  4.     Function = ByVal pi

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

  8. Print i, f( )
复制代码



回复

使用道具 举报

1

主题

159

回帖

622

积分

用户组: 大·技术宅

UID
7535
精华
0
威望
0 点
宅币
462 个
贡献
0 次
宅之契约
0 份
在线时间
71 小时
注册时间
2021-10-16
发表于 2022-5-17 10:40:00 | 显示全部楼层

珍惜生命,果断回帖。
回复 赞! 靠!

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-4-18 20:15 , Processed in 0.040126 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表