CS105

What is a function?       Why do you need a function?

Scope of the function.   Examples of functions.     

Example

Example program which calls separate functions.
This program is pretty silly, but it will hopefully show some of the main syntax and fundamental aspects of using separate functions. You might like to read the complete program first, then link to the notes embedded in it. The program has a structure which matches the diagram shown above.

// Sample program
// using separate function

#include <iostream.h>

// function prototypes
void say_hello( void ); // must have semicolon here
void say_goodbye( void );

void main( void )
{
cout << "This program calls two functions" << endl;
cout << "First a greeting... here goes..." << endl;

say_hello(); // : calling a function

cout << "Now back in main" << endl;
cout << "Now we call the other function..." << endl;

say_goodbye(); // calling another function

cout << "Back in main. That's all folks." << endl;
}

// The full definitions of the separate functions
// See Note 3.

void say_hello( void ) // no semicolon here
{
cout << "This is the hello function" << endl;
cout << "HELLO THERE!" << endl;
}

void say_goodbye( void ) // no semicolon here
{
cout << "This is the goodbye function" << endl;
cout << "GOODBYE, Hope you enjoyed this program" << endl;
}

 

       What is a function?       Why do you need a function?

Scope of the function.   Examples of functions.