Course List http://www.c-jump.com/bcc/
|
|
|
|
|
|
|
|
||||||
|
|
|
|
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> int main() { double result = sqrt( 1234.5 ); return 0; }
#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; }
int square( int x ) { return x * x; } int main( ) { int value = 5; int result = square( value ); return 0; }
Demo: download and run this EXE, then click
#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; }
return-type function-name( argument-declarations ) { /* declarations and statements */ }
void dummy() {}
which does nothing and returns nothing.
#include <iostream> using std::cout; 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; }
#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; }
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