Mehedi Hasan Shuvo
Mehedi Hasan Shuvo

Reputation: 21

How to use functions return in another function in C?

This function i have returns the billType and I want to use this billType in my main function.

int bill()
{
    int billType;
    printf("Enter bill type (1=utilities, 2=internet) : ");
    scanf("%d",&billType);
    return billType;
}

here main function:

int main()
{
    int discount;

    bill();

    if(billType == 1)
    {
        int choice;
        printf("\nEnter 1=TNB or 2=SYABAS: ");
        scanf("%d", &choice);

        if(choice == 1)
        {
            discount= 0.05;
        }

        else if(choice == 2)
        {
            discount= 0.10;
        }
    }

    if(billType == 2)
    {
        int choice;
        printf("\nEnter 1=DIGI, 2=UNIFI or 3=MAXIS FIBRE : ");
        scanf("%d", &choice);

        discount = 0.05;
    }
}

Upvotes: 0

Views: 34

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

You will want to assign the return value to a variable for later use.

You should change

bill();

to

int billType = bill();

Also it looks weird to assign small values such as 0.10 and 0.05 to int discount; because they will be truncated to zero. Consider using another type (for example, double) or remove the meaningless lines (because discount is not read).

Upvotes: 1

Related Questions