# Thread: Return values from a FUNCTION

1. ## Return values from a FUNCTION

I am using this FUNCTION: xyz as C (Vend,(PO,Err ) and i can not get the function to return any values!! I understood that in the function you could set XYZ to some character value and/or that you can change the values of PO and Err in the FUNCTION!! I just can not get this to work!! Can anyone show me the correct way to use a FUNCTION??? The FUNCTION does what it is supposed to do but will NOT return any values!!

Don Walton
352-243-3303

2. ## Re: Return values from a FUNCTION

Can you post the function?

3. ## Re: Return values from a FUNCTION

Function xyz as c etc.

last line is: xyz = whatever is desired
end function

That should cause it to return the value of xyz.

like this:

Code:
```function discretiontaxget as N(taxable as N,countytaxrate as N,max as N)
dim mcountytax as n
if   countytaxrate = 1.00 .and. taxable < max then
mcountytax = taxable * (countytaxrate/100)
elseif  countytaxrate = 1.00 .and. taxable >= max then
mcountytax = 50.00
elseif countytaxrate = .50 .and. taxable < max then
mcountytax = taxable * (countytaxrate/100)
elseif  countytaxrate = .50 .and. taxable >= max then
mcountytax = 25.00
elseif countytaxrate = .75 .and. taxable < max then
mcountytax = taxable * (countytaxrate/100)
elseif countytaxrate = .75 .and. taxable >= max then
mcountytax = 37.50
elseif countytaxrate = 1.25 .and. taxable < max then
mcountytax = taxable * (countytaxrate/100)
elseif countytaxrate = 1.25 .and. taxable >= max then
mcountytax = 62.50
elseif  countytaxrate = 0 then
mcountytax = 0.00
end if
discretiontaxget = mcountytax
end function```

4. ## Re: Return values from a FUNCTION

This is what I did almost exactly!! The difference is that I tried to change the parameters that were sent to the function - in your case - taxable as N,countytaxrate as N,max as N!! Ie, setting taxable to a value etc!! It seems that the only values that can be returned are in the name of function - discretiontaxget!! Is it always just a SINGLE value that can be returned?

5. ## Re: Return values from a FUNCTION

Originally Posted by DonWalton
This is what I did almost exactly!! The difference is that I tried to change the parameters that were sent to the function - in your case - taxable as N,countytaxrate as N,max as N!! Ie, setting taxable to a value etc!! It seems that the only values that can be returned are in the name of function - discretiontaxget!! Is it always just a SINGLE value that can be returned?

Some examples
Using with ... end with
Code:
```dim txt as C = "aaa"
dim num as N = 3
dim lv as P = local_variables()

msgbox("Local vars before", txt + "  " + num)
msgbox("return from function",dosomething(lv))
msgbox("Local vars after", txt + "  " + num)

Function dosomething as C (lv as P)
dim text as C
dim numb as N
with lv
txt = "bbb"
text = txt
num = num * 2
numb = num
end with
dosomething = text + "  " + numb
End function```
Using byref
Code:
```dim txt as C = "aaa"
dim num as N = 3

msgbox("Local vars before", txt + "  " + num)
msgbox("return from dosomething",dosomething(txt,num))
msgbox("Local vars after dosomething", txt + "  " + num)

dosomethingelse(txt,num)
msgbox("Local vars after do something else", txt + "  " + num)

Function dosomething as C(byref text as C, byref numb as N)
text = "bbb"
numb = numb * 2
dosomething = "something completely unrelated"
End function

Function dosomethingelse as V(byref text as C, byref numb as N)
text = "ccc"
numb = numb * 4
End function```
Using a Pointer
Code:
```dim result as P
result = return_a_pointer()
msgbox(result.Name + " lives in " + result.city)

function return_a_pointer as P()
dim return_a_pointer as P
return_a_pointer.name = "Tim"
return_a_pointer.city = "Geelong"
end function```
You could also return a crlf delimited string that contains a particular value on each line.
You could take that further and put name:value pairs on each line.
And using the flavour of the month you could return fully formed JSON

6. ## Re: Return values from a FUNCTION

Don asked: Is it always just a SINGLE value that can be returned?
Yes, that's right. But the "single" value might be a character string containing formatted data. The script that calls the function would then be responsible for parsing the string returned by the function.

For example a function might return both a first and a last name, formatted as a single character string. Like this:

"WALTON | DON"

The string returned by the function could then be separated into first and last names.

7. ## Re: Return values from a FUNCTION

You can return more than one value from a function, but subversely.
for instance:
declare a variable as global, assign it a value in the function and the use the variable anywhere. I have one function that gives a car payment and then does all the other math and those numbers are either placed in variables or placed in fields on the form directly as they are computed.
This way I have the interest and balance as well as the payment all complete.
Like this:

Code:
```FUNCTION pmtonapr AS N (p as n, r as n, mos as n, type as n, daystofirst as n)
dim global balance as n
dim global interest as n
if r <= 0.00 then
pmtonapr = p/mos
else
dim df as n
dim rte as n = (r/100)/12
df = round_UP((daystofirst-30)/30,6)
dim trm as n = 0
dim formu as n
'dim factor as n
var->factor = round_up((rte*df)*p,8) 'round_down() does not work in v7
formu = round_up(p+var->factor,5)
pmtonapr = round_down(pmts1(formu,r,mos),2)

end if
var->balance = pmts*mos
var->interest = var->balance-p

end function```

8. ## Re: Return values from a FUNCTION

Tim

Thank you for the reply!! I am definitely doing something wrong - when the FUNCTION ENDs and returns to the calling script, the name of the FUNCTION is always blank, IE does not contain the RESULT of the FUNCTION!! If I had a FUNCTION defined as XYZ as C (PO as C), the FUNCTION would set XYZ to a character result and the calling script,upon return from the FUNCTION, should be able to use XYZ and set it to a variable, for instance Char=XYZ!! I have tried so many things and I just cant get anything to work!!

9. ## Re: Return values from a FUNCTION

Thanks you!! Very helpful and I thank you!!

10. ## Re: Return values from a FUNCTION

This help me alot and I thank you!!

11. ## Re: Return values from a FUNCTION

Don,
Is this a web app you have or a desktop app?

12. ## Re: Return values from a FUNCTION

It is a desktop application!!

13. ## Re: Return values from a FUNCTION

Ok. This statement through me and is the reason for the question.

IE does not contain the RESULT of the FUNCTION!!

14. ## Re: Return values from a FUNCTION

To use the function like most functions:
from the calling script,
dim anyvar as c
anvar = xyz() and whatever calls are needed in zyz.
Like the pmt function above when called is like this.
Code:
`var->vpmt = pmtonapr(var->vfinance+var->vdocs,mon->aprate,mon->term,mon->termtype,mon->daystofirst)`
After, the variable "vpmt" has the results in it.

15. ## Re: Return values from a FUNCTION

Dave

If the name of the FUNCTION is XYZ as C() then when the function ends & returns to the calling script, XYZ should contain the result. Right? XYZ is always blank!!??!! Yet i knpow that the FUNCTION executed correctly - i watched it run with DEBUG() and XYZ contains the correct return but when I look at it in the calling program it is BLANK

16. ## Re: Return values from a FUNCTION

Don,

try:

dim My_Result as C
My_Result = XYZ()
msgbox("Status",My_result)

XYZ is a function name, it's not a variable.

17. ## Re: Return values from a FUNCTION

Tom

This is what I needed to know!! I knew I was doing something wrong and that is it - have to use the parentheses!! Will try it!!

18. ## Re: Return values from a FUNCTION

THE ANSWER IS that the FUNCTION has to be called from the script as Char = XYZ()!! Char now contains the value returned by the function!! This is what I did not know!! THANK YOU!!

19. ## Re: Return values from a FUNCTION

The answer is, a user defined function performs pretty much the same as a built in function.

Char = XYZ()

just as you would say

Char = STR(12345)

20. ## Re: Return values from a FUNCTION

Of course the confusion arises because in the IW, some functions will return a value just by doing this:

?xyz()
="something"

Whereas others must set a variable to get the return value - it would by good to know the difference for this...

21. ## Re: Return values from a FUNCTION

Whereas others must set a variable to get the return value
Are you meaning one must supply a parameter?

22. ## Re: Return values from a FUNCTION

No, I meant that in the IW sometimes

Vc = myfunction() will return a result for vc whereas

?myfunction() won't

I still have trouble getting functions that use an XD to return a result...in either case.

23. ## Re: Return values from a FUNCTION

Let me say that any variable that has been declared and value set in a function as shared or global can be read from the appropriate place. Global = anywhere, shared = from the calling form or...

24. ## Re: Return values from a FUNCTION

Vc = myfunction() will return a result for vc whereas

?myfunction() won't

I still have trouble getting functions that use an XD to return a result...in either case.
it would depend on function type. Like a logical function would return .t. or .f..
A function that declares a return value will retur what it is supposed to.
A function may just create shared or global variables and you would need to know what the variables are.
A function that does a job like updating a table may return nothing you can see.

25. ## Re: Return values from a FUNCTION

Originally Posted by MoGrace
No, I meant that in the IW sometimes

Vc = myfunction() will return a result for vc whereas

?myfunction() won't

I still have trouble getting functions that use an XD to return a result...in either case.
Robin

Can you give examples of that?

26. ## Re: Return values from a FUNCTION

Hi Al,
This zip contains my exported function and 2 small text files that should be in the same directory as the function when you import it. It is using the clipboard but will not return a value otherwise in the IW. Let me know what you think - I have others!

27. ## Re: Return values from a FUNCTION

Originally Posted by MoGrace
Hi Al,
This zip contains my exported function and 2 small text files that should be in the same directory as the function when you import it. It is using the clipboard but will not return a value otherwise in the IW. Let me know what you think - I have others!
Getting this error message on 2 machines...

RobinUnzip.png

Try attaching them without zipping them. They don't look like they are too big. Or email them to me.

28. ## Re: Return values from a FUNCTION

Its because I am unloading from home with IE8 - it makes the upload feature here hang. I will try again.

29. ## Re: Return values from a FUNCTION

You are using a modeless dialog box so the dialog opens but the code continues to run and the function finishes with the dialog box open it can't return anything.

30. ## Re: Return values from a FUNCTION

Allen,
So my problem would be resolved if I give up the roll up feature and go modal? Thanks, I'll try it. I want to call this function from my floating modeless toolbar (used for development only) so I guess I don't really need the modeless roll up feature.

Merry Christmas!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•