Passing in values to functions

This is a discussion on Passing in values to functions within the C Programming forums, part of the General Programming Boards category; I remember that there was a way to pass a value into a function sometimes, but not always. If you ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174

    Passing in values to functions

    I remember that there was a way to pass a value into a function sometimes, but not always. If you didn't pass the value into the function, then either that variable doesn't exist for the lifetime of the function or the variable is automatically set to a predefined value.

    For example, I have a function prototype

    Code:
    printCommand(Line *head, int lineNumber)
    And sometimes I want to call this function without the int being called. Also, is it possible to instead send a string to the function as well?

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,483
    is it possible to instead send a string to the function as well
    The C language does not support sending/returning arrays by value, but you may pass them in by reference - i.e. A pointer to the first char in the array

    And sometimes I want to call this function without the int being called
    If you are referring to overloading the function, the answer is no. What you can do is use a pointer to pass the int into the function and when you want to not use the int pass a NULL into the function -> Your function could test for this and behave differently if the int was a NULL pointer
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Quote Originally Posted by Click_here View Post
    If you are referring to overloading the function, the answer is no. What you can do is use a pointer to pass the int into the function and when you want to not use the int pass a NULL into the function -> Your function could test for this and behave differently if the int was a NULL pointer
    Oh I see. Is that how some common library functions such as printf and scanf work?

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Mentallic View Post
    Oh I see. Is that how some common library functions such as printf and scanf work?
    They use a "variable argument list" using the macros defined in stdarg.h. That might be what you want, but you'd have to explain exactly what you're trying to accomplish to get the best advice.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Quote Originally Posted by oogabooga View Post
    They use a "variable argument list" using the macros defined in stdarg.h. That might be what you want, but you'd have to explain exactly what you're trying to accomplish to get the best advice.
    Well, each node in my linked list is a line of text, and I'm required to - depending on user input - print out either a specific line number (hence the int lineNumber) or also if they input '+' or '-' to print out the next/previous line respectively.
    I've settled with changing the function prototype to
    Code:
    printCommand(Line *head, int lineNumber)
    because I think it'll keep things more clear in the end if I just converted the strings to numbers if they are numbers, because I'm going to also be taking in a lot more different types of character input down the track.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,483
    Entering a NULL pointer to printf is a big no.

    If you are interested in the way they work, look up "va_list"/ "va_start"/ "va_end".
    Fact - Beethoven wrote his first symphony in C

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Mentallic View Post
    Well, each node in my linked list is a line of text, and I'm required to - depending on user input - print out either a specific line number (hence the int lineNumber) or also if they input '+' or '-' to print out the next/previous line respectively.
    Here are some possibilities.


    Use negative line numbers as special codes:
    Code:
    #define CMD_NEXT -1
    #define CMD_PREV -2
    
    void print(Line *head, int lineNumber) {
        switch (lineNumber) {
        case CMD_NEXT:
            // print next line
            break;
        case CMD_PREV:
            // print prev line
            break;
        default:
            if (lineNumber > 0)  // or >= 0
                // print line lineNumber
            else
                // error
        }
    }
    
    // calls
    print(line, 20);         // print line 20
    print(line, CMD_NEXT);   // print next line
    print(line, CMD_PREV);   // print prev line

    Use an extra parameter:
    Code:
    void print(Line *head, char command, int lineNumber) {
        switch (command) {
        case 'N':
            // print next line
            break;
        case 'P':
            // print prev line
            break;
        case 'L':
            // print line lineNumber
            break;
        default:
            // error
        }
    }
    
    // calls
    print(line, 'L', 20); // print line 20
    print(line, 'N', 0);   // print next line
    print(line, 'P', 0);   // print prev line

    Use a string:
    Code:
    void print(Line *head, char *command) {
        int lineNumber;
        switch (command[0]) {
        case 'N':
            // print next line
            break;
        case 'P':
            // print prev line
            break;
        case 'L':
            // print line lineNumber
            lineNumber = atoi(command + 1);
            break;
        default:
            // error
        }
    }
    
    // calls
    print(line, "L20"); // print line 20
    print(line, "N");   // print next line
    print(line, "P");   // print prev line
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Thanks for showing me a few options. I had already began using negative values to represent traversing the list but decided to go with strings instead since it seemed to make things a bit clearer.

    Oh and it works now, so again thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Values From Functions
    By skmightymouse in forum C Programming
    Replies: 21
    Last Post: 03-25-2012, 11:48 AM
  2. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  3. Problem Passing Values between functions
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2007, 01:21 PM
  4. functions passing values
    By srinurocks in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2002, 11:49 PM
  5. Passing Values to Functions
    By shad0w in forum C Programming
    Replies: 2
    Last Post: 12-25-2001, 07:28 PM

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