How can I get this value from function stored properly?

You are using Pass by value method. What ever the changes takes places in the function it won’t affect in the main program. Those changes are limited with in the function.

You need use the following method to do this!

int num5(int *number)
{
    *number = 5;
    return 0;
}

int main()
{
    int number;
    number = num5(&number); // pass the address of the number instead value
    printf("%d", number);
}

When you Pass the address of the variable to the function, whatever the changes you are doing in the function, that is retained by that variable. So you will get the latest assigned value!

Leave a Comment