Using only the values returned by functions -- elementary question

P

pauldepstein

Suppose I have a function f from integers to integers. Besides
mapping x to f(x) the function does lots of other things as well -- for
example f displays a message on screen etc.

I then want to set y = f(5).

However, all I want to do is to replace y by the value returned by
f(5). I don't want another message on screen and whatever else f does.

How is this problem solved? What do I do if I am only interested in
the value returned by a function and not in all the other stuff that
the function does?

Thank you,

Paul Epstein
 
G

GB

Suppose I have a function f from integers to integers. Besides
mapping x to f(x) the function does lots of other things as well -- for
example f displays a message on screen etc.

I then want to set y = f(5).

However, all I want to do is to replace y by the value returned by
f(5). I don't want another message on screen and whatever else f does.

How is this problem solved? What do I do if I am only interested in
the value returned by a function and not in all the other stuff that
the function does?

Thank you,

Paul Epstein

You need to split the function up into smaller pieces and then call the
pieces in the ways you want (e.g., with and without display). For example:

int f(int x)
{
std::cout << "Input is " << x << std::endl;
int retval = x*x;
std::cout << "Returning " << retval << std::endl;
return retval;
}

becomes

int square(int x)
{
return x*x;
}

int f(x)
{
std::cout << "Input is " << x << std::endl;
int retval = square(x);
std::cout << "Returning " << retval << std::endl;
return retval;
}

Depending on your needs, you could also pass in a couple of function
pointers that provide additional actions to perform at certain points:

typedef void (*func)(int x);

int square(int x, func before, func after)
{
if (before)
(*before)(x);

int retval = x*x;

if (after)
(*after)(retval);
}

Gregg
 
T

Thomas Tutone

(e-mail address removed) wrote:
Suppose I have a function f from integers to integers.

That doesn't make sense, but I assume you mean a function with
signature like the following:

int func(int);
Besides
mapping x to f(x) the function does lots of other things as well -- for
example f displays a message on screen etc.

Yes, those are called side effects.
I then want to set y = f(5).

So what's stopping you?
However, all I want to do is to replace y by the value returned by
f(5). I don't want another message on screen and whatever else f does.
How is this problem solved? What do I do if I am only interested in
the value returned by a function and not in all the other stuff that
the function does?

<SIGH>

You're kidding, right?

You have three choices:

(1) Live with the side effects;

(2) Rewrite the function to eliminate the side effects; or

(3) Find another function that returns the same result but does not
have the side effects.

By the way, what was your C++ question? If you actually have one,
please follow FAQ 5.8, including posting some short, compilable code
that reproduces the problem you're having.

Best regards,

Tom
 
R

Rennie deGraaf

Suppose I have a function f from integers to integers. Besides
mapping x to f(x) the function does lots of other things as well -- for
example f displays a message on screen etc.

I then want to set y = f(5).

However, all I want to do is to replace y by the value returned by
f(5). I don't want another message on screen and whatever else f does.

How is this problem solved? What do I do if I am only interested in
the value returned by a function and not in all the other stuff that
the function does?

Thank you,

Paul Epstein

In other words, you have a function with side effects, and you want to
get rid of the side effects. I'm afraid that the only way to do this in
general is to re-write the function without the side effects. I don't
know of /any/ languages where you can do what you want.

For certain types of side effects, you could write a wrapper function to
undo some side effects. For instance, if the function wrote to a global
variable, then your wrapper could store the variable's original value,
and restore it after calling the function. If the function writes to a
stream, then the wrapper could close the stream and re-open it to
/dev/null or some other safe device, call the function, and then close
and re-open the file to where it had been before. But there aren't ways
to undo all side effects that your function could have, and it would
take a lot of work to undo what can be undone.

If possible, just re-write or re-compile your function. If not
possible, well, good luck.

Rennie deGraaf
 
G

GB

GB said:
Depending on your needs, you could also pass in a couple of function
pointers that provide additional actions to perform at certain points:

typedef void (*func)(int x);

int square(int x, func before, func after)
{
if (before)
(*before)(x);

int retval = x*x;

if (after)
(*after)(retval);
}

I should also mention that, if your function f() is a class member
function, you would ordinarily accomplish the above more directly by
using virtual functions:

class Base {
public:
virtual int f(int x) {
return x*x;
};

class WithPrinting : public Base {
public:
virtual int f(int x) {
std::cout << "Input is " << x << std::endl;
int retval = Base::f(x);
std::cout << "Returning " << retval << std::endl;
return retval;
}
};

Gregg
 
J

Jim Langston

Suppose I have a function f from integers to integers. Besides
mapping x to f(x) the function does lots of other things as well -- for
example f displays a message on screen etc.

I then want to set y = f(5).

However, all I want to do is to replace y by the value returned by
f(5). I don't want another message on screen and whatever else f does.

How is this problem solved? What do I do if I am only interested in
the value returned by a function and not in all the other stuff that
the function does?

Thank you,

Paul Epstein

Another way in addition to the suggestions given is to pass another variable
to tell if you want to do the side effects or not.

int f(int SomeNum, bool SideEffects = true)
{
int SomeNumOut = SomeNum * 1234;
if ( SideEffects )
{
std::cout "In f\n";
std::cout << "Num in: " << SomeNum << " Num Out:" << SomeNumOut <<
std::endl;
}
return SomeNumOut;
}

I'm not positive if the = true will work for stand alone functions in C++,
it might only work for methods. If it doesn't work remove it. If it does
work then you dont' have to speicify it if you want the side effects.

int main()
{

int y = f(123); // Side Effects
y = f(234); // Side Effects
y = f(456, false); // No Side Effects
}
 
M

Marcus Kwok

Thomas Tutone said:
(e-mail address removed) wrote:
Suppose I have a function f from integers to integers.

That doesn't make sense, but I assume you mean a function with
signature like the following:

int func(int);

It does make sense if you are used to mathematical terminology. The
function f maps from the set of integers to the set of integers:

f:Z -> Z

where Z represents the set of integers representable by the C++ "int"
type.

Your interpretation is correct.
 

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

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top