Need recursion help - void function

Question:
My code will not call itself, I am wondering why?
*reverse at the bottom says “reverse function doesn’t exist” while it’s right there
**assume “using namespace std;” and “#include <vector>” are above

Edit: clarity, title fixed
Repl link:
https://replit.com/@QibliTheSandwing/Recursion-2#reverse.cpp

void reverse(vector<char> s){

  if(s.size() == 0){
    
  }
  else{
    cout << s[s.size()-1];
    reverse(s.pop_back());
  }
}

Your error is that pop_back does not return a value but alters the vector.
So your should write

void reverse(vector<char> s){

  if(s.size() != 0){
    cout << s[s.size()-1];
    s.pop_back();
    reverse(s);
  }
}
4 Likes

Thank you, kind stranger, I will keep this in mind for future use

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.