Thread: which is best practice

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    which is best practice

    given a main that is the same bar the parameters passed to the function which function is best practice

    Code:
    int CountDays( int iNumDays, int *iWeekDay )
    {
        //declare function variable
        int iFirstisSunday = 0;
    
        //test to see if first day of the month is a sunday
        if ( *iWeekDay == 1 )
        {
            //set ifirstissunday to 1
            iFirstisSunday = 1;
        }
    
        //there are 29 days in four weeks counting the first day as 1 ( 1 + 28 = 29 )
        iNumDays -= 29;
    
        //add the remainder of inumdays to i weekday + 1 so iweeday is set to the 1st day of next month
        *iWeekDay += ( iNumDays + 1 );
    
        //see  if a new week has started
        if ( *iWeekDay > 7 )
        {
            //subtract 7 from i weekday to get the current day
            *iWeekDay -= 7;
        }
    
        return iFirstisSunday;
    }
    or
    Code:
    int CountDays( int iNumDays )
    {
        //declare function variables
        static int iWeekDay = 2; //sun = 1 mon = 2 tues = 3 ....1/1/1900 was a monday
        int iFirstisSunday = 0;
    
        //test to see if first day of the month is a sunday
        if ( iWeekDay == 1 )
        {
            //set ifirstissunday to 1
            iFirstisSunday = 1;
        }
    
        //there are 29 days in four weeks counting the first day as 1 ( 1 + 28 = 29 )
        iNumDays -= 29;
    
        //add the remainder of inumdays to i weekday + 1 so iweeday is set to the 1st day of next month
        iWeekDay += ( iNumDays + 1 );
    
        //see  if a new week has started
        if ( iWeekDay > 7 )
        {
            //subtract 7 from i weekday to get the current day
            iWeekDay -= 7;
        }
    
        return iFirstisSunday;
    }
    the main doesn't care about the day of the week all the calculation is done in the function
    Last edited by cooper1200; 05-26-2023 at 03:39 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,669
    The first one is better.

    Having hidden statics inside the function means trouble for
    - calling the function multiple times between sequence points
    - calling the function from multiple threads
    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. Practice
    By RunDosRun in forum C++ Programming
    Replies: 3
    Last Post: 12-24-2007, 12:35 PM
  2. Practice
    By 00Sven in forum C Programming
    Replies: 1
    Last Post: 03-13-2006, 04:13 PM
  3. just practice
    By Fizz in forum C++ Programming
    Replies: 4
    Last Post: 05-27-2004, 07:24 AM
  4. is it bad practice to....
    By abrege in forum C++ Programming
    Replies: 7
    Last Post: 02-07-2003, 09:03 PM
  5. Bad Practice??
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 08:37 AM

Tags for this Thread