syque.com

The Psychology of Quality and More

| Menu | Books | Share | Search | Settings |

C Style: Standards and Guidelines (contents)

CHAPTER 8 : Language Usage

PART 4 : USAGE

CHAPTER 8 : Language Usage
8.1 General principles of language usage
8.2 Using expressions
8.3 Using 'if'
8.4 Using 'while'
8.5 Using 'for'
8.6 Using 'do'
8.7 Using 'switch'
8.8 Using 'goto'
8.9 Using 'continue' and 'break'
8.10 Using 'return'
8.11 Using functions
8.12 Using '#define'
8.13 Conditional compilation
8.14 Other preprocessor commands
8.15 Summary

<--Prev page | Next page -->

 

8.10  Using 'return'

Where do you return from a function? A simple philosophy is to return as soon as you are ready, for whatever reason:

 

WriteStatus = PutDetailBlock( pEmpRec->EmpDetail );
if ( WriteStatus == WRITE_OK )
    return ( WRITE_OK );

if ( WriteStatus == DISC_FULL )
{
    printf( "Disc full, can't write details\n" );
    return ( WRITE_ERR ):
}
...

 

This reduces levels of nesting, but may be difficult to follow and trace, and can result in many return's throughout the code. A simple way of counteracting this is to ban all return's, except at end of the function:

 

WriteStatus = PutBlock( pEmpRec->EmpDetail );
if ( WriteStatus == WRITE_OK )
    RetVal = WRITE_OK;
else
{
    if ( WriteStatus == DISC_FULL )
    {
        printf( "Disc full, can't write details\n" );
         RetVal = WRITE_ERR;
    }
    else...
...
}
...
return ( RetVal );

-------------------------------------------------------

This can help tracing (e.g. put breakpoint just before the end) and ensures use of any final 'cleanup code' (eg. closing files), but it can also make code more unreadable, as it forces more use of nesting and awkward paths through the code.

A compromise between the above two examples is to allow error returns, but to insist that there is only one non-error return point which must be at the end of the function:

 

WriteStatus = PutBlock( pEmpRec->EmpDetail );
if ( WriteStatus != WRITE_OK )
{
    if ( WriteStatus == DISC_FULL )
    {
        printf( "Disc full, can't write details\n" );
        return ( WRITE_ERR ):
    }
...
}
...
return ( WRITE_OK );

 

Return type

If the function is declared to return a given type, then a value of that type should always be returned. The only time a plain return; may be used is when the function is declared to return a type void.

Returning from 'main'

In several environments, values returned from programs are available and may be tested in a shell script. Typically a non-zero return indicates that the program did not successfully complete its purpose. It is worth doing this, even if no other error codes are returned.

 

<--Prev page | Next page -->

 

Site Menu

| Home | Top | Settings |

Quality: | Quality Toolbook | Tools of the Trade | Improvement Encyclopedia | Quality Articles | Being Creative | Being Persuasive |

And: | C Style (Book) | Stories | Articles | Bookstore | My Photos | About | Contact |

Settings: | Computer layout | Mobile layout | Small font | Medium font | Large font | Translate |

 

You can buy books here

More Kindle books:

And the big
paperback book


Look inside

 

Please help and share:

 

| Home | Top | Menu |

© Changing Works 2002-2015
Massive Content -- Maximum Speed