CIS-62 Home http://www.c-jump.com/CIS62/CIS62syllabus.htm

C++ functions


  1. Structured Programming
  2. Software Design and Implementation
  3. C++ functions
  4. Math library functions
  5. Character classification
  6. Function parameters
  7. Function definitions
  8. Definition format
  9. Problem with function definitions
  10. Solution: function declarations (prototypes)
  11. Communication between functions
  12. return from a void function
  13. More terminology
  14. C++ Functions and Program Structure

Structured Programming


 
  • shuffle
  • deal
  • bet
  • hit
  • stand
  • etc... actions

  • card
  • suite
  • deck
  • bank
  • hand
  • score
  • dealer
  • player
  • etc... objects
  • ·
  • ·
  • ×
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ×
  • ·
  • ·
  • ·
black jack

Software Design and Implementation


  • Idea...
  • Drawing board...
  • Vocabulary...
  • Features...
  • User interface...
  • Components:
    • variables
    • functions
  • Step-by-step approach:
    • incremental steps
    • compile often
    • test small changes
  • Variables:

int card;
int suite;
int deck[ 52 ];
double bank;
int hand[ 6 ];
int score;
struct dealer;
struct player;
//etc... 
  • Functions:

shuffle( deck );
deal( deck, hand );
bet( 1.00 );
hit( player );
stand();
//etc... 

C++ functions


Math library functions


Character classification and conversion library functions



#include <cctype>
// isalpha(c) non-zero if c is alphabetic, 0 if not 
// isupper(c) non-zero if c is upper case, 0 if not 
// islower(c) non-zero if c is lower case, 0 if not 
// isdigit(c) non-zero if c is digit, 0 if not 
// isalnum(c) non-zero if isalpha(c) or isdigit(c), 0 if not 
// isspace(c) non-zero if c is blank, tab, newline, return, formfeed, vertical tab 
// toupper(c) return c converted to UPPER CASE 
// tolower(c) return c converted to lower case 
void main( )
{
    char ch = 'A';
    int result = isalpha( ch );
}

Function parameters



int square( int x )
{
    return x * x;
}

void main( )
{
    int value = 5;
    int result = square( value );
}

function parameters

Function definitions



#include <iostream>

int square( int x )
{
    return x * x;
}

void display( int x )
{
    std::cout << x;
}

void main( )
{
    int value = 5;
    int result = square( value );
    display( result );
}

function parameters

Function definition format


Problem with function definitions


Solution: function declarations (prototypes)


#include <iostream>
char heads( bool value );   // function declarations
char tails( bool value );

char heads( bool value )    // function definitions
{
    if ( value == true )
        return 'H';
    return tolower( tails( value ) );
}

char tails( bool value )
{
    if ( value == false ) {
        return 'T';
    }
    return tolower( heads( value ) );
}

void main( )
{
    std::cout << heads( true );  // prints H
    std::cout << heads( false ); // prints t
    std::cout << tails( true );  // prints h
    std::cout << tails( false ); // prints T
}

Communication between functions


return from a void function



    void display_value( int x )
    {
        if ( x < 0 )
            return;
        std::cout << x;
    }

More terminology


C++ Functions and Program Structure