|< C & C++ Functions 2 | Main | C & C++ Functions 4 >| Site Index | Download |


 

 

 

MODULE 4b

MORE ON C/C++ FUNCTIONS - PROGRAM EXAMPLES 3

Receive nothing, return nothing-receive nothing, return something-
receive something, return something-receive something, return nothing
And they do something.  That is a function!

 

 

 

 

 

 

 

 

My Training Period: xx hours

 

This is a continuation from the previous Module. The source code for this Module is: C/C++ functions source codes and the lab worksheets for your practice: Function lab worksheet 1, lab worksheet 2, lab worksheet 3 and lab worksheet 4.

 

The C and C++ programming skills:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Program Examples and Experiments

 

Example #1

 

// function skeleton example, passing by values

#include <iostream>

using namespace std;

 

// function prototypes and their variations...

// notice and remember these variations...

void FunctOne(void);

double FunctTwo();

int FunctThree(int);

void FunctFour(int);

 

// main program...

void main()

{

       cout<<"I'm in main()..."<<endl;

       // function call, go to FunctOne without any argument...

       FunctOne();

       cout<<"\nBack in main()..."<<endl;

       // function call, go to FunctTwo without any argument...

       double q = FunctTwo();

       // display the returned value...

       cout<<"The returned value = "<<q<<endl;

       // function call, go to FunctThree with an argument...

       int y = 100;

       int x = FunctThree(y);

       cout<<"Back in main()..."<<endl;

       // display the returned value...

       cout<<"Display the returned value = "<<x<<endl;

       int r = 50;

       FunctFour(r);

 

       // return nothing or just omit this 'return;' statement

       return;

}

 

void FunctOne()

{

       // do nothing here just display the following text...

       cout<<"\nNow I'm in FunctOne()!..."<<endl;

       cout<<"Receives nothing, return nothing..."<<endl;

       // return to main, without any returned value

       // return;   optionally can put this empty 'return;'

}

 

double FunctTwo()

{

       // receive nothing but do some work here...

       double p = 10.123;

       cout<<"\nNow I'm in FunctTwo()!\nmay do some work here..."

              <<"\nReceives nothing but return something"

              <<"\nto the calling function..."<<endl;

       // and return something...

       return p;

}

 

int FunctThree(int z)

{

       // receive something...do some work and return the something...

       int a = z + 100;

       cout<<"\nThen, in FunctThree()!..."<<endl;

       cout<<"Receive something from calling function\ndo some work here and"

              <<"\nreturn something to the calling function...\n"<<endl;

       // then return to main, with return value

       return a;

}

 

void FunctFour(int s)

{

       // received something but return nothing...

       int r = s - 20;

       cout<<"\nNow, in FunctFour()..."<<endl;

       cout<<"Received something, but return nothing..."<<endl;

       cout<<"The value processed = "<<r<<endl;

       // return;   optionally can put this empty 'return;'

}

 

Output:

 

C C++ function pass by value complete example

 

Example #2

 

// demonstrates the use of function prototypes

#include <iostream>

 using namespace std;

 

// another method simplifying type identifier using typedef

// the words unsigned short is simplified to USHORT

typedef unsigned short USHORT;

// a function prototype

USHORT FindTheArea(USHORT length, USHORT width); 

 

int main()

{

                   USHORT lengthOfYard;

                   USHORT widthOfYard;

                   USHORT areaOfYard;

                    cout<< "\nThe wide of your yard(meter)? ";

                    cin>> widthOfYard;

                    cout<< "\nThe long of your yard(meter)? ";

                    cin>> lengthOfYard;

                    areaOfYard = FindTheArea(lengthOfYard, widthOfYard);

                    cout<< "\nYour yard is ";

                    cout<< areaOfYard;

                    cout<< " square meter\n\n";

                    return 0;

}

 

USHORT FindTheArea(USHORT l, USHORT w)

{

   return (l * w);

}

 

Output:

 

C/C++ function prototype

 

Example #3    Function Prototype code snippet

 

 // returns long, has two parameters
