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 andlab worksheet 4.
// 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 FunctTwowithout any argument...
double q = FunctTwo();
// display the returned value...
cout<<"The returned value = "<<q<<endl;
// function call, go to FunctThreewith 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;'
}

// 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);
}

// returns long, has two parameterslong 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();
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";}
#include <iostream>using namespace std;
// a function prototypefloat 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 definitionfloat Convert(float TempFer){ // local variable.... float TempCel; TempCel = ((TempFer - 32) * 5) / 9; // return the result to the calling program return TempCel;}

#include <iostream>using namespace std;
// a function prototypevoid myFunction();// a global scope variablesint 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";}

// 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;
}

// 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";
}

// demonstrates multiple return statements#include <iostream>using namespace std; // a function prototypelong 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:
|
// demonstrates the use of default parameter values#include <iostream>using namespace std;
// a function prototype, width = 25 and height = 1, are default valuesint 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); }

// demonstrates an inline functions#include <iostream>using namespace std;
// an inline function doesn't need prototype here// directly declares and defines the functioninline 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;}

// 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;
}

- ISO/IEC 9899 (ISO/IEC 9899:1990) - C Programming languages.
- ISO/IEC 9899 (ISO/IEC 9899:2018) - C Programming languages.
- ISO/IEC 9945-1:2003 POSIX Part 1 Base Definition.
- ISO/IEC 14882:1998 on the programming language C++.
- Latest, ISO/IEC 14882:2017 on the programming language C++.
- ISO/IEC 9945:2018, The Single UNIX Specification, Version 4.
- Get the GNU C library information here.
- Read online the GNU C library here.