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!

How to make a function in a Class

Status
Not open for further replies.

Qwark

Programmer
Sep 26, 2000
59
0
0
NL
Hello,

I would like to make a function in the *.prg file of a class. Now I have a method getname. In the *.prg file I see
PROCEDURE getname, but that must be FUNCTION getname. Because getname returns a value, but when I change the procedure in a function (including FUNCTION and ENDFUNC)
it's not saving. Does anyone now what's the problem.

I get an error when i use:

i = createobject(....
? i.getname

Thanks,

Qwark
 
It does not matters what you use - PROCEDURE or FUNCTION: both allow to return value. So, if your application works with PROCEDURE, you can use it.

Vlad Grynchyshyn
vgryn@softserve.lviv.ua
The professional level of programmer could be determined by level of stupidity of his/her bugs
 
And what is the diference between function and procedure? In other languages such as pascal such things are not allowed!

When using what, what is the difference?
 
Hello,

FIRST:
SET CLASSLIB TO "d:\mon\class\iniaccess"
i = createobject("iniaccess", "parameter1", "parameter2")
i.getmapclass

The error is: variable GETMAPCLASS not found.

My Procudure has the next code:
? this.cmapclass

SECOND:
SET CLASSLIB TO "d:\mon\class\iniaccess"
i = createobject("iniaccess", "parameter1", "parameter2")
? i.getmapclass

The error is: variable GETMAPCLASS not found.

My Procudure has the next code:
RETURN this.cmapclass

In both times i'll get the same error. I think it's my fault, but maybe it's the classdesigners fault. A third question how can I make functions in de classdesigner.

I'll hoop anyone can answer my questions. I'm just a beginner.

Sorry for all that incorrect English.

Thanks,

Qwark
 
I don't know <:). Maybe you will find answer in Hacker's guide for VFP?

Vlad Grynchyshyn
vgryn@softserve.lviv.ua
The professional level of programmer could be determined by level of stupidity of his/her bugs
 
I'm not sure what all the trouble is, but keep in mind that a method cannot have an embedded procedure or function; this is true whether you build the class via the user interface or in code.

The primary difference between a Function and a Procedure:

A Function accepts parameters by value by default; a Procedure accepts parameters by reference by default. Keep in mind this behavior is also affected by other factors (how you pass the arguments; SET UDFPARMS, etc.). Other than that, they are essentially identical.

Robert Bradley

 
a Procedure accepts parameters by reference by default

Robert, is it true!!? I always thought that both procedure and funtion gets parameter by value when SET('UDFPARAMS') = 'VALUE'...

Vlad Grynchyshyn
vgryn@softserve.lviv.ua
The professional level of programmer could be determined by level of stupidity of his/her bugs
 
First, a reply to original question:

You cannot access a method as a property (or the other way round).
Let's give an example:

Create a file TESTIT.PRG, containing the following code:
Code:
DEFINE CLASS MyThingy AS custom
   cName = &quot;Hi there!&quot;

   FUNCTION MyName
      LOCAL lcRetVal

      lcRetVal = &quot;Hello World!&quot;

      RETURN lcRetVal
   ENDFUNC
ENDDEFINE

Running reveals the following:
Code:
SET PROCEDURE TO TESTIT
loTest = CREATEOBJECT(&quot;MyThingy&quot;)
?loTest.cName       && &quot;Hi there!&quot;
?loTest.MyName      && ERROR
?loTest.MyName()    && &quot;Hello World&quot;

Doesn't matter if you use the PROCEDURE or FUNCTION keyword.

Second:
a Procedure accepts parameters by reference by default
Sorry Robert, but I disagree:
Both PROCEDURES and FUNCTIONS take parameters by VALUE as default.
The difference between a PROCEDURE and a FUNCTION in VFP has given me some headaches, untill I finally realized that they are exactly the same. VFP doesn't have a difference between a procedure and a function (Imagine this: a procedure returns .T. by default ?!)

However, as good coding practice:
- Use the PROCEDURE keyword if you don't set a return-value.
- Use the FUNCTION keyword if you have 1 and only 1 return-value.
(and ALWAYS use LPARAMETERS to define the parameter list)
Diederik Vermeeren
verm1864@exact.nl
 
I don't have Hacker's on my new machine, so I can't look it up, and the online docs don't seem to explicitly say one way or the other.

So, until I can find proof, let me restate it a bit differently, which, in the old days, evaluates to the same as my original statement (I'll explain later):

If a routine is called as a program/procedure, such as DO MYPROG WITH X, then X is passed by reference.

If a routines is called as a function, such as MYPROG(X), the X is passed by value.

This is regardless of whether MYPROG is an individual PRG, declared as a FUNCTION, or declared as a PROCEDURE.


This analysis is supported by the online docs.

Now, what did I mean about the old days? In early versions of Fox (and dBase), you could not call a function &quot;naked&quot;, as in MYPROG(X). You had to use it as an expression, as in DUMMY=MYPROG(X), or in later versions, you could use =MYPROG(X).

I'll research this further concerning the declarations of PROCEDURE and FUNCTION.

Robert Bradley

 
I should add: SET UDFPARMS controls whether parameters passed when using the MYPROG(X) syntax are by reference or by value. Sorry for the omission. And, occasionally, for emissions.

Robert Bradley

 
Hmm, it seems that the MSDN (VFP 6.0) gives some confusing information.

Entry: SET UDFPARMS Command
(..) By default, variables are passed to a user-defined function by value. (Variables passed to procedures with DO ... WITH are passed by reference.) (..)

Entry: PROCEDURE
(..) By default, parameters are passed to procedures by value (..)

Entry: FUNCTION
(..) By default, parameters are passed to functions by value (..)

NOTE: varaibles passed to FUNCTIONS with DO ... WITH are also passed by reference!
So the question remains: IS there a difference between a procedure and a function?
Diederik Vermeeren
verm1864@exact.nl
 
So the question remains: IS there a difference between a procedure and a function?

There are no difference in declaration. It looks like a difference in how you call it. If you call it as function - parameters passed by value, if you call it as procedure - parameters passed by reference (by default). If you use SET UDFPARAMS, it seems affects only functions. However, all above does not means that 'do ... with 5*6' will be passed by reference, of course, and will not cause error (this is the most confusing thing in VFP for C++/Delphy programmers, however, if you look from the common sense point of view - all seems clear ;).




Vlad Grynchyshyn
vgryn@softserve.lviv.ua
The professional level of programmer could be determined by level of stupidity of his/her bugs
 
Though not a definitive source, this from Special Edition: Using Visual FoxPro 6 by Bazian, Booth, et al, Chapter 2:

&quot;By default, parameters are passed to a procedure by reference and to a function by value.&quot;

I think that what we've all found and probably agree on is that how you declare it (FUNCTION SHMEDLAP vs PROCEDURE SHMEDLAP) has less to do with the value/reference passing than how you call it and other factors such as SET UDFPARMS.

Not perfectly &quot;no difference&quot;, but probably no difference in practice.


Robert Bradley

 
Qwark,

You need to use more descriptive variable names:

i = createobject(&quot;iniaccess&quot;, &quot;parameter1&quot;, &quot;parameter2&quot;)
i.getmapclass


oIni = createobject(&quot;iniaccess&quot;, &quot;parameter1&quot;, &quot;parameter2&quot;)
oIni.getmapclass()



Jon Hawkins
jonscott8@yahoo.com

The World Is Headed For Mutiny,
When All We Want Is Unity. - Creed
 
Thanks Jon,

That was my problem. Now it works fine.
 
This discussion is interesting.

First, the two keywords PROCEDURE and FUNCTION have only one difference, spelling!

What makes a routine either a procedure or function in VFP is how you call it, NOT how you declare it.

DO Whatever && Procedure

Whatever() && Function

Any routine can be called either way (except for methods which can only be called using the Object.Method() syntax)

Functions get their parameters by value by default (unless SET UDFParms has been used to change the default) Parms can be forced to By Referenece using the @ symbol to preceed the parameter as in Whatever(@MyParm)

Procedures get their parms by reference by default. There is no way to change the default passing of parms to procedures. However, by value can be forced using ()'s to surround the argument as in DO Whatever WITH (MyParm).

The original problem in this thread is related to the DEFINE CLASS syntax which can uses the PROCEDURE keyword to define methods within the class definition.

My advice is to use one of the keywords consistently. It makes searching for things easier because you only need to search for one or the other and not both.

Bottom line ..., there is no difference between the two keywords.

JimB


JimBooth
jbooth@jamesbooth.com
 
Hi!

My advice is to use one of the keywords consistently. It makes searching for things easier because you only need to search for one or the other and not both.

I agree with Jim and want to add that better to use PROCEDURE constantly in all places, just because it is more universal. In my Application no FUNCTION declarations. Once I even started to think that FUNCTION keyword is obsolete ;-)

Vlad Grynchyshyn
vgryn@softserve.lviv.ua
The professional level of programmer could be determined by level of stupidity of his/her bugs
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top