Functions

  • Thread starter Thread starter Microsoft
  • Start date Start date
M

Microsoft

I know how to call a finction and pass a parameter to it, but I don;t
understand how to take the resulting output of the function and use it from
the subroutine that called the function

Something like

I have a number
I send the number to a fumction that divides it by 2
I then can refer to the quotient as a variable in my original subroutine.
 
* "Microsoft said:
I know how to call a finction and pass a parameter to it, but I don;t
understand how to take the resulting output of the function and use it from
the subroutine that called the function

Something like

I have a number
I send the number to a fumction that divides it by 2
I then can refer to the quotient as a variable in my original subroutine.

\\\
Public Function DivideBy2(ByVal Number As Double) As Double
Return Number / 2
End Function
..
..
..
Dim d As Double = DivideBy2(34.56)
///
 
Microsoft said:
I know how to call a finction and pass a parameter to it, but I don;t
understand how to take the resulting output of the function and use it from
the subroutine that called the function

Something like

I have a number
I send the number to a fumction that divides it by 2
I then can refer to the quotient as a variable in my original subroutine.

Sub test1
Dim i as Integer = 2
Dim f as Float = test2(i)
'Now f has the result of function test2
End Sub

Funktion test2(ByVal i as Integer)
Dim f as Float = i / 2
Return f
End Funktion

Cheers

Arne Janning
 
Arne said:
Sub test1
Dim i as Integer = 2
Dim f as Float = test2(i)
'Now f has the result of function test2
End Sub

Funktion test2(ByVal i as Integer)
Dim f as Float = i / 2
Return f
End Funktion

Cheers

Arne Janning

Ups,

this won't work...

Sub test1
Dim i as Integer = 2
Dim f as Float = test2(i)
'Now f has the result of function test2
End Sub

Funktion test2(ByVal i as Integer) as Float
Dim f as Float = i / 2
Return f
End Funktion

Now it works ;-)
Or take Herfrieds version...

Cheers

Arne Janning
 
Microsoft said:
I know how to call a finction and pass a parameter to it, but I
don;t understand how to take the resulting output of the function and
use it from the subroutine that called the function

Something like

I have a number
I send the number to a fumction that divides it by 2
I then can refer to the quotient as a variable in my original
subroutine.

http://msdn.microsoft.com/library/en-us/vbcn7/html/vbconFunctionProcedures.asp


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
* Arne Janning said:
Dim f as Float = test2(i)
'Float'?

Funktion test2(ByVal i as Integer) as Float
Dim f as Float = i / 2
Return f
End Funktion
'Funktion'?

Now it works ;-)
Or take Herfrieds version...

LOL!
 
Microsoft said:
Thanks for the replies. Can a fumction return more than one value in vb.net?

No. (See the link posted by Armin)

But it can return arrays, structures or objects which can of cource
contain more than one value.

Cheers

Arne Janning
 
* "Microsoft said:
Thanks for the replies. Can a fumction return more than one value in vb.net?

There are different ways to return more than one value:

\\\
Public Sub Swap(ByRef x As Integer, ByRef y As Integer)
dim t As Integer = x
x = y
y = t
End Sub
..
..
..
Dim i As Integer = 22
Dim j As Integer = 99
Swap(i, j)
MsgBox(CStr(i))
MsgBox(CStr(j))
///

- or -

\\\
Public Class CalculationResult
Public x As Integer
Public y As Integer
Public s As String
...
End Class
..
..
..
Public Function Calculate(...) As CalculationResult
Dim c As New CalculationResult()
c.x = ...
c.y = ...
Return c
End Function
..
..
..
Dim c As CalculationResult = Calculate(...)
MsgBox(CStr(c.x))
MsgBox(CStr(c.y))
MsgBox(c.s)
///

- or -

\\\
Public Function Foo() As String()
Return New String() {"Hello", "World"}
End Function
..
..
..
Dim astr() As String = Foo()
MsgBox(astr(0))
MsgBox(astr(1))
///
 
