0

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!

scottrod
  • 1
  • 2
  • 2
    `void main(void)` is an invalid signature on hosted environments. `main` shall return an `int` result. – too honest for this site Aug 24 '16 at 17:39
  • I really struggled if that is "primarily opinion based", "too broad" or "asking for external resources". Anyway, it is off-topic here. – too honest for this site Aug 24 '16 at 17:42
  • 1
    It seems that you need proper knowledge.My suggestion is to go for the definitive C books list. – machine_1 Aug 24 '16 at 17:48
  • 1
    Opinion based: my own preference is to provide a function prototype which is identical to the function definition. Copy/paste, add a `;` and it's done. It is also more readable, provided I have used meaningful names for its arguments. I always state the type. What's the alternative? Hunting around to remind myself what the types and arguments are. It's a no-brainer really. – Weather Vane Aug 24 '16 at 17:58
  • 1
    In example 3, the `int` is for the benefit of the compiler, so it knows what type of parameter the function takes. The name `idum2` is for the benefit of the programmer, so s/he knows what sort of information the parameter conveys. Choosing a descriptive name is therefore important. (Choosing a name that duplicates the name of a global variable should be avoided whenever possible.) And I agree with machine_1. At some point you need to go beyond the random junk on the internet, and actually buy a book. Here's the [list of books](http://stackoverflow.com/a/562377/3386109). – user3386109 Aug 24 '16 at 18:00

2 Answers2

0

While in forward declarations you don't need to give parameter's name only type is required (while you can provide name). But this doesn't have to be same as when you define the function .

In funciton definition say void my_func(int i){..} , in this function parameter is of type int and variable name is i . You can access it's value by variable name i inside the function block.

Also ,there is no re-declaration of variables as these have scope limited to function block.

There is basically no difference in those methods , it's convention how you use it.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
0

This would work too:

void my_func(int);

In function signatures it's not about the name of the arguments but about the type.

There are special rules for when you declare an external variable with the same name as a parameter

int idum1 = 1;

void my_func(int idum1)
{
  idum1++;
}

Here the variable with the most local scope is incremented, not the external.

Serve Laurijssen
  • 9,266
  • 5
  • 45
  • 98