-
prototype
When people give a prototype,
Code:
ie/ rval foo(Int32 val, Int32 *val2)
Is it supposed to be what I am passing into the function or what the first line of the function looks like?
like is it
Code:
>>function foo(Int32 val, Int32 *val2){
...
}
or
Code:
int main(void){
>>foo(Int32 val, Int32 *val2)
}
Thanks!
-
> ...what the first line of the function looks like?
By that you mean the function definition? Then yes, that's correct.
Code:
int foo(int val, int * val2);
int main(void)
{
int test = 5;
int b = 10;
foo(test, &b);
}
int foo(int val, int * val2)
{
/* do something */
return 0;
}
-
prototypes end in semi-colons
It declares the function for the compiler to look for or something like that
It is also declares the types and the names of the variables that you will be passing into when you call it
If you prototype a function like
Code:
int hi (int a, char *v);
you can call it like
You can change the variable name too when you are calling it
as long as the types match.
Then the function itself and what it does must be coded, in this example since it is int it must return a type of int
Code:
int hi (int a, char *v) {
return 0;
}
-
The prototype specifies what type of arguments a function takes and what it returns - nothing else (hence the compiler doesn't care which arguments you pass to the function, as long as the types match what the function expects).
The compiler uses that information to generate code to call your function.
Oh and make the definition and the prototype match, because the compiler will prioritize the prototype and if it compiles (usually it doesn't), then you will get bad things(TM).