Topic: Assignment to function name -- any mea
Author: dacut@henry.ece.cmu.edu (David A. Cuthbert)
Date: 1997/04/21 Raw View
Paul D. DeRocco <strip_these_words_pderocco@ix.netcom.com> wrote:
>Steve Clamage wrote:
>> If myFunction had a return type of "int (*)()" the statement
>> "return myFunction;" would be valid and would return a pointer
>> to the function itself. Since myFunction has a return type of int and
>> there is no implicit conversion from pointer-to-function to int, that
>> statement in this particular case is invalid due to type mismatch.
>True. You couldn't extend it to allow the function name to represent a
>general-purpose variable within the function that could be read and
>written. But if it was a write-only pseudo-variable that only appeared
>on the left side of an assignment, it could be added to the existing
>grammar without breaking any existing programs.
Furthermore, the return statement can be left empty, e.g.:
int myFunc(int someParam) {
myFunc = someParam + 1;
return; // implicitly returns myFunc
}
Both of those lines are currently illegal in C++ ...
>Personally, I wouldn't
>much care for this change, but Dave Cuthbert was just asking if it was
>possible.
Right; I didn't like it to begin with, and I like it even less the
more I think about it. Just verifying my intuition here.
>> Perhaps you are thinking of Pascal. Pascal has no "return" statement.
>Yeah, that was it.
You were right about BASIC, as well (at least MS Visual Basic and
QBasic); they handle return values by assignment to the function
name (a syntax which, again, I don't like).
--
David A. Cuthbert (henry.ece.cmu.edu!dacut)
Graduate Student, Electrical and Computer Engineering
Data Storage Systems Center, Carnegie Mellon University
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/04/22 Raw View
David A Cuthbert writes:
> Paul D. DeRocco <strip_these_words_pderocco@ix.netcom.com> wrote:
[about allowing the function name to be used as a variable inside the
function body]
>> Personally, I wouldn't
>> much care for this change, but Dave Cuthbert was just asking if it was
>> possible.
> Right; I didn't like it to begin with, and I like it even less the
> more I think about it. Just verifying my intuition here.
There some more difficulties involved. If the return type is an class
type without a trivial constructor, when will the return object be
constructed? Will it be default constructed, or will the first
assignment invoke a copy-constructor? What if several assignments are
done? What if there's no default constructor, or if it's not visible?
g++ has actually implemented something like this (named return
values), but the return value is declared and initialized in the
beginning of the function. There can be empty returns that return
that value, or non-empty returns with a different value:
Foo bar() return r(arg1, arg2); {
if (something)
return Foo(2,3);
r += 3;
if (somethingelse)
return; // returns r
r.donothing();
// returns r
}
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Stephen.Clamage@Eng.Sun.COM (Steve Clamage)
Date: 1997/04/18 Raw View
In article EEF@ix.netcom.com, "Paul D. DeRocco" <strip_these_words_pderocco@ix.netcom.com> writes:
>David A. Cuthbert wrote:
>>
>> However, I was wondering if assignment to an object using the name of
>> the function has any meaning currently. Specifically, something like:
>>
>> int myFunction(void) throw() {
>> myFunction = 8; // note lack of declaration.
>> return myFunction;
>> }
>>
>> Does this have any meaning in C++?
>
>No, it doesn't.
Well, it isn't something to which we could provide a meaning without
affecting existing valid programs. The assignment statement is
explicitly invalid under existing rules.
The name of a function is a constant representing a pointer to that
function. Assigning to a constant is never valid, and in this
case requires a diagnostic.
If myFunction had a return type of "int (*)()" the statement
"return myFunction;" would be valid and would return a pointer
to the function itself. Since myFunction has a return type of int and
there is no implicit conversion from pointer-to-function to int, that
statement in this particular case is invalid due to type mismatch.
> I recall some other language in which the function name
>was implicitly a variable, and you'd return a value by setting that
>variable and then returning.
Perhaps you are thinking of Pascal. Pascal has no "return" statement.
The name of the value returned from a function is the same as the
function name. Its type is the type declared for the return value.
You return from a procedure or function by falling off the end. Prior
to returning from a function, you assign something to the return
variable. The assignment can take place any time before returning,
and if you return a structure ("record"), you can do the assignments
piecemeal. Example:
function ident(i: integer) : integer;
begin
ident := i; (* the value to be returned *)
end; (* fall off the end to return *)
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Paul D. DeRocco" <strip_these_words_pderocco@ix.netcom.com>
Date: 1997/04/21 Raw View
Steve Clamage wrote:
> Well, it isn't something to which we could provide a meaning without
> affecting existing valid programs. The assignment statement is
> explicitly invalid under existing rules.
But that's exactly why it _could_ be done without affecting existing
valid programs. No valid program contains within the definition of
function f() the statement "f = ...;" (assuming, obviously, that f isn't
redefined in an inner scope).
> If myFunction had a return type of "int (*)()" the statement
> "return myFunction;" would be valid and would return a pointer
> to the function itself. Since myFunction has a return type of int and
> there is no implicit conversion from pointer-to-function to int, that
> statement in this particular case is invalid due to type mismatch.
True. You couldn't extend it to allow the function name to represent a
general-purpose variable within the function that could be read and
written. But if it was a write-only pseudo-variable that only appeared
on the left side of an assignment, it could be added to the existing
grammar without breaking any existing programs. Personally, I wouldn't
much care for this change, but Dave Cuthbert was just asking if it was
possible.
> Perhaps you are thinking of Pascal. Pascal has no "return" statement.
Yeah, that was it.
--
Ciao,
Paul D. DeRocco
(Please send e-mail to mail:pderocco@ix.netcom.com instead of the
return address, which has been altered to foil junk mail senders.)
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]