0

I am trying to learn C but when trying to make a function I get an error.

This is my code:

int main()
{
    sayHi("Isaac", 14);
    return 0;
}

void sayHi(char name[], int age){
    printf("Hello %s! You are %d\n", name, age);
}

When I try to run this I get an error that says this:

Warning: Implicit declaration of function 'sayHi' is invalid in C99 Warning: This function declaration is not a prototype

Error: Conflicting types for 'sayHi'

Can anyone help me please?

1 Answers1

2

When the compiler examines main, it notices that you've referenced a function that it doesn't know about. It doesn't need to know the code for sayHi at this point but it does need to know what kind of arguments that function takes as well as what kind of value it returns.

This can be accomplished by a function declaration at the top of your code.

void sayHi(char name[], int age);

Once again, this is all C needs at this moment. This line tells the compiler that there is some function called sayHi with the specified arguments and return value. The actual code for the function, also known as the function's definition, can occur later as you have it.

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
  • 1
    For completeness: Specifying the types like this `void sayHi(char[], int);` would do. – alk May 15 '20 at 05:52