Explanation of arguments/parameters in Functions

This is a discussion on Explanation of arguments/parameters in Functions within the C Programming forums, part of the General Programming Boards category; I am not 100% sure about functions at the moment. [1] Declare function above main. Code: int qcheck(int holdans, int ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    21

    Explanation of arguments/parameters in Functions

    I am not 100% sure about functions at the moment.

    [1] Declare function above main.

    Code:
    int qcheck(int holdans, int *p);

    [2] Call function:

    Code:
    qcheck (holdans, &score);
    [3] Function definition that is called:

    Code:
    int qcheck(int holdans, int *p)
    {
               code
    }

    Question.
    Does 1 and 2 always have to match exactly?



    p.s I love pointers (when I understand them), they can be so simple and short.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Yes, they should be.
    You are asking if 1 and 3 have to match exactly, right?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,165
    Quote Originally Posted by hellogamesmaste
    Does 1 and 2 always have to match exactly?
    That depends on what you mean by "match exactly". For example, a parameter could be declared as a const int*, but you can pass a pointer to a non-const int as a corresponding argument.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    2 is coded wrong. It returns an int, therefore assign the return value to an int.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by Dino View Post
    2 is coded wrong. It returns an int, therefore assign the return value to an int.
    It is not a requirement to read the return value of a function. Do you always assign the return value of printf() to a variable when you use it?
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    The return value is optional... it's correct code. Whether it's semantically correct or not, we can only guess.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    21
    I ask because my program didn't work and I didn't know why.

    In [1] I had:

    Code:
    int qcheck(int , int *p);
    in [3] I had:


    Code:
    int qcheck(holdans, int *p)

    So I kept getting error messages because the int was missing in 3.


    Another question, regarding pointers in functions.

    Usually you do the following:

    int a, *p
    p = &a
    *p = 10

    However, with functions, you can leave out the p = &a by:

    Code:
    int qcheck(int , int *p);
    Code:
    qcheck (holdans, &score);
    Passing the &score into a calling function to a called function that has int *p as a parameter, passing p to &score is therefore not necessary. Does the function do this automatically?

    I got really confused and kept doing in main:

    Code:
    p=&score
    and then tried to
    Code:
    qcheck(holdans, p)
    because I thought it would pass the address.

    I got it working in the end, but I kept thinking of pointing p to &score. Obviously this must not be necessary when passing pointers to functions.

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    So I kept getting error messages because the int was missing in 3.
    yeah - you need to put the type in both places

    However, with functions, you can leave out the p = &a by:
    Whenever you pass a value to a function, the values you pass in are assigned to the local variables specified in the parameters. In other words, if I have a function

    Code:
    add(int a, int b)
    that I call from main, main shouldn't be manually assigning anything to a and b, because they're not in scope for main. All you do is call the function as
    Code:
    add(1, 3)
    and the values 1 and 3 are assigned to a and b as local variables inside the function. The same goes for pointer. If you accept a pointer as a parameter, whatever address is passed in is assigned to that pointer.

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    21
    Ahh thank you. That has made it clearer. I just hope it sticks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 12:50 PM
  2. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  3. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 02:41 PM
  4. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 06:39 AM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 08:53 AM

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