写作功能 [英] writing functions
问题描述
如果我正在编写一个函数,该函数需要一个数字并对其执行任何操作,则将其乘以2也许...在编写函数时如何引用该变量?
if im writing a function, that takes a number and does whatever to it, multiply it by two maybe...how do i refer to that variable when writing the function?
推荐答案
好吧,由于您将此问题发布为VB.Net,所以我想有人应该告诉您如何在VB.Net中这样做,因为存在一些差异.
函数声明包含几个部分.第一个处理功能的范围.范围标识可从何处访问功能.两个主要选项是公共"和私人". Public意味着可以从类本身之外访问该函数.私有意味着该函数只能在其编写的类中访问.
例如:
Well, since you posted this as a VB.Net question, I guess someone should show you how to do it in VB.Net since there are some differences.
The function declaration has several parts. The first deals with the scope of the function. The scope identifies where the function can be accessed from. The two main options are Public and Private. Public means that that function can be accessed from outside of the class itself. Private means that the function can only be accessed within the class that it is written.
For example:
Public Class Math
Public Function Multiply(...) As Double
Private Function MultiplySubFunction(...) As Double
End Class
如果您在表单中引用了Math
类,则可以从表单中调用公共功能Multiply
.但是,由于MutliplySubFunction
是私有的,因此只能从Math类中编写的代码中对其进行访问.
下一部分将说明这将是哪种方法(Function
或Sub
).函数将返回定义为变量的任何类型的变量. Sub不会返回任何内容.
然后,您具有方法的名称.名称后面是一组括号.您可以在这些括号内定义将传递给方法的变量.
在此示例中:
If you have referenced the Math
class within your form, then you can call the public function Multiply
from within your form. However, because MutliplySubFunction
is private, it is only accessible from code written within the Math class.
The next part tells what type of method this will be (Function
or Sub
). A Function returns a variable of whatever type you define it as. A Sub does not return anything.
Then, you have the name of the method. Following the name are a set of parentheses. It is within these parentheses that you define the variables that will be passed to the method.
In this example:
Public Function Multiply(ByVal firstNo As Double, ByVal secondNo as Double) As Double
您传入两个变量firstNo
和secondNo
.变量声明本身包含几个部分,因此让我们看一下.
第一个标识如何传递变量(ByVal
或ByRef
).如果传递变量ByVal
,则该方法将在内存中创建变量的副本.这意味着无论您对该变量执行什么操作,原始变量都不会受到影响.但是,如果将其传递给ByRef
,则仅将变量的地址传递给该方法.您对引用变量所做的任何操作都会影响原始变量.在Multiply
的情况下,我们不会被要求更改原始值,因此我们将其传递给ByVal
.
变量的下一部分是变量的名称.这是您将在该方法中使用的名称.最后,您告诉它所需的变量类型.如果将其定义为Integer
,则可以将其隐式转换为Integer.例如,如果您将firstNo
声明为Integer
并将其传递给24.56,则会将其四舍五入为25.
完整的示例如下所示:
you pass in two variables firstNo
and secondNo
. The variable declaration itself has several parts, so let''s look at these.
The first identifies how the variable is passed (ByVal
or ByRef
). If you pass a variable ByVal
the method will create a copy of the variable in memory. This means that no matter what you do to that variable, the original will not be affected. However, if you pass it ByRef
, the method is just passed the address of the variable. Anything you do to a reference variable affects the original. In the case of Multiply
we aren''t being asked to change the original values, so we pass them ByVal
.
The next part of the variable is the name of the variable. This is the name that you will use within that method. Lastly, you tell it what type of variable it will required. If you define it as an Integer
, then if it can be implicitly converted to an Integer, it will be. For instance, if you declared firstNo
as an Integer
and passed it 24.56, it would round it to 25.
The full example would look like:
Public Function Multiply(ByVal firstNo As Double, ByVal secondNo As Double) As Double
Return (firstNo * secondNo)
End Function
该变量最有可能范围内的功能.
因此,您将无法从该函数外部访问它.
要访问它,您必须在函数外部声明.
然后,您将可以访问此功能的内部和外部.
可能还有其他方法可以访问变量的值(通过ref传递,使用return关键字将其返回等),但是您将无法直接访问此变量.
The variable will most likely be scoped within the function.
So you will not be able to access it from outside that function.
To access it, you will have to declare outside the function.
You will then be able to access both inside and outside this function.
There could be other ways to access the value of the variable (pass by ref, return it back using the return keyword etc) but you will not be able to access this variable directly.
将给你C#代码:
Will give you C# code:
// Sample method to show how parameters are passed and used in a given method
private int AddTwoIntegers(int firstNo, int secondNo)
{
int returnResult = 0;
returnResult = firstNo + secondNo;
}
因此,如果您看到了,我们会将数字传递给功能.它以它们为参数,然后这些参数可以在函数中的任何位置用作计算变量.
So, if you can see, we pass on the number to function. It takes them as parameters and then those parameters can be used anywhere in the function as variables for calculation.
这篇关于写作功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!