0
any idea how it can be done with void in C
I have created a reverse the number program I think people do it with arrays but currently I have not reached array chapters of my book. anybody know how this program can be created using "function with argument but no return type". I mean void function. because when I initialize int k = reverse(n) in void case the compiler puts error and there is also no way to print a void function using printf("",reverse(n)); because i think its not possible because it has no return type. https://code.sololearn.com/ccjTUFQt7sqR/?ref=app
11 Respuestas
+ 3
Using void functions, one usually does call-by-reference, i.e. manipulate the data in memory directly.
This way, you don't need ro return anything, since the number already changed in its memory place.
Does this help?
Otherwise I can get a bit more into detail^^
+ 3
https://code.sololearn.com/cBWvTOR5Y1pC/?ref=app
new version
+ 3
Thanks for the help Swiss James and Matthias
+ 1
~ swim ~ Thanks Swim you made my doubt clear yeah its true that void function can not be used as an operand like k = reverse();
+ 1
You can make the function an integer. To reverse the integer input, use % with powers of 10 that increase, and multiply by powers of 10 in the opposite order and sum together to construct the new numbers.
I'll attempt it now.
+ 1
I started working on it but didn't have time to finishing solving the maths logic. Maybe it can start you in the right direction, though.
Basically you want to isolate each digit using % and - , then you want to multiply it by the power of 10 for its new position and add it to the output.
https://code.sololearn.com/csnw17DvuHHI/?ref=app
0
Matthias I didn't understand call by reference and memory I think its from pointers chapter. I am on 6th chapter of my book which is functions. The upcomming chapters are Recusion,Arrays,Pointers,Strings,Union etc. So I think i am unable to understand what you said.😕
0
James I took a 30 minutes to figure out the solution. I am not good in maths thats why I was unable to figue out your method. I just don't know how to improve this basic math.