returning nothing

Is it possible to return nothing from a function which is defined as int?
What I try to do is:

I have a stack class,if you peek it checks for the position of last item and if its ok function will send[peek] you a value, otherwise it will say that "stack is empty!" and return nothing.In the example code, the coder uses exit(-1) to exit the program in case of error,returning nothing but completely terminates the program.What do you suggest instead of exiting the program?

1
2
3
4
5
6
7
8
9
10
11
12
13
class Stack
{
private:
     .........
public:
.............
int peek()
{
   if(top<0)
       { cout<<"|ERROR| Stack is empty!\n"; exit(-1);}    
    else return st[top];        
}
};
Throw an exception.

Well, first of all, your class shouldn't do any output all on it's own. Just imagine you using that class in a windows program- do you want to rewrite the class everytime you change the environment (sure, this is probably just a practice for you, but don't start doing this in the first place so you won't fall into bad habits).

No, not really. What you could do would be either throwing an exception, or set a bool variable of the stack (valid) to false, and then you could provide a function isValid that returns the value of valid. That way your program won't explode when the user enters a false value, but you'll also hand all of the error handling responsibility to the user. Some people will probably hate you for doing that, but it's better than just randomly crashing the program. Still, throwing an exception is better.
Just want to ask because it's bothering me, but did the origional author have those elipses in? Or did you skip writing the destructor and\or atexit(...) commands? Those items matter with a question like the one you posted.
@hanst99; Thanks for the answer,yes this is just a practice,Im studying inheritance topic in the book that I follow.Since I didnt cover exception handling yet,I leave this example until later.

@Computergeek01; I didnt include base class, it has no-arg constructor,doesnt have destructor and no atexit.That was just an example for inheritance,a base class and derived one,with derived one checking the bounds and wrapping the base class.I posted the code partly because I wanted to find a way to handle the error instead of just exiting with exit(-1).

Also thanks to jsmith for quick answer.
There are other options depending on the application. Realizing that this is just a practice example, I recognize that it is hard to make design decisions without real requirements.

The easiest option is to return a "special" value to mean empty. For instance, if it only positive numbers make sense to be stored in the stack, you could return -1 if the stack is empty. This technique won't work, obviously, if all values within a domain are meaningful.

An option I have used in situations like this is to change the function prototype to:

bool peek(int& value)

The return value indicates whether the peek was successful. The "peeked" value is returned in the reference argument.
A functional possibility is the Maybe monad.

The problem with this issue is that you most likely need a temporary in order to both test and use the result. (If you are really smart you can avoid this, but at that point you are getting too complex where simpler ways of doing things, like doug4 and jsmith suggested, would do.)

Here's a simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;

template <typename T>
struct Maybe
  {
  bool Nothing;
  T    Value;

  Maybe(): Nothing ( true ), Value( T() ) { }
  Maybe( const T& value ): Nothing( false ), Value( value ) { }
  };

template <typename T>
ostream& operator << ( ostream& outs, const Maybe <T> & m )
  {
  if (m.Nothing) outs << "";
  else outs << m.Value;
  return outs;
  }

Maybe <float> divby( float n, float d )
  {
  if (d != 0.0) return Maybe <float> ( n / d );
  return Maybe <float> ();
  }

int main()
  {
  cout << "4 / 2 = " << divby( 4, 2 ) << endl;
  cout << "4 / 0 = " << divby( 4, 0 ) << endl;
  cout << "0 / 2 = " << divby( 0, 2 ) << endl;

  return 0;
  }

:-]

[edit] Went home and compiled code ... then fixed it
Last edited on
Topic archived. No new replies allowed.