Search code examples
cfunctionpointersfunction-pointers

function parameter difference


Here are two same codes but with different function parameters, but they seem to be outputting same things.

void get_rate_drop_factor(int *rate_ml, int *drop, int *drop_per_min)
{
    printf("Enter rate in ml/hr=> ");
    scanf("%d", rate_ml);
    printf("Enter tubin's drop factor(drop/ml)=> ");
    scanf("%d",drop);
    *drop_per_min = (*rate_ml / 60) * *drop;
    printf("The drop rate per minute is: %d\n\n", *drop_per_min);

}

from what I learned, those variables with '*' in the parameters are output parameters.

void get_rate_drop_factor(int *drop_per_min)
    {
        int rate_ml, drop;
        printf("Enter rate in ml/hr=> ");
        scanf("%d", &rate_ml);
        printf("Enter tubin's drop factor(drop/ml)=> ");
        scanf("%d",&drop);
        *drop_per_min = (rate_ml / 60) * drop;
        printf("The drop rate per minute is: %d\n\n", *drop_per_min);

    }

when I call the function, those two codes seem to be outputting the same thing. So what exactly is the '*' doing for the parameters? I don't think I'm completely understanding the concept of parameters.

Please explain nicely because I really want to learn this and It's been really frustrating.


Solution

  • from what I learned, those variables with '*' in the parameters are output parameters.

    You correctly pointed out that such parameters are output parameters.

    So in the first function all three parameters are output parameters.

    In the second function there is only one output parameter. So the caller of the function can not get values of rate_ml and drop entered by the user in the function.

    So if the caller of the function needs to get all three values he should use the first function declaration. If he needs only the value of the result calculation he should use the second function declaration.