optional parameters

This is a discussion on optional parameters within the C++ Programming forums, part of the General Programming Boards category; How do I make the head of a linked list as a function parameter optional? For example, myFunction(node* head = ...

  1. #1
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61

    optional parameters

    How do I make the head of a linked list as a function parameter optional?

    For example,

    myFunction(node* head = node()) //does not work.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    void myFunction(node *head = NULL)
    {
      if (head == NULL)
        // Whatever you need to do to take care of the head not being given.
    }

  3. #3
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    Is there another way to do it? Because I create the linked list by calling a function from main, (so head will not be null), but then I have main call another function that needs to recursively use the list...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Is there another way to do it?
    Function overloading?

    Because I create the linked list by calling a function from main, (so head will not be null), but then I have main call another function that needs to recursively use the list...
    I do not see how is this related to needing optional parameters without default arguments. You need to explain more, methinks.
    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

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So in that scenario you don't want an optional parameter. In main, you would need to pass the head of the list that you just created into the function, and the recursive calls would use their own thing as appropriate.

  6. #6
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    Well, the head is a private variable inside a class... therefore, main does not have access to it... the functions that create and recursively use the list are members of the class...

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If the functions that use the list are members of the class, then you need to be calling them from an instance of the class, no? And that instance of the class will be able to access the head just fine. So your function should be called like MyPrivateClass.doStuff(), and doStuff() can call another method doRecursiveStuff(list_head) or whatever.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Well, the head is a private variable inside a class... therefore, main does not have access to it... the functions that create and recursively use the list are members of the class...
    Right, but the other member functions have access to the head, so what is the problem?
    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

  9. #9
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    tabstop: I can do that but I want main to give an overall picture of the program. If it is not possible it is not possible... I just like main to be my function caller.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And since in my scenario main does give an overall picture of the program (I'm assuming you have a more descriptive name in mind than "doStuff"), I fail to see the problem here.

    In other words, people reading your main will see that X happens; that it happens recursively they would have to look at the class to discover. On the other hand, they shouldn't care whether it happens recursively or iteratively. (Unless they're your teacher, in which case they would be reading the whole code anyway so big deal.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Optional Function Parameters after ...
    By LPP in forum C Programming
    Replies: 7
    Last Post: 10-28-2006, 01:40 AM
  2. Optional Function Parameters in Prototypes
    By LPP in forum C++ Programming
    Replies: 27
    Last Post: 10-21-2006, 01:11 PM
  3. function with variable number of parameters
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 04:35 PM
  4. including optional parameters in function
    By louiegonzalus in forum C Programming
    Replies: 4
    Last Post: 11-28-2002, 03:34 PM
  5. command-line parameters.
    By Tombear in forum C Programming
    Replies: 2
    Last Post: 10-28-2001, 08:40 AM

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