CIS-62 Home http://www.c-jump.com/CIS62/CIS62syllabus.htm
|
|
|
|
|
|
|
|
||||||
|
|
|
|
int card; int suite; int deck[ 52 ]; double bank; int hand[ 6 ]; int score; struct dealer; struct player; //etc... |
shuffle( deck ); deal( deck, hand ); bet( 1.00 ); hit( player ); stand(); //etc... |
log( x ); // natural logarithm of x pow( x, y ); // x raised to power y sin( x ); // trigonometric sine of x sqrt( x ); // square root of x tan( x ); // trigonometric tangent
#include <cmath> void main() { double result = sqrt( 1234.5 ); }
#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 ); }
int square( int x ) { return x * x; } void main( ) { int value = 5; int result = square( value ); }
Demo: download and run this EXE, then click
#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 ); }
return-type function-name( argument-declarations ) { /* declarations and statements */ }
void dummy() {}
which does nothing and returns nothing.
#include <iostream> char heads( bool value ) { if ( value == true ) return 'H'; return tolower( tails( value ) ); // *** does not compile! } 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 }
#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 }
int max_value( int x, int y ) { if ( x > y ) return x; return y; }
void display_value( int x ) { if ( x < 0 ) return; std::cout << x; }
void f();
void f(); void f() { /*function body*/ }
void f() { /*function body*/ }
Functions break large computing tasks into smaller ones.
Functions enable people to build on what others have done,
instead of starting over from scratch.
Functions hide details of operation from other parts of the program