Hi,when using user defined fxns,is it neccessary to print required values from main or from fxn definition or are there rules? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hi,when using user defined fxns,is it neccessary to print required values from main or from fxn definition or are there rules?

User defined function

17th Mar 2020, 3:35 AM
Sandesh Khanal
11 Answers
+ 1
Sandesh Khanal Answer to the question is contextual, it depends on some considerations. If you can show me the code, I may be able to help with that. But I can't say much without looking at a code : )
17th Mar 2020, 5:10 AM
Ipang
+ 1
For example if when you have to print two real roots in quad eqn there are two values you must return to calling fxn but that is not possible so the only way is to print from called fxn?
21st Mar 2020, 8:06 AM
Sandesh Khanal
+ 1
I do know how to use those except for returning struct but there were some codes where the values where displayed from both main and called fxn so i guess both are fine to do?
21st Mar 2020, 8:44 AM
Sandesh Khanal
0
Do you know the way to use pointer to pass in function argument? this is one possibility. Another way is to return a struct, or you can create a dynamic array and return the array (if the values are of the same type).
21st Mar 2020, 8:24 AM
Ipang
0
Personally, I prefer to have user defined functions to process the data and return the information to me. And if the user defined function is meant to only print the values, then I will set it as void function, and it shouldn't return anything. But if you find that there's a need to print the values twice (in user defined function & in main), then I guess the choice is yours.
21st Mar 2020, 8:52 AM
Ipang
0
Alright then for returning struct can i create a structure struct root with members r1 and r2 and a fxn struct x(struct n) and just return the the whole struct and not just individual members?
21st Mar 2020, 8:56 AM
Sandesh Khanal
0
You want to pass 'struct root' as function argument and return also 'struct root'? I didn't understand ...
21st Mar 2020, 9:13 AM
Ipang
0
How would you approach the question by using structure?
21st Mar 2020, 9:55 AM
Sandesh Khanal
0
I'm sorry, but I didn't get "approaching the question" ...
21st Mar 2020, 10:00 AM
Ipang
0
I mean how would you solve it ?do you pass struct as arguments or just a fxn with return type and no arguments ?
21st Mar 2020, 10:05 AM
Sandesh Khanal
0
Pass the "raw materials" as arguments to the function. Let the function "bake" it, and return to you the "cake". I think this is your idea?
21st Mar 2020, 10:26 AM
Ipang