long   FindArea(long length, long width);
// returns void, has one parameter
void   PrintMessage(int messageNumber);
// returns int, has no parameters
int    GetChoice();

// returns char, has no parameters

char    BadFunction();

 

Example #4    Function Definition code snippet

 

long Area(long l, long w)
{
     return (l * w);
}
 
void   PrintMessage(int  whichMsg)
{
     if (whichMsg == 0)
          cout << "Hello.\n";
     if (whichMsg == 1)
          cout << "Goodbye.\n";
     if (whichMsg > 1)
          cout << "I'm confused.\n";
}

 

Example #5    The use of local variable and parameters

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

#include <iostream>

    using namespace std;

 
// a function prototype
float  Convert(float);
 
int  main()
{
  	float  TempFer;
  	float  TempCel;
  	cout<<"Please enter the temperature in Fahrenheit: ";
  	cin>>TempFer;
  	TempCel = Convert(TempFer);
  	cout<<"\n";
  	cout<<TempFer<<" Fahrenheit = "<<TempCel<<" Celcius"<<endl;
  	return 0;
}
 
// a function definition
float  Convert(float  TempFer)
{
  	// local variable....
  	float   TempCel;
  	TempCel = ((TempFer - 32) * 5) / 9;
  	// return the result to the calling program
  	return TempCel;
}

 

Output:

 

C/C++ function return value

 

Example #6    Demonstrating local and global variable

 

#include <iostream>

    using namespace std;

 
// a function prototype
void  myFunction();
// a global scope variables
int  x = 5, y = 7;
 
int  main()
{
  	cout<<"x = 5, y = 7, global scope\n";
  	cout<<"\nx within main: "<<x<<"\n";
 	 cout<<"y within main: "<<y<<"\n\n";
  	cout<<"Then function call....\n";
  	myFunction();
  	cout<< "Back from myFunction...\n\n";
  	cout<< "x within main again: "<<x<<"\n";
  	cout<< "y within main again: "<<y<<"\n\n";
  	return 0;
}
 
void  myFunction()
{
  	// a local scope variable
  	int  y = 10;
  	cout<<"\ny = 10, local scope\n"<<"\n";
  	cout<<"x within myFunction: "<<x<<"\n";
  	cout<<"y within myFunction: "<<y<<"\n\n";
}

 

Output:

 

C/C++ function global and local variable

 

Example #7     Variable scope within a block

 

// demonstrates variables scope within a block

#include <iostream>

    using namespace std;

 

// a function prototype

void myFunc();

 

int main()

{

          int x = 5;

          cout<<"\nIn main x is: "<<x;

          myFunc();

          cout<<"\nBack in main, x is: "<<x<<endl;

          return 0;

}

 

void myFunc()

{

        // a local scope variable

        int x = 8;

        cout<<"\nWithin myFunc, local x: "<<x<<endl;

        {

             cout<<"\nWithin block in myFunc, x is: "<<x;

             // another local variable, within block

             int x = 9;

             cout<<"\nVery local x: "<<x;

        }

        cout<<"\nOut of block, in myFunc, x: "<<x<<endl;

}

 

Output:

 

C/C++ function global and local variable

 

Example #8

 

// demonstrates passing by value to a function

#include <iostream>

    using namespace std;

 

// a function prototype

void  swap(int x, int y);

 

int main()

{

      int  x = 5, y = 10;

      cout<<"In main. Before swap, x: "<<x<<" y: "<<y<<"\n";

      cout<<"\nThen calling function swap(x, y)...\n";

      swap(x, y);

      cout<<"\n...back to main. After swap, x: "<<x<<" y: "<<y<<"\n";

      return 0;

}

 

void swap(int x, int y)

{

        int  temp;

        cout<<"\nIn swap function, confirm before swapping, x: "<<x<<" y: "<< y << "\n";

        temp = x;

        x = y;

        y = temp;

        cout<<"In swap function. After swapping, x: "<<x<<" y: "<<y<<"\n";

}

 

Output:

 

C/C++ function passing by value

 

