Passing values to and from a function

K

Karen

I have a function which I call. I pass several variables to the function and I want to also get a variable passed back from the function.

This works fine now for passing variables to the function:
Call ComboAdd(NewData, "TestContactTable", "TradeorSkillType", TradeorSkillType)

After the function has done it's work I want to get back from the function a variable that I can use to determine which of two operations were done.

Can I pass several variables to a function and get back one value to act on?

Karen
 
P

Pavel Romashkin

You have many options here.
First, if your routine is declared as a function, you can return a value
(one value).

Result = MyFunction(a, b, c)
IF Result = 2 THEN ... and so on.

Also, you can pass parameters by reference, in which case they are
returned back from the routine. This method works for either Sub or Function.
There are other options too, but these are the simplest.
I could not figure out the advantage of using CALL as opposed to simply
calling the routine without it. I read that it can be used "for clarity
of the code". I guess its a matter of personal preference.

Pavel
 
K

Karen

Hi Pavel,

I hate to sound dense but let's say I use the format you suggest, say I want
to have a variable 'result' to have a value of 1 or 2 depending on actions
in the function so I could do this:

result = ComboAdd(NewData, "TestContactTable", "TradeorSkillType",
TradeorSkillType, resultback)

The first four are values I pass to the function; the last one, 'resultback'
is the value I want to get back from the function so I can do the

IF result = 1 then.....

I set resultback to a value in the function but it doesn't seem to pass
back.

Hope this makes sense!

Karen
 
K

Karen

Got it!

I had the function as ComboAdd(firstthing as string, secondthing as string)

All I had to do was change the function to ComboAdd(firstthing as string,
secondthing as string) as string.
Then I call it from the sub as ResponseBack = ComboAdd(firstthing as
string, secondthing as string)

Works like a charm. Thanks Pavel for getting me to look in the right
direction.

Karen
 
P

Pavel Romashkin

You also could declare Secondthing ByRef, and it would then return back
to the caller:

public function ComboAdd(firstthing as string, _
byref secondthing as string) as string
secondthing = "Prefix " + firstthing
ComboAdd = secondthing + " suffix"
end

then you will have in the caller function:

Dim StrWithPrefix as String
FullString = ComboAdd("Middle part", StrWithPrefix)

and both FullString and StrWithPrefix will be properly defined.
Pavel
 

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

Top