Course List http://www.c-jump.com/bcc/

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 
int main( )
{
    char ch = 'A';
    int result = isalpha( ch );
    return 0;
}

Function parameters



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

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

function parameters

Function definitions



#include <iostream>

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

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

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

function parameters

Function definition format


Problem with function definitions


Solution: function declarations (prototypes)



#include <iostream>
using std::cout;

// Function declarations
void print_b( int count );
void print_a( int count );
void print_n( int count );

// Function definitions
void print_b( int count )
{
    --count; // 6
    if ( count == 0 ) return;
    cout << "b";
    print_a( count );
}

void print_a( int count )
{
    --count; // 5 3 1
    if ( count == 0 ) return;
    cout << "a";
    print_n( count );
    return;
}

void print_n( int count )
{
    --count; // 4 2 0
    if ( count == 0 ) return;
    cout << "n";
    print_a( count );
    return;
}

int main( )
{
    // banana
    int count = 7;
    print_b( count );
    return 0;
}

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