Passing Stack to Function

17,406

Solution 1

This should suffice:

void displayStack(const Stack<char>& stack);

Solution 2

The name DisplayStack indicates that the function only displays the stack, not changing it in any way. So then the argument can be a reference to const. However, the suffix Stack in the name is redundant since it is implied by the argument, so I’d do it like this:

#include <iostream>
using namespace std;
#include "stack.h"

typedef Stack< char > CharStack;

void display( CharStack const& stack )
{
    // ... Display the stack
}

int main()
{
    CharStack stack;

    for( auto const ch : { 'a', 'b', 'c' } )
    {
        stack.push( ch );
    }
    display( stack );
}

Note that …

  • The function has been moved above main. No silly pure declaration required then, less work. DRY: Don't Repeat Yourself.

  • Incorrect semicolons after the function definitions, have been removed. Well, at least I think they’re incorrect. Whether they are or not, they’re totally superfluous.

  • Superfluous return 0; in main has been removed, because that is the default. However, some programmers prefer to have it explicit.

On the downside, while the C++11 loop compiles nicely with g++ 4.7.2, it causes an Internal Compiler Error (ICE) with Visual C++ 11.0:

[d:\dev\test]
> cl foo.cpp
foo.cpp
foo.cpp(7) : warning C4100: 'stack' : unreferenced formal parameter
foo.cpp(16) : error C2059: syntax error : '{'
foo.cpp(16) : error C2143: syntax error : missing ';' before '}'
c1xx : fatal error C1063: INTERNAL COMPILER ERROR
         Please choose the Technical Support command on the Visual C++
         Help menu, or open the Technical Support help file for more information

[d:\dev\test]
> _

Oh well.

Do that your way. ;-)

Compiler bug reported to Microsoft.

Solution 3

If you do not want to modify contents of the stack inside the function:

void displayStack(const Stack<char> &starRef)

If you want to modify the contents of the stack inside the function:

void displayStack(Stack<char> &starRef)

Points to note:

  • The type of the variable being passed must be the type you mention in function prototype.
  • In C/C++, by default all arguments to function are passed by copy, i.e: A copy of the argument rather than the argument itself is passed to the function. The overhead is the copy. You pass by reference to avoid overhead of a copy of variable being passed.
  • You use const qualifier on the argument if you want the passed variable to be immutable in the function.
Share:
17,406
Howdy_McGee
Author by

Howdy_McGee

I'm a Web Developer who is constantly learning the trade. I've built and manage 100+ WordPress websites with the help of the talented Web Designers around me. I'm currently looking for ways I can help the WordPress community grow and maintain its awesomeness! ~ Alex

Updated on June 11, 2022

Comments

  • Howdy_McGee
    Howdy_McGee almost 2 years

    So I'm playing around with stacks and I've filled one in my main function, but now I want to pass it to my other functions so I can traverse through it. I'm not sure what kind of data type to put into the prototype though so that it accepts it. Suggestions? Here's what I have:

    Main.cpp

    #include <iostream>
    using namespace std;
    #include "stack.h"
    
    void displayStack(char &stackRef);
    
    int main()
    {
        Stack<char> stack;
        stack.push('a');
        stack.push('b');
        stack.push('c');
    
        return 0;
    };
    
    void displayStack(char starRef)
    {
        // Cannot Get here - Errors!
    };
    

    It's telling me I have too many arguments and it doesn't match argument list.

    • jogojapan
      jogojapan over 11 years
      Since the object you pass to the function is of type Stack<char>, it is most natural for the function to be declared as void displayStack(Stack<char> &stackref) (possibly add const). Also, the homework tag is deprecated, see meta.stackexchange.com/questions/147100/…
  • jogojapan
    jogojapan over 11 years
    The type of the variable being passed must be the type you mention in function prototype. -- Must it?
  • Alok Save
    Alok Save over 11 years
    @jogojapan: Usually When you write a function prorotype Yes. You might also have implicit casting i.e: One conversion sequence the standard provides for overload resolution or Upcasting which will also work without exact type matching but the discussion here is about writing a function prortype and not the possibilities in general.