Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What's with these returning values in User Defined Functions in C?

PROGRAM TO PRINT REVERSE OF THE USER INPUT INTEGER IN C

#include<stdio.h>

int reverse(int x)
{
    
int lastdigit;

while(x>0)
{
    lastdigit=x%10;
    printf("%d",lastdigit);
    x=x/10;
}

return lastdigit;
}

int main()
{
    int num,fn;
    printf("Enter any Number: ");
    scanf("%d",&num);
    fn=reverse(num);
    return 0;
}

Here in this code at the function definition part, when I run with returning value setting as lastdigit. I get correct output. when i replace it by x i get correct output.when i replace it by return 0; i get correct output. why is it so ?What actual role does return value play here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your program does not use the function return value to compute or print anything, so it does not matter what value the function returns.

Your function reports its results by using printf to print digits. So the results go to the user that way, not through the function return value. You could change your program so the function return type were void, meaning the function does not return a value, and the program would still work.

In other programs, a function may return a value to its caller, and that caller may use the value in an expression or further computations. Then the value returned by the function matters.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading