any idea how it can be done with void in C | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

7th May 2019, 12:15 PM
Robin Singh
Robin Singh - avatar
8 Answers
+ 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^^
7th May 2019, 12:24 PM
Matthias
Matthias - avatar
7th May 2019, 12:33 PM
Robin Singh
Robin Singh - avatar
+ 3
Thanks for the help Swiss James and Matthias
7th May 2019, 4:07 PM
Robin Singh
Robin Singh - avatar
+ 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();
7th May 2019, 12:33 PM
Robin Singh
Robin Singh - avatar
+ 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.
7th May 2019, 3:21 PM
James
James - avatar
+ 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
7th May 2019, 3:51 PM
James
James - avatar
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.😕
7th May 2019, 12:29 PM
Robin Singh
Robin Singh - avatar
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.
7th May 2019, 3:23 PM
Robin Singh
Robin Singh - avatar