I always declare functions ahead of main(). What confuses me is the correct (or at least, best practice) method of declaring, defining, assigning, and passing function arguments. For example, this works:
// EXAMPLE 1 //
int idum1 = 1;
void my_func(int); // Says the function's going to need an integer.
void main (void)
{
my_func(idum1);
}
void my_func(idum2) // Is this then declaring AND defining a local integer, idum2?
{
// Do stuff using idum2...
}
But this works too:
// EXAMPLE 2 //
int idum1 = 1;
void my_func(int idum2); //Is this declaring a variable idum2 local to my_func?
void main (void)
{
my_func(idum1);
}
void my_func(idum3) // A different variable or the same one (idum2) still works.
//Is this declaring idum3 as a local integer?
{ //What happened to idum2, still a viable integer in the function?
// Do stuff using idum3...
}
And this works:
// EXAMPLE 3 //
int idum1 = 1;
void my_func(int idum2); // Declaring...
void main (void)
{
my_func(idum1);
}
void my_func(int idum2) //Declaring again. Different address as in function declaration?
//Same variable and address?
{
// Do stuff using idum2...
}
And so does this:
// EXAMPLE 4 //
int idum1 = 1;
void my_func(int);
void main (void)
{
my_func(idum1);
}
void my_func(int idum2) //Yet another way that works.
{
// Do stuff using idum2...
}
I'm a self-taught beginner, but I've been squeaking by for years not really knowing the right way and what's going on behind the scenes. I just know it works (always dangerous).
My gut says EXAMPLE 4 is the best way; tell it what type it's going to need, then declare it at the function along with the type for ease of coding and error checking. I'm sure there are reasons to do it one way or another, depending on what you're trying to do, but could really use some guidance here.
I do see EXAMPLE 3 a lot, but that seems superfluous, declaring a variable twice.
Can someone explain or point me to an article that explains the ins and outs of what I'm trying to get at here? Almost don't quite know what I'm asking, iykwim. Everything's so fragmented on the web. Tried CodingUnit but the tutorials just aren't deep enough. TIA!