Thread: Calling a function with 50 input variables

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    204

    Calling a function with 50 input variables

    I have a bunch of variables declared at the top of my main function. Is there a way I can call a function without creating a mess like the one below?
    Code:
    some_function(aasdf1,afas2,a33,aaasd4,adhjd5,adghj6,adgf7,a8djfg,a9,q1ffj,q2ff,q3,q4dfg,
    q5,6,q7,wff1,w2,w333,wfgjdf4,w5,wdfgj6,w7,e,e1,e2,efgjdfg3,edfgje44,tdfgjdft,ygdfgjsf,
    uuu,afas2,a33,aaasd4,adhjd5,adghj6,adgf7,a8djfg,a9,q1ffj,q2ff,q3,q4dfg,q5,6,q7,wff1,w2,
    w333,wfgjdf4,w5,wdfgj6,w7,e,e1,e2,efgjdfg3,edfgje44,tdfgjdft,ygdfgjsf,uuu);
    ???

    Should I just use the #define directive? Am I just being lazy?

  2. #2
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    >I have a bunch of variables declared at the top of my main function. Is there a way I can call a function without creating a mess like the one below?

    yes you can, by making the required paramater less in number

    Code:
    some_function(aasdf1,afas2,a33,aaasd4,adhjd5,adghj6,adgf7,a8djfg,a9,q1ffj,q2ff,q3,q4dfg,
    q5,6,q7,wff1,w2,w333,wfgjdf4,w5,wdfgj6,w7,e,e1,e2,efgjdfg3,edfgje44,tdfgjdft,ygdfgjsf,
    uuu,afas2,a33,aaasd4,adhjd5,adghj6,adgf7,a8djfg,a9,q1ffj,q2ff,q3,q4dfg,q5,6,q7,wff1,w2,
    w333,wfgjdf4,w5,wdfgj6,w7,e,e1,e2,efgjdfg3,edfgje44,tdfgjdft,ygdfgjsf,uuu);
    >???
    this is as well allowed, tho don't forget to put the paramater type

    >Should I just use the #define directive?
    of course you can, but you will have to type extra "#define" for each parameter, which is worse

    >Am I just being lazy?
    you look unneccesarily dilligent to me, don't worry


    another idea is to use for loop to pass in your variable
    Code:
    int i;
    for(i = 0; i < SOMETHING; i++) {
       someFunction(i);
    }

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Hey, what does some_function do? May-be you are making it do too much? May-be some of the variables could be rightfully global? Can you tell what each of the variables/parameters stands for? May-be structs would help?

  4. #4
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    This sounds like a job for arrays!

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    58

    re:Calling a function with 50 input variables

    You could also put your variables in a structure, then pass the address of the structure to your function.

    Sam

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yeah, the numbers at the end of the variable names makes one suspect that arrays should be used somewhere. But then, the variable names look totally random, like a made-up hypothetical example, and it's impossible to diagnose the problem (how on Earth can a function need that many variables) properly.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,625
    If they're all that closely related, put them in a structure.

    Then pass a pointer to that struct as a single parameter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. function to read an int from input
    By linucksrox in forum C Programming
    Replies: 8
    Last Post: 06-08-2004, 10:15 AM
  5. Function calling woes
    By RedZippo in forum C Programming
    Replies: 6
    Last Post: 01-09-2004, 12:39 AM