Home > Software design >  Can't get a function to return a value [Solved]
Can't get a function to return a value [Solved]

Time:11-23

I need to get the largest and the smallest number out of three using two functions. I assigned the inputted numbers to the function parameters and wrote the functions but I don't seem to be getting the value returned from the function. Code compiles and prints correctly but instead of the smallest and largest number, I am getting a bunch of zeroes.

Edit: When assigning the value to a variable, the variable needs to be written first. For example first = number is not the same as number = first.

Also, my way of checking which number is the largest/smallest is not done correctly here :D

#include <stdio.h>

int smallest(int first, int second, int third);
int largest(int first, int second, int third);

int main()
{
    int first_number, second_number, third_number, largest_number, smallest_number;
    
    printf("Enter the 1. number:");
    scanf("%d", &first_number);
    printf("Enter the 2. number:");
    scanf("%d", &second_number);
    printf("Enter the 3. number:");
    scanf("%d", &third_number);
    
    largest_number = largest(first_number, second_number, third_number);
    smallest_number = smallest(first_number, second_number, third_number);
    
    printf("Among the numbers you entered,\nthe largest was %d and the smallest was %d.", largest_number, smallest_number);
    
    return 0;
}

int largest(int first, int second, int third)
{
    int number;
    
    if (first>second && second>third)
        first = number;
    else if (second>third && third>first)
        second = number;
    else
        third = number;
    return number;
}

int smallest(int first, int second, int third)
{
    int number;
    
    if (first<second && second<third) 
        first = number; 
    else if (second<third && third<first) 
        second = number; 
    else 
        third = number; 
    
    return number;
}

CodePudding user response:

You give the value of the first to number and you test with the following ones to give it their values if any.

int largest(int first, int second, int third)
{
    int number = first;
    if (second>number) number=second;
    if (third>number) number=third;

    return number;
}

int smallest(int first, int second, int third)
{
    int number = first;
    if (second<number) number=second;
    if (third<number) number=third;

    return number;
}

CodePudding user response:

int largest(int first, int second, int third)
{
    int number;

    if (first>second && second>third)
        first = number;
    else if (second>third && third>first)
        second = number;
    else
        third = number;
    return number;
}

The problem is here: first = number assigns the value of number to the variable first. So when you return the value of number, you return the value of an uninitialized variable.

You should replace something = number by number = something in your largest() and smallest() functions.

By the way, your conditions won't work, even with the fix I suggested:

if (first>second && second>third)
        number = first;

Here, if first == 8, second == 4 and third == 6, this condition will fail to assign the value of first to number, but I'll let you figure out why on your own.

  • Related