I am student who just learned c++ not long ago. I have a doubt in mind, for the linked list code below, I don't quite understand the logics behind it, why does the function when it reaches return, it will continue to execute the func1() and cout command ? Isn't it whenever a the programs reaches return it will automatically exits the function and ignore the rest of the blocks below ?
#include <iostream>
using namespace std;
// Creating a node
class Node {
public:
string value;
Node* next;
};
void func1(Node* head) {
if (head == NULL)
{
return;
}
cout << " " << head->value;
func1(head->next);
}
int main() {
Node* head;
Node* one = NULL;
Node* two = NULL;
Node* three = NULL;
// allocate 3 nodes in the heap
one = new Node();
two = new Node();
three = new Node();
// Assign value values
one->value = "A";
two->value = "B";
three->value = "C";
// Connect nodes
one->next = two;
two->next = three;
three->next = NULL;
func1(one);
return 0;
}
func1(head->next)is roughly equal tohead = head->next; goto top_of_function;.