Hi Arne,

From which country are you, Herfried saw the same as me, "Funktion" while it
is normaly in the program languages Function.

That was the only LOL about it, I think you made consequent typos by typing
it in the message.

(It is not impossible it is in the German language with a K, I do not know
that, in Dutch it is official with C however often written with a K)

:-)

Cor
 
* "Cor Ligthert said:
From which country are you, Herfried saw the same as me, "Funktion" while it
is normaly in the program languages Function.

That was the only LOL about it, I think you made consequent typos by typing
it in the message.

(It is not impossible it is in the German language with a K, I do not know
that, in Dutch it is official with C however often written with a K)

AFAIK, Arne is from Germany, and in German it's "Funktion". I assume
Arne uses C# more than he uses VB.NET, so he doesn't know how to spell
'Function' :-).

SCNR
 
Herfried said:
AFAIK, Arne is from Germany,

Correct.

and in German it's "Funktion". I assume
Arne uses C# more than he uses VB.NET,

Correct as well, although I have been a VB-Classic-programmer for years.

so he doesn't know how to spell
'Function' :-).

I only spent too much time in writing a custom wrapper class for the
IActiveDesktop interface and did the post without concentration :-(

I'm sorry for that.

Cheers

Arne Janning
 
Hello:

I want a function to return three variables to the calling procedure:

Private Function CalcTimes(ByVal iAddDays as Integer, ByVal iAddHours as
Integer, ByVal iAddMins as Integer) As Array

Variable values are calculated in the function.

Calling procedure receives the values preferably into variables of the same
name.

Like: Me.CalcTimes(iAddDays, iAddHours, iAddMins)

Further use is ...(New TimeSpan(iAddDays, iAddHours, iAddMins, 0))

Could use ParamArray Arguments?

Is As Array correct or should it be As Integer?
Do I need to declare CalcTimes as an array before it is used, or is it an
array by definition?

This is somewhat confusing to me.
Balena's Prog-Vb ParamArray example begins:
Function Sum(ParamArray ByVal args() As Integer) As Integer
Dim index As Integer
For index = 0 to UBound(args)....
No mention of index being an array as Dim index As Array

Sum? Is that a keyword being used, or just a poor choice of a symbol
representation?
So far, I'm getting a debug error. I tried a return statement:
Return CalcTimes(iAddDays, iAddHours, iAddMins)
I'm not even sure about using a return statement here. It immediately
preceeds the End Function statement anyway.
Sorry about being lazy. 5:36AM is near end of the day for me.
Thanks.

Dennis D.
 
Dennis D. said:
Hello:

I want a function to return three variables to the calling procedure:

Private Function CalcTimes(ByVal iAddDays as Integer, ByVal iAddHours as
Integer, ByVal iAddMins as Integer) As Array

Variable values are calculated in the function.

Calling procedure receives the values preferably into variables of the
same name.

Like: Me.CalcTimes(iAddDays, iAddHours, iAddMins)

Further use is ...(New TimeSpan(iAddDays, iAddHours, iAddMins, 0))

Could use ParamArray Arguments?

Is As Array correct or should it be As Integer?
Do I need to declare CalcTimes as an array before it is used, or is it an
array by definition?

This is somewhat confusing to me.
Balena's Prog-Vb ParamArray example begins:
Function Sum(ParamArray ByVal args() As Integer) As Integer
Dim index As Integer
For index = 0 to UBound(args)....
No mention of index being an array as Dim index As Array

Sum? Is that a keyword being used, or just a poor choice of a symbol
representation?
So far, I'm getting a debug error. I tried a return statement:
Return CalcTimes(iAddDays, iAddHours, iAddMins)
I'm not even sure about using a return statement here. It immediately
preceeds the End Function statement anyway.
Sorry about being lazy. 5:36AM is near end of the day for me.
Thanks.

Dennis D.
Whoops. Index is a counter. My bass.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top