PDA

View Full Version : Solved: Calling a Function, of Sorts



Opv
07-15-2010, 01:26 PM
Is there a way to call specifically designated lines of code within a function from another function. For example:


Sub Testing()

''''' SOME CODE HERE '''''

Test:
msgbox "This is a Test"

End Sub



Sub Testing2()

Call Testing.Test

End Sub


I know that's not the way to do it, if it is even possible.

EDGE
07-19-2010, 03:22 PM
Yeah there are a couple ways you can do this. You will need to work with functions instead of sub routines as they do not allow you to pass variables between sub routines.

Here are a couple different functions depending on you want to use the information:

Here is the main sub-routine that calls two different functions and passes a variable to them.

Sub MyTest()

' Pass value A to function and msgbox from
' the PassToMe function

PassToMe ("A")

' Pass value B to PassThenReturn return string
' value and assign it to a variable then
' msgbox the variable content

MyReturnVal = PassThenReturn("B")
MsgBox MyReturnVal

End Sub

Once in the function you can put whatever logic you want to check if a certain condition is met.

Function: PassToMe requires a string to be passed to it in order to proceed. You can then take that string value and compare the value to determine what code will actually execute.

Function PassToMe(var1 As String)

If var1 = "A" Then
' put code block here
MsgBox "In A Code Block"
End If

If var1 = "B" Then
' put code block here
MsgBox "In B Code Block"
End If

End Function
Function: PassThenReturn requires a string to be passed to it in order to proceed and will return a string. Again you can then take that string value and compare the value to determine what code will actually execute.

Function PassThenReturn(var1 As String) As String

If var1 = "A" Then
' put code block here
PassThenReturn = "In A Code Block"
End If

If var1 = "B" Then
' put code block here
PassThenReturn = "In B Code Block"
End If

End Function

Hope that helps..

Bob Phillips
07-19-2010, 03:29 PM
You will need to work with functions instead of sub routines as they do not allow you to pass variables between sub routines.

Wherever do you get that idea from?

EDGE
07-19-2010, 03:55 PM
Sorry about that I Misspoke! Should have stated only Functions can return values and that values can be passed to both Subs and Functions.

Opv
07-19-2010, 08:20 PM
Thanks. I just reread my original question, and I realized that I asked about a function; whereas, I meant to say subroutine. I was just curious as to whether a named bookmark (or whatever "Test:" would be called) within the subroutine could be accessed by reference from another subroutine.

I'll have to try to digest the function idea.

mikerickson
07-19-2010, 08:27 PM
That way leads to spaghetti code.
It might be cleaner if the subroutine were re-written as a seperate sub, to be called when needed.

Bob Phillips
07-20-2010, 12:52 AM
Sorry about that I Misspoke! Should have stated only Functions can return values and that values can be passed to both Subs and Functions.

You are mistaken about that too, Subs can return values.



Sub Caller()
Dim val1 As String

val1 = "original value"
MsgBox val1

Call called(val1)
MsgBox val1

End Sub

Sub called(ByRef passedval As String)

passedval = "changed value"
End Sub

Aflatoon
07-20-2010, 12:58 AM
That's not really returning a value though; that's altering a passed value.

Bob Phillips
07-20-2010, 02:06 AM
You think! So APIs dn't return a value either?

And doesn't a function alter a passed value?



MyVal = MyFunc()


MyVal will have a value at the start, even if empty, but will have a different value after, so in that sense a passed value is changed, just as with a sub. IT is only teh syntax that differs.

Aflatoon
07-20-2010, 02:17 AM
You think!
Sometimes, yes.

So APIs dn't return a value either?

Depends. API functions do, subs don't.


And doesn't a function alter a passed value?



MyVal = MyFunc()


MyVal will have a value at the start, even if empty, but will have a different value after, so in that sense a passed value is changed, just as with a sub. IT is only teh syntax that differs.

So on that basis, if I use:
MyVal = 5

5 is altering a passed value? MyVal is not passed to the function, the return value of the function (essentially a variable) is assigned to the MyVal variable. Again, if you wrote:

MyVal = 4
MyOtherVal = 5
MyVal = MyOtherVal

would you really argue that MyOtherVal is changing a passed value?

Bob Phillips
07-20-2010, 02:24 AM
Depends. API functions do, subs don't.

Nonsense, the API declaration frequently calls the parameter a return code. That sounds like return to me.



So on that basis, if I use:
MyVal = 5

5 is altering a passed value? MyVal is not passed to the function, the return value of the function (essentially a variable) is assigned to the MyVal variable.

What function? You are assigning a variable to a value. No function involved.


Again, if you wrote:

MyVal = 4
MyOtherVal = 5
MyVal = MyOtherVal

would you really argue that MyOtherVal is changing a passed value?

As before there is not a function or sub in sight, it is just assigning a value to a variable. I would argue that the variable is being modified, and if you wanted you can say that is changing a passed value, but I wouldn't.

I feel you have must be having a bad day and are looking to pick an argument on nothing. It doesn't change my point that subs can return a value.

Aflatoon
07-20-2010, 02:41 AM
You said:


And doesn't a function alter a passed value?



MyVal = MyFunc()


MyVal will have a value at the start, even if empty, but will have a different value after, so in that sense a passed value is changed, just as with a sub. IT is only teh syntax that differs.

which seems to be you saying that MyVal is passed to MyFunc. Therefore, my analogy was:
MyVal = 5

By your reasoning, MyVal is being passed to 5.

Hence I then disagree with:


I would argue that the variable is being modified, and if you wanted you can say that is changing a passed value, but I wouldn't.

It seems to me you just did say that.

And I'm actually having a fun day, thanks.

Bob Phillips
07-20-2010, 02:59 AM
By your reasoning, MyVal is being passed to 5.

Well, that line of reasoning (fabrication?) is total tosh, so there is no point continuing this at all.

EDGE
07-20-2010, 05:27 AM
XLD I appreciate the feedback and glad that you pointed out that there are alternative ways to achieve a similar results.

Thanks

Opv
07-20-2010, 06:54 AM
That way leads to spaghetti code.
It might be cleaner if the subroutine were re-written as a seperate sub, to be called when needed.

You're probably right. Thanks.