Example #9

 

// demonstrates multiple return statements
#include <iostream>

    using namespace std;

 
// a function prototype
long int Doubler(long int AmountToDouble);
 
long int main()
{
  	long int result = 0;
  	long int input;
  	cout<<"Enter a number to be doubled: ";
  	cin>>input;
  	cout<<"\nBefore Doubler() is called... ";
  	cout<<"\ninput: "<<input<<" doubled: "<<result<<"\n";
  	result = Doubler(input);
  	cout<<"\nBack from Doubler()...\n";
  	cout<<"\ninput: " <<input<< "   doubled: "<<result<<"\n";
  	cout<<"Re run this program, input > 10000, see the output...\n";
  	return 0;
}
 
long int Doubler(long int original)
{
if (original <= 10000)
  return (original * 2);
else
{
  cout<<"Key in less than 10000 please!\n";
  return -1;
}
}

 

Output:

 

C/C++ function with multiple choice of return value

 

 
Example #10

 

// demonstrates the use of default parameter values
#include <iostream>

    using namespace std;

 
// a function prototype, width = 25 and height = 1, are default values
int AreaOfCube(int length, int width = 25, int height = 1);
 
int main()
{
  	// assigning new values
 	int length = 100;
 	int width = 50;
 	int height = 2;
  	int area;
  	area = AreaOfCube(length, width, height);
  	cout<<"First time function call, area = "<<area<<"\n";
  	area = AreaOfCube(length, width);
  	// height = 1, default value
  	cout<<"Second time function call, area = "<<area<<"\n";
  	area = AreaOfCube(length);
  	// width = 25, height = 1, default values
  	cout<<"Third time function call, area = "<<area<<"\n";
	return 0;
}
 
AreaOfCube(int length, int width, int height)
{	return (length * width * height);	}

 

Output:

 

C/C++ function default parameter value

 

Example #11

 

// demonstrates an inline functions
#include <iostream>

    using namespace std;

 
// an inline function doesn't need prototype here
// directly declares and defines the function
inline int Doubler(int target)
{   return (2*target);  }
 
int main()
{
	int target;
	cout<<"Enter a number to work with: ";
	cin>>target;
	cout<<"\n";
	target = Doubler(target);
	cout<<"First time function call, Target: "<<target<<endl;
	target = Doubler(target);
	cout<<"Second time function call, Target: "<<target<<endl;
	target = Doubler(target);
	cout<<"Third time function call, Target: "<<target<<endl;
	return 0;
}

 

Output:

 

C/C++ inline function

 

Example #12

 

// a simple random number function generator...

#include <iostream>

 using namespace std;

#include <cdos>

 

int main()

{

       int i;

       cout<<"Ten random numbers from 0 to 99\n\n";

       for(i=0; i<10; i++)

       {

              // a random number function generator

              cout<<rand()%100<<" ";

              // let have 2 seconds delay...

              sleep(2);

       }

       cout<<endl;

       return 0;

}

 

Output:

 

C/C++ function random number generator

 

C & C++ programming tutorials

 

 

 

 

 

 

 

 

 

 

 

 

C and C++ further readings:

 

  1. Check the best selling C / C++ books at Amazon.com.
  2. For this Module purpose, you can check the standard libraries of these various standards of C / C++.  Explore and compare the standard functions and their variation if any in the libraries.  You can download or read online the specification at the following links. (ISO/IEC is covering ANSI and is more general):
  1. ISO/IEC 9899 (ISO/IEC 9899:1999) - C Programming languages.
  2. ISO/IEC 9945:2002 POSIX standard.
  3. ISO/IEC 14882:1998 on the programming language C++.
  4. ISO/IEC 9945:2003, The Single UNIX Specification, Version 3.
  5. Get the GNU C library information here.
  6. Read online the GNU C library here.

 

 

 

 

 

 

|< C & C++ Functions 2 | Main | C & C++ Functions 4 >| Site Index | Download |


C and C++ Functions:  Part 1 | Part 2 | Part 3 | Part 4 |