Ok, brilliant, I think i finally get it.
So, the prototype is just for the compiler, if the function is defined after main....I'm guessing it would compile with a prototype even if the function wasnt defined at somepoint, but obviously wouldnt run?
If the definition of the function is before main, there is no need for a prototype.
The definition has to be the same as the prototype except for the names of the variables.
When the function is used in main, it can be passed any 2 variables as long as they are the same type as the prototype.
Going on these assumptions, the following is correct (and follows good practice?):
Code:
#include <stdio.h>
int mult ( int x, int y ); //prototype
int main()
{
int f;
int r;
printf( "Please input two numbers to be multiplied: " );
scanf( "%d", &f );
scanf( "%d", &r );
printf( "The product of your two numbers is %d\n", mult(f,r));
getchar();
getchar();
}
int mult ( int eggs,int bread)
{
return eggs * bread;
}
One final question, if all the variables were first declared, would it be then be ok to leave out the types in the prototype and function, as follows:
Code:
#include <stdio.h>
int x,y,f,r,eggs,bread; //declare the variables first
int mult ( x, y ); //then no need to specify type in protoype?
int main()
{
printf( "Please input two numbers to be multiplied: " );
scanf( "%d", &f );
scanf( "%d", &r );
printf( "The product of your two numbers is %d\n", mult(f,r));
getchar();
getchar();
}
int mult (eggs, bread) //no need to specify a type of eggs or bread as already declared?
{
return eggs * bread;
}
Do I seem to have the correct ideas?
Thankyou for your help!