Make a square() function to multiply floats | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Make a square() function to multiply floats

Hi guys, i want to modifie the code bellow in order to multiply a floats numbers: But only the definition need be changed without having to make changes to the declaration. #include <stdio.h> void* square (const void* num); int main() { int x, sq_int; x = 6; sq_int = square(&x); printf("%d squared is %d\n", x, sq_int); return 0; } void* square (const void *num) { int result; result = (*(int *)num) * (*(int *)num); return result; }

22nd Apr 2020, 5:46 PM
Abdelkarim MESKAOUI
Abdelkarim MESKAOUI - avatar
2 Answers
+ 2
You can't return something when the return type of a function is void . Run this code for float values #include <stdio.h> float square (float *num); int main() { float x, sq_int; x = 6.5; printf("%f squared is :" , x); sq_int = square(&x); printf("%f\n",sq_int); return 0; } float square (float *num) { float result; *num = (*num) * (*num); result=(*num); return(result); } Vote me if its helpful
23rd Apr 2020, 6:40 AM
Param Dudhe
Param Dudhe - avatar
0
Thanks for feedback, but I don't agree with you, Because the function pointer (void *) provides the possibility to return something.... Right!
23rd Apr 2020, 8:47 PM
Abdelkarim MESKAOUI
Abdelkarim MESKAOUI - avatar