Functions - Parameter Passing
-
Is there a tutorial on this somewhere? I'm going nuts here.... how many different variables do i need? i'm writing a program that uses a function to accept a number as a parameter. make some calculations to it, and return it.. then sub main will output the results... don't give me code please...i just want some pointers... "I'm not me when I dream...anymore." - TRUSTcompany
-
Is there a tutorial on this somewhere? I'm going nuts here.... how many different variables do i need? i'm writing a program that uses a function to accept a number as a parameter. make some calculations to it, and return it.. then sub main will output the results... don't give me code please...i just want some pointers... "I'm not me when I dream...anymore." - TRUSTcompany
-
Is there a tutorial on this somewhere? I'm going nuts here.... how many different variables do i need? i'm writing a program that uses a function to accept a number as a parameter. make some calculations to it, and return it.. then sub main will output the results... don't give me code please...i just want some pointers... "I'm not me when I dream...anymore." - TRUSTcompany
Ummm...pass it "ByRef"? Or make your Function return the Value? Function should ALWAYS Return something. So if you want the ByRef method, make it a Sub Routine. Your question isn't really clear, so I hope this Helps. 'Pass ByRef Example Public Sub Test(ByRef MyValue as Integer) MyValue = 5 End Sub 'Return a Variable Public Function Test(ByRef MyValue as Integer) As Integer Return 5 'VB.NET Test = 5 'VB6 / VB.NET End Sub
-
Ummm...pass it "ByRef"? Or make your Function return the Value? Function should ALWAYS Return something. So if you want the ByRef method, make it a Sub Routine. Your question isn't really clear, so I hope this Helps. 'Pass ByRef Example Public Sub Test(ByRef MyValue as Integer) MyValue = 5 End Sub 'Return a Variable Public Function Test(ByRef MyValue as Integer) As Integer Return 5 'VB.NET Test = 5 'VB6 / VB.NET End Sub
i asked the professor what i forgot was that when u call the function you have to store the value somewhere: thanks though :) ex: Sub Main() Dim int as integer int = MyFunction(5) End Sub Private Function MyFunction(ByVal int as integer) as integer Dim sum as integer sum = int * 10 return sum End Function ---------------------------------------------------------------------------- This works right? ______________________________________________ "I'm not me when I dream...anymore." -TRUSTcompany
-
thanks for the help! NOT! ______________________________________________ "I'm not me when I dream...anymore." -TRUSTcompany
Sorry for not responding dude, but really now.... There are examples on how to create/call functions in VB everywhere. Just download just about any sample VB code and you will see a sample of how to write and consume a function. I did not answer this question because I felt that it was asked by someone that clearly needed to do some looking around themselves on the subject. Sorry... Looking at the responses that you did get and what you found on your own you got the concept. Just please remember that no one here OWES anyone an answer. Just as a personal note… I hate function that have byref arguments. They are used alot in the Windows world and I still hate them. IMHO functions take in arguments and return a value. That is the pure way of thinking about it. When you use a function you always think of it as:
returnValue = FunctionName(arg1, arg2, … argn)
And the function looks as follows:
<scope> Function FunctionName(arg1 As Type, arg2 As Type, … argn As Type) As ReturnType
'define the return value to use
Dim retVal as ReturnType'perform what ever calculations you need to do
'set the return value
retVal = answerToCalculationReturn ReturnType 'if you are using VB.NET
FunctionName = answerToCalulation 'if you are using VB6End Function
The number of input args are up to what you need, and it is very clear as to what is being returned and where. Using
ByRef
is called output arguments. I don't like them at all. X| As I see it, if the returned values does not end up on the left side of a function call then it is not a function, it is a Subroutine.
Paul Watson wrote: "At the end of the day it is what you produce that counts, not how many doctorates you have on the wall."
George Carlin wrote: "Don't sweat the petty things, and don't pet the sweaty things."