Thread: Explanation of arguments/parameters in Functions

  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
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Chappell Hill, Texas
    Posts
    2,332
    2 is coded wrong. It returns an int, therefore assign the return value to an int.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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
    Location
    Inside my computer
    Posts
    24,654
    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
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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, 11:50 AM
  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, 01:41 PM
  4. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM