Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function to return a value

Status
Not open for further replies.

OldtTimerDon

Programmer
Oct 6, 2012
34
US
Hi, the Senile Senior is having a problem with a function returning a value.

I have a form that contains an editbox that does not have a controlsource tied to a table. The intent of the edit box is to display data compiled by my FetchIt function. The function is called by FetchIt(acno). This routine captures data from several sources and merges it into a variable cMyText. My goal is to display cMyText in the editbox for a particular client. The editbox is read-only.

Any change in the client viewed produces a corresponding change in the edit box. When there is no corresponding data for a client, the edit box should be blank.
In the Init event for the editbox i call the routine with FetchIt(acno). Because the routine processes other tables, I have a Select (origtable) followed by THIS.Value=cMyText.
The same code is found in the refresh event.

I tried FetchIt(acno, cMyText) and it didn't work. I'm baffled but glad I don't have to bill a client for my time.

What am I doing wrong?

Senile Senior Don
Sun City, AZ
 
If a function returns a value, that means you don't only set cMyText in it, you finally RETURN cMyText. It doesn't matter how you name the variable you use inside of your function, what matters is the final RETURN statement. To get the return value, your call of the function has to set a variable or property.

For example you could do

Code:
Editbox.Value = FetchIt(acno)

Take a look at the functions section of the VFP help, no matter if using a native function or a user defined function, the value the function results in is fetched by assigning it to a variable or property. You will find plenty of samples showing that. For example some math functions like SIN() or COS() or TAN() or ARC(), they all are called with a numeric parameter, eg "x", and return the function value, let's say "y", but not as SIN(x,y), you do y=Sin(x), as you write it in math. And the same easy notation is used in programming nowadays, not only in Foxpro.

And last least, I'm afraid I have to remind you, in one of your first post I explained how you could use a function already in quite detail for another editbox value.

See thread184-1713419

I even explained the general way to use functions as either
1. VARIABLE = function(parameters)
2. do procedure with parameter TO VARIABLE

As you see the second form is old style Foxpro, outdated, you may remember that form, which you can also do with FUNCTIONS, not only PROCEDURE, you can also DO a PRG.

Bye, Olaf.
 
Olaf said:
As you see the second form is old style Foxpro, outdated

The pedant here again. The second form isn't old-time at all. We got DO proc TO var at the same time we got DO form TO var.

In other words, it's as outdated as DO FORM. <g>
 
Olaf,
Thanks for helping with this problem which had me hung up. It now works great! I thought I needed a second variable for the text I wanted returned. I see now that after manipulation, I am returning info through the same portal through which I sent values.

Warmly,
the Senile Senior
 
Hi dan,

so the syntax y=f(x) was always there and do proc with variables to variable was introduced later? Weird. What about STORE? Well, off topic.


Glad you solved it, Don. I assume with "same portal through which I sent values" you mean you're using a by reference parameter.

That's also a difference between DO Function WITH ... TO dest and dest = Function(...), WITH ... passes values by reference, so a procedure/function changing the input values influence the variable at the sender, too, in general. I won't expand the explanation, as it leads off topic. You can also pass values with the more wideespread synatx by calling Function(@Parameter).

Also read about SET UDFPARMS and the variable scope PRIVATE. Loose coupling (eg think about http request/response) make by value calls the normal way today, not by reference.

Bye, Olaf.
 
We got DO proc TO var at the same time we got DO form TO var.

Are you saying that the following is valid syntax:

Code:
DO SomeProcedure TO SomeVariable

If so, it's news to me. I've never seen that syntax before, and as far as I can see it's not documented (not even in the ever-reliable HackFox).

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hello Mike,

Oops, I'm afraid I set a this into the wild and actually it only works with DO FORM .. TO Variable, not DO function.prg The only things you can do if function.prg exists within the current dir or SET("PROCEDURE") or SET("PATH") is Variable=function() or STORE function() TO Variable, not DO function.prg TO Variable.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top