1

I am new to programing and I am trying to learn about functions, so I tried some questions with it to get a better understanding of it.

I am still unsure so right now I got stuck and i don't know how to continue.

I tried to make a first function in the main that has only the answers and that function calls a second function and the second one is calling to a third function

but I dont know how to fix this error i tried changing names but I am not sure what to do and even if i am doing is right the error I get is related to "void Ex1()" so what can I change?

        int main()
    {
        Ex1();
    }
    void Ex1()
    {
        int num1, num2, num3,min;
        printf("enter three diff numbers  \n");
        scanf("%d %d %d", &num1, &num2, &num3);
        printf("%d", function1(min));
    }

    int function1(int sum)
    {
        int num1, num2, num3, min, max;
        if (num1 > num2)
        {
            max = num1;
            min = num2;
        }
        else
        {
            max = num2; min = num1; 
        }
        if (num3 > max)
        {
            min = max;
            max = num3;
        }
        if (num3 > min&& num3 < max)
        {
            min = num3;
        }
        return min;
}
Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
abababa
  • 49
  • 8
  • You do not have a min defined in Ex1, so you can't pass it to function1 – traintraveler Nov 29 '19 at 13:02
  • oh sorry i didnt copy the code right i defined the min but i still have that problem – abababa Nov 29 '19 at 13:04
  • 2
    You might need to declare Ex1 and function1 before you call them or define them as in the answer below. Please refer to this question https://stackoverflow.com/questions/13297233/function-declaration-order-matters-in-c-language-or-am-i-doing-something-wrong – traintraveler Nov 29 '19 at 13:06

3 Answers3

2

The problem you have is that you are trying to call your functions before you have defined (or at least, declared) them. In the C language, when the compiler encounters a call to such an "undeclared" function, it assumes that it is of a type that returns int (it can normally work out what any argument types will be from the actual values given).

So when, in your main function, the compiler comes across Ex1(), it assumes that the function returns an int (even though this isn't actually used). Then, later, when you have your definition for void Ex1(), the compiler has encountered a conflicting (re-)definition - with a different return type.

To fix this, the simplest option is to put a "forward declaration" of any functions used (both Ex1 and function1) before any code that calls them. In your case, this would be:

#include <stdio.h> // This "system header" provides the declarations for "printf" and "scanf"
void Ex1(); // No definition here - just telling the compiler what form it takes.
int function1(int sum); // And the same here
int main()
{
    Ex1();
}
// .. . the rest of your code follows

Feel free to ask for further clarification and/or explanation.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
1

You have problem with passing arguments between two functions EX1 and function 1. function1 should have num1, num2 and num3 as arguments.

int main()
    {
        Ex1();
    }
    void Ex1()
    {
        int num1, num2, num3,min;
        printf("enter three diff numbers  \n");
        scanf("%d %d %d", &num1, &num2, &num3);
        printf("%d", function1(num1, num2, num3));
    }

    int function1(int num1, int num2, int num3)
    {
        int min, max;
        if (num1 > num2)
        {
            max = num1;
            min = num2;
        }
        else
        {
            max = num2; min = num1; 
        }
        if (num3 > max)
        {
            min = max;
            max = num3;
        }
        if (num3 > min&& num3 < max)
        {
            min = num3;
        }
        return min;
}
0

This is just the order of definition, try to do it like this:

int num1, num2, num3, min, max;

int function1(int sum)
{

    if (num1 > num2)
    {
        max = num1;
        min = num2;
    }
    else
    {
        max = num2; min = num1; 
    }
    if (num3 > max)
    {
        min = max;
        max = num3;
    }
    if (num3 > min&& num3 < max)
    {
        min = num3;
    }
    return min;

}

void Ex1()
{
    int num1, num2, num3;
    printf("enter three diff numbers  \n");
    scanf("%d %d %d", &num1, &num2, &num3);
    printf("%d", function1(min));
}

int main()
{
    Ex1();
}
Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92