Question about passing & receiving parameters

This is a discussion on Question about passing & receiving parameters within the C Programming forums, part of the General Programming Boards category; Guys, I have a question or two about the following code segment that I hope someone will answer. This was ...

  1. #1
    TCB
    TCB is offline
    Registered User
    Join Date
    Apr 2006
    Posts
    22

    Question about passing & receiving parameters

    Guys,

    I have a question or two about the following code segment that I hope someone will answer. This was part of a source code file provided on the CD, so I did not pick the variables.

    Code:
    void output(float a) 
    {
        printf("The average is %.2f\n", a);
    }
    
    int main()
    {
        int     num1, num2, num3;
        float   ave;
    
        num1 = input(1); 
        num2 = input(2); 
        num3 = input(3);
    
        ave = average3(num1, num2, num3);
    
        output(ave);
    }
    My questions:

    1) When passing ave to output as 'a' why don't we have to declare 'float a;' in the function--or are we in essence doing that when we declare the function?

    2) I have seen a couple different examples of parameters that should be included in the main(). In a case like this, what would be the recommended parameter(s) to include for main to receive? ...void?

    The texts I have don't seem to agree and I have no one else to ask; so I very much appreciate the help.

    TB

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    2) I have seen a couple different examples of parameters that should be included in the main(). In a case like this, what would be the recommended parameter(s) to include for main to receive? ...void?
    In C and C++, main(void) is the same as main(). In C, a parameter like func() is different from func(void) (but not in C++); the first means "parameters unknown" and the second "no parameters". So for a declaration, you can use either, but for a prototype you should use (void).

    You may also have seen
    Code:
    int main(int argc, char *argv[])
    which accepts command line parameters. You don't need to use this.

    There's an FAQ on the subject. [edit]http://faq.cprogramming.com/cgi-bin/...&id=1043284376[/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    1) When passing ave to output as 'a' why don't we have to declare 'float a;' in the function--or are we in essence doing that when we declare the function?
    You're passing a float value to output(), which can call that float whatever it wants.

    It's like this:
    Code:
    float a = 5;
    float b = a;
    It doesn't matter what b is called. It still gets the value of a.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    1) When passing ave to output as 'a' why don't we have to declare 'float a;' in the function--or are we in essence doing that when we declare the function?
    Yes the formal parameters gets declared for you so you don't need to bother with doing that in the function body.

  5. #5
    TCB
    TCB is offline
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    Quote Originally Posted by dwks
    You're passing a float value to output(), which can call that float whatever it wants.
    Ok...so passing a value to a function basically negates the need to declare any such variable in that receiving function--even though that might be the first use of 'a' or whatever else you tell it to receive?


    Quote Originally Posted by dwks
    It's like this:
    Code:
    float a = 5;
    float b = a;
    It doesn't matter what b is called. It still gets the value of a.
    But it *does* matter what 'b' is called--you need to assign the value of 'a' to something. When you declared 'b' it was created, so now it's available for assignment; same as if you had named it 'bb' or 'bbb' or...

    I guess what I am trying to say is that I completely understand about declaring & initializing variables--and making assignments to them. But I guess I just didn't understand whether or not there is a difference between passing a value (and not having to declare it in the new function), and declaring it in the first place.

    So from the code sample above if I pass 'ave' to output(), the only way that the new function will know anything about the parameter is by me telling it to expect a float variable; and then passing by value into it. Do I have that straight?

    I will read that faq that dwks linked--though I think I might have already.

    Thanks for the help.

    TB
    Last edited by TCB; 04-10-2006 at 07:15 PM.

  6. #6
    TCB
    TCB is offline
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    Quote Originally Posted by OnionKnight
    Yes the formal parameters gets declared for you so you don't need to bother with doing that in the function body.
    Thanks OnionKnight...

    I was responding to another post when you posted. That clarified it for me.

    TB

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    So from the code sample above if I pass 'ave' to output(), the only way that the new function will know anything about the parameter is by me telling it to expect a float variable; and then passing by value into it. Do I have that straight?
    Exactly. The new function doesn't know, and doesn't care, what the original variable was called.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    TCB
    TCB is offline
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    Quote Originally Posted by dwks
    Exactly. The new function doesn't know, and doesn't care, what the original variable was called.
    Thanks.

    And thanks for the link--I had NOT seen it, and appreciate it very much.

    TB

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,597
    On a side note and for future reference you will do yourself a good favor if you make your prototype in a header exactly match the function definition in the module file.

    Header
    Code:
    float Add(float v1,float v2);
    Module
    Code:
    float Add(float v1,float v2)
    {
      return v1+v2;
    }
    Technically it does NOT have to be this way and will compile using different parameter names, however, I do not think it is a good practice.

  10. #10
    TCB
    TCB is offline
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    Bubba,

    Good tip...

    I just learned about function prototypes last week. I guess I knew about them for quite some time, but didn't know what they really were. But I can see how they would really be a useful tool--especially down the road a bit.

    TB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct question... difference between passing to function...
    By Sparrowhawk in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2009, 03:59 PM
  2. QUESTION!! parameter passing
    By jave in forum C Programming
    Replies: 8
    Last Post: 10-21-2005, 01:50 PM
  3. Passing Values Question... (continue)
    By tinkerbell20 in forum C++ Programming
    Replies: 4
    Last Post: 06-29-2005, 02:51 AM
  4. Question about Templates and passing arguments
    By supaben34 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2002, 02:32 AM
  5. Passing parameters to a module
    By Sue Paterniti in forum C Programming
    Replies: 25
    Last Post: 05-09-2002, 07:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21