Thread: function argument not needed

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

    function argument not needed

    I have a function that takes a string as an argument. I call the function from main but because the function is recursive, I pass it a dummy string. Once inside the function that string is overwritten (because it is useless) and in short the recursive process begins and everything works great.

    My question is, is there anyway around having to pass the dummy string to the function from main? The string argument is only needed once inside the called function for recursion.

    Thanks.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Make it an optional parameter.
    Code:
    void foo(const string& bar = string());
    // or
    void foo(string bar = string());

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Here is a way:
    Code:
    myFun(char *dummyString);
    Fun() {
       char *string = "Overwritten Value";
       myFun(string);
    };
    so you can call Fun() instead of myFun(). Fun() will be like a function to initialize myFun(), your original function.

    I don't think there is another way. Well, maybe a macro could do the trick. Like:
    Code:
    #define myFun() myFun("");
    so when you write myFun() it will be replaced with myFun(""), with "" as the dummy string.

    Which is "better"? The first is more readable, the second might be more efficient, depending on your function

    EDIT: But of course optional parameter seems much better (didn't know it existed)
    Last edited by C_ntua; 09-16-2008 at 05:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Replies: 9
    Last Post: 12-25-2007, 05:01 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM