
Sponsored
Sponsored
This approach uses the concept of closures to return a function that ignores its arguments and always returns the string 'Hello World'. In this method, createHelloWorld returns a new function that consistently returns the string when invoked.
Time Complexity: O(1) as the function performs a constant-time operation.
Space Complexity: O(1) since it doesn't use any extra space dependent on input size.
1
In C, we use a function createHelloWorld that simply returns the string "Hello World". We then define a function pointer type hello_function to point to such functions and return it through another function getHelloWorldFunction.
This approach directly returns a function without using intermediate variables like closures. The returned function will always output 'Hello World', ignoring any provided arguments.
Time Complexity: O(1). The function executes a single return instruction.
Space Complexity: O(1) as there are no dynamic allocations.
1#include <functional>
2#include <string>
3
4std::string helloWorldFunction() {
5 return "Hello World";
6}
7
8std::function<std::string()> createHelloWorld() {
9 return helloWorldFunction;
10}In C++, a direct function helloWorldFunction is created to return "Hello World" and returned as a std::function in createHelloWorld.