Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Calling functions into another function or even main is what my question is about.
One way to call a function is :
callOne(1, 2);
Another way to call a function is :
callTwo = func2(5, 9);
Please explain what is the difference? Is one way better than the other? Which way is most encouraged to use?
It depends on the return value of the function.
If this is void the function does not have a return value and you do the first.
callOne(1, 2);
If it has something else like int, char or std::string you can use the first but also the second.
callTwo = func2(5, 9);
In this case you save the value the function returns in the variable.
If you use the first variant on a function with return value (not void) the result will be ignored.
I would advice you to google for some basic tutorials like this or this for example. You won't be very fast if you let SO teach you basic things.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
Hello I just do not get it further. I would like to pass a variable string to a function and compare it with an array which i filled b4.
The problem is that i dont know how I can pass all values of the candidates array to the function : I can just pass a single string to the function. In my Code that would be the 0.
I would like to compare the candidates with the user inputed name in the function call in line 148. Can anybody tell me how I have to define the function in order to be able to do that? Or is it even possible to do it in this manner?
Thank you really much!
https://godbolt.org/z/Evcjfxn75
Take a look at the AnyOf function from the algorithm library. With it, you can apply the vote function to each array element, not only the first one.
Pseudocode:
if (std::all_of(candidates.begin(), candidates.end(), vote) {
...
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I'm learning SDL right now and I'm trying to use timer callback function.
The function SDL_AddTimer() offer me only one argument to pass through but I want to pass different types of variables into it.
(https://wiki.libsdl.org/SDL_AddTimer)
I think of one solution is that I can declare a structure containing all of my variables but I'm not sure if it is the best way to do so.
Thank you for your help ~~
I think of one solution is that I can declare a structure containing all of my variables but I'm not sure if it is the best way to do so.
Yes this is the correct and best way to pass parameters whenever you are presented with a callback that accepts void*. Then cast back to the struct type from inside the callback.
Same thing goes when using pthreads/Windows threads or other such common APIs.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to pass values from qml to set function written in Qt cpp, the values that will be passed is of different data types, example int or string, I will be writing only one set function in cpp which will take this values and return Qstring or int or double.How can I write a code for this.
C++ does know of two types:
QVariant
QJSValue
which might be the value, that you passed... That depends on you.
Both of them have various methods to test for their content, and to convert to this.
See the linked documentations for this.
You may even store the passed value as the corresponding type, without the need of conversion (up to the time, you need it for calculations in C++) It depends on, what you will do with it, to find the right choice.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Just curious about how the return type of a function will have an effect on the program in C++. For example, the following three functions will do the same stuff but have different return types:
void myfun();
int myfun();
bool myfun();
Suppose void myfun() will be enough for my purpose as there is no need to return a value. However, I may still consider to use int myfun() as with the evolution of the development it is possible that this function need to return a value. So, if I use int myfun() rather than void myfun(), what kind of cost I will expect?
Absolutely bugger all. That's about equivalent to the cost of changing it. Don't give it a return value just because it might someday later need one. Keep it strictly to what you know you need.
a very slight cost indeed to allocate memory to store the return value on the stack..
but for general SW design - refactoring it not a bad word! design and write your code to be simple... the thumb rule is Simple is Better. if you don't need an int return value - don't define it. if you'll need one in the future - refactor your code!
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Suppose that we have class C and our process is as follows
do func1
do func2
do func3
do func4
where each function operates on the data from the previous stage
Which is better from the point of view of system design?
make each func takes an input, returns its results and pass the result to the next stage
make each func operate on data members in the class c and they all return void
And if these 2 strategies are famous design patters what is the name of each design pattern?
If you use option 2, and you'll pass the code to another developer. If he/she accidentally calls func3(), func4() in the middle of the code, it will be logically wrong.
If you use option 1, the developer needs to think of the parameters which will be passed through. It is less likely that he will make mistake.
Therefore, I'll go with option 1.