Program 1:
#include <iostream>
std::string Hello(void){return "Hello";}
std::string World(void){return "world!";}
int main(){
std::cout << Hello() << " " << World() << std::endl;
}
The functions Hello() and World() exist in the global namespace.
This means they can be called by any other function that comes after their declaration (or in this case, I provided the definition and did not need the declaration first).
I have a problem with this, because as my project gets bigger, I include more header files that fill the global namespace with lots of functions, and I risk having function signature collisions, or worse, accidentally calling the wrong function that should only be called as a sub-task of another function.
I am trying to follow the paradaim of functionally decomposing a task into sub-tasks, and thus it would not make sense for particular functions to ever be called outside the scope of another particular function.
Here is a work-around, and apart from the code becoming unreadable due to the indentation depth, I want to know if there are any performance or implementation gotchas. lambda functions are a bit of magic for me at the moment, so I'm curious about the unforeseen dangers.
Program 2:
#include <iostream>
int main(){
auto Hello = [](void) -> std::string{return "Hello";};
auto World = [](void) -> std::string{return "world!";};
std::cout << Hello() << " " << World() << std::endl;
}
Hello() and World() are encapsulated inside main() and can not be called from outside main()'s scope.
Is that all that's different?