return-value
How do I return multiple values from a function in C?
I don’t know what your string is, but I’m going to assume that it manages its own memory. You have two solutions: 1: Return a struct which contains all the types you need. struct Tuple { int a; string b; }; struct Tuple getPair() { Tuple r = { 1, getString() }; return r; } … Read more
How to return a result from a VBA function
For non-object return types, you have to assign the value to the name of your function, like this: Public Function test() As Integer test = 1 End Function Example usage: Dim i As Integer i = test() If the function returns an Object type, then you must use the Set keyword like this: Public Function … Read more
How can I return two values from a function in Python?
You cannot return two values, but you can return a tuple or a list and unpack it after the call: def select_choice(): … return i, card # or [i, card] my_i, my_card = select_choice() On line return i, card i, card means creating a tuple. You can also use parenthesis like return (i, card), but … Read more
How do I make the method return type generic?
You could define callFriend this way: public <T extends Animal> T callFriend(String name, Class<T> type) { return type.cast(friends.get(name)); } Then call it as such: jerry.callFriend(“spike”, Dog.class).bark(); jerry.callFriend(“quacker”, Duck.class).quack(); This code has the benefit of not generating any compiler warnings. Of course this is really just an updated version of casting from the pre-generic days and … Read more
Best way to return multiple values from a function? [closed]
Named tuples were added in 2.6 for this purpose. Also see os.stat for a similar builtin example. >>> import collections >>> Point = collections.namedtuple(‘Point’, [‘x’, ‘y’]) >>> p = Point(1, y=2) >>> p.x, p.y 1 2 >>> p[0], p[1] 1 2 In recent versions of Python 3 (3.6+, I think), the new typing library got … Read more
How to get the return value from a thread in Python?
One way I’ve seen is to pass a mutable object, such as a list or a dictionary, to the thread’s constructor, along with a an index or other identifier of some sort. The thread can then store its results in its dedicated slot in that object. For example: def foo(bar, result, index): print ‘hello {0}’.format(bar) … Read more
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
It actually is possible to do what you want, but you have to use the functional form of the indexing operator. When you perform an indexing operation using (), you are actually making a call to the subsref function. So, even though you can’t do this: value = magic(5)(3, 3); You can do this: value … Read more
How do I execute a command and get the output of the command within C++ using POSIX?
#include <cstdio> #include <iostream> #include <memory> #include <stdexcept> #include <string> #include <array> std::string exec(const char* cmd) { std::array<char, 128> buffer; std::string result; std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, “r”), pclose); if (!pipe) { throw std::runtime_error(“popen() failed!”); } while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { result += buffer.data(); } return result; } Pre-C++11 version: #include <iostream> #include <stdexcept> #include … Read more
What should main() return in C and C++?
The return value for main indicates how the program exited. Normal exit is represented by a 0 return value from main. Abnormal exit is signaled by a non-zero return, but there is no standard for how non-zero codes are interpreted. As noted by others, void main() is prohibited by the C++ standard and should not … Read more