Basic Recursion- Hints requested

Problem Statement:
I am trying to wrap my head around recursion, and I'm slowly getting a handle on it, but I keep running into brick walls with it. My current issue is with an assignment to write a recursive function to empty a linked list. A wrapper function is employed to call the recursive function. The recursive function takes as argument a single pointer to a node, and returns a bool value. This last part is where I keep stumbling. I can write the function as a void, but when the function call is required to return something that isn't part of what the function is supposed to accomplish, I don't understand how to structure it.

1
2
3
4
5
6
7
8
9
10
11
bool list::delall(node * & node)
{
     if(head == nullptr)
           return false;
     else
     {
          bool cleared = delall(node->next);
          delete node;
          return cleared;
     }
}


This is the most recent version of this function. I've tried rearranging this a bunch of different ways, and if it were permitted to write it as a void function, this wouldn't be a problem. Any hint as to what I'm missing as far as designing this would be greatly appreciated!
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
36
37
38
39
40
41
42
43
44
45
#include <iostream>

struct node
{
    int value = 0 ;
    node* next = nullptr ;
};

void print( const node* first )
{
    if( first != nullptr )
    {
        std::cout << first->value << " => " ;
        print( first->next ) ; // recursively print the remaining nodes
    }

    else std::cout << "nullptr\n" ;
}

// delete all nodes starting with first
// return true if at least one node was deleted
bool delete_all( node* first )
{
    if( first == nullptr ) return false ; // nothing was deleted

    else
    {
        auto tail = first->next ;
        delete first ;
        delete_all(tail) ; // recursively delete the remaining nodes
        return true ; // true because a node was deleted
    }
}

int main()
{
    auto lst = new node { 12, new node { 34, new node { 56, new node { 78, nullptr } } } } ;
    print(lst) ; // 12 => 34 => 56 => 78 => nullptr

    std::cout << std::boolalpha << delete_all(lst) << '\n' ; // true, nodes were deleted
    lst = nullptr ; // all nodes are deleted; nothing is left
    print(lst) ; // nullptr

    std::cout << std::boolalpha << delete_all(lst) << '\n' ; // false, nothing to delete
}

http://coliru.stacked-crooked.com/a/ddf71d3dba039b93
Do I understand correctly from your example that a bool function can be called in the same fashion as a void function? That is, I don't need an assignment such as bool whatever = funcCall(), or to use the function call in an expression?
> I don't need an assignment such as bool whatever = funcCall(), or to use the function call in an expression?

Yes; the value can be discarded.

funcCall(); all by itself, is an expression statement
"Most statements in a typical C++ program are expression statements"
https://en.cppreference.com/w/cpp/language/statements#Expression_statements

The full expression of an expression statement is a discarded-value expression.
https://en.cppreference.com/w/cpp/language/expressions#Discarded-value_expressions

Note: C++17 introduced the attribute [[nodiscard]]
https://en.cppreference.com/w/cpp/language/attributes/nodiscard
from your example that a bool function can be called in the same fashion as a void

This is not specific to bool. Which the above links should explain along the way, but just to be sure you got it since this part of your question would not be stated explicitly. Any type can be thrown away.
Thank you both! I knew I had to be missing something basic with this!
It should also be pointed out that it is pointless for this function to return anything. Normally it would just be void.
Topic archived. No new replies allowed.