How many parameters should a function have?


There is no set number or "guideline" limit to the number of parameters your functions can have. However, it is considered bad programming style for your functions to contain an inordinately high (eight or more) number of parameters. The number of parameters a function has also directly affects the speed at which it is called—the more parameters, the slower the function call. Therefore, if possible, you should minimize the number of parameters you use in a function. If you are using more than four parameters, you might want to rethink your function design and calling conventions.

One technique that can be helpful if you find yourself with a large number of function parameters is to put your function parameters in a structure. Consider the following program, which contains a function namedprint_report() that uses 10 parameters. Instead of making an enormous function declaration and proto- type, the print_report() function uses a structure to get its parameters:

#include <stdio.h>
typedef struct
     int       orientation;
     char      rpt_name[25];
     char      rpt_path[40];
     int       destination;
     char      output_file[25];
     int       starting_page;
     int       ending_page;
     char      db_name[25];
     char      db_path[40];
     int       draft_quality;
void main(void);
int print_report(RPT_PARMS*);
void main(void)
     RPT_PARMS rpt_parm; /* define the report parameter
                            structure variable */
     /* set up the report parameter structure variable to pass to the
       print_report() function */
       rpt_parm.orientation = ORIENT_LANDSCAPE;
     rpt_parm.rpt_name = "QSALES.RPT";
     rpt_parm.rpt_path = "C:\REPORTS";
     rpt_parm.destination = DEST_FILE;
     rpt_parm.output_file = "QSALES.TXT";
     rpt_parm.starting_page = 1;
     rpt_parm.ending_page = RPT_END;
     rpt_parm.db_name = "SALES.DB";
     rpt_parm.db_path = "C:\DATA";
     rpt_parm.draft_quality = TRUE;
     /* Call the print_report() function, passing it a pointer to the
     parameters instead of passing it a long list of 10 separate
        parameters. */
     ret_code = print_report(&rpt_parm);
int print_report(RPT_PARMS* p)
     int rc;
     /* access the report parameters passed to the print_report()
        function */
     set_printer_quality((p->draft_quality == TRUE) ? DRAFT : NORMAL);
     return rc;

The preceding example avoided a large, messy function prototype and definition by setting up a predefined structure of type RPT_PARMS to hold the 10 parameters that were needed by the print_report()function.  The only possible disadvantage to this approach is that by removing the parameters from the function definition, you are bypassing the compiler's capability to type-check each of the parameters for validity during the compile stage.

Generally, you should keep your functions small and focused, with as few parameters as possible to help with execution speed. If you find yourself writing lengthy functions with many parameters, maybe you should rethink your function design or consider using the structure-passing technique presented here. Additionally, keeping your functions small and focused will help when you are trying to isolate and fix bugs in your programs.


© 2017