Click here to Skip to main content
15,041,393 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted
Comments
Sandeep Mewara 15-Oct-10 11:19am
   
?????

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:
VB
Public Class Math
  Public Function Multiply(...) As Double

  Private Function MultiplySubFunction(...) As Double
End Class


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:
VB
Public Function Multiply(ByVal firstNo As Double, ByVal secondNo as Double) As Double

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:
VB
Public Function Multiply(ByVal firstNo As Double, ByVal secondNo As Double) As Double
  Return (firstNo * secondNo)
End Function
   
Comments
Sandeep Mewara 15-Oct-10 13:26pm
   
1. No. I just didn't wanted to present it in othr language
2. It was easier for me to write in C#
:)
Sandeep Mewara 15-Oct-10 13:27pm
   
Though, your explaination definitly deserves a 5! :)
Abdalla hammad 16-Oct-10 3:51am
   
thank you a lot, i appreciate the time you spent on this :)
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.
   
Will give you C# code:
C#
// 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.
   
Comments
William Winner 15-Oct-10 11:37am
   
Is Vb code too difficult for you?
Abdalla hammad 16-Oct-10 3:55am
   
that helps too, thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900