Question about return of a function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Question about return of a function

I have wrote a function that asks first name and last name and stores them separately in 2 variable namely firstName and lastName. How i can use this two string variables in the body of program when i call the function?

18th Jun 2016, 10:37 PM
Yousef Sayadi
Yousef Sayadi - avatar
2 Answers
+ 1
You need to use struct or class. Struct has two string and this function return struct.
18th Jun 2016, 11:10 PM
Emre Özdil
+ 1
Hi Yousef, there's a more standard way of doing this that does not include writing a custom struct or class. There is a class called "pair" in the standard library header "utility". It provides two places to store values per pair. You can use that. Also, if you need to return more parameters, you can do this by returning a container class like "list" or "vector" (in case they are of the same nature) or by the class "tuple" (part of C++11 standard library header "tuple" and the commonly used Boost library in the header "boost/tuple.hpp"). The container classes and the tuple class let you return a virtually arbitrary number of arguments. (Please be aware that this might entail a significant slowdown in your code if the number of elements in either of these containers is high; for a discussion of options to solve this for say more than 100000 elements pls post again :-) )
19th Jun 2016, 12:31 AM
Stefan
Stefan - avatar