Overview
The function itself takes the form:
void sqr(int number)
{
number *= number;
cout << number;
}
Notice there is no semi-colon on the first line of the function.
The easiest way of working is to first write the function, remember that there is no semi-colon. Then copy the function, to either the top of the program (underneath any #include or #define statements) or to the header file if one is to be used. Finally add a semi-colon to the prototype.
The function can then be called from the main program:
#include <iostream.h>
void main()
{
int number;
cout << "\nPlease enter a number\n";
cin >> number;
sqr(number);
}
It's worth pointing out that the example here won't even compile and the
comment to the left is misleading - number is the original number, not
a copy. This program does nothing either...
It is important that only a copy of number is sent to the function, and on returning from the function, the original variable is unchanged.
FURTHER FUNCTION PROTOTYPE EXAMPLES
A function called plot which has two int parameters x and y and returns an int:
int plot(int x, int y);
A function called statistics which has two int parameters and returns a float
float statistics(int, int);
A function called calculate which has 3 float parameters x y and z and returns a float
float calculate(float x, float y, float z);
The above function, that returns a float value, can be called like this
Excusing the incorrect return type for main...
void main()
{
float x,y,z,result;
x = 5.3;
y = 3.5;
z = 6.2;
result = calculate(x,y,z);
}
Only one variable can be returned by this method.
EXERCISE 8
- Write a program that asks the user to enter two integer numbers
- Write a function that takes two values, adds them together and returns an integer total. Print the total to the screen. >
- Write a further function that takes the total and calculates the square root by using the sqrt() function. You will need to include math.h at the top of the program in order to do this. Then return a float from your function. Again, print this to the screen
That should be cmath
DATA STORAGE CLASSES
auto
An auto variable is created when it is declared and is destroyed when it goes "out of scope".
A data member declared as auto in a class is created when it is declared inside a block.
void MyFunction(int value)
{
auto int value2; // not available outside this function
}
The auto keyword is not necessary and is rarely used.
static
If a local variable is required to retain its value between function calls, it must be declared as a static:
A static variable is created only when it is used for the first time:
void MyFunction()
{
static int value2 = 0; // can be changed but retains it's
value2++; // value between function calls.
}
The first time the function is called, value2 will have an initial value of 0, on leaving the function, it will have a value of 1.
The next time the function is called, value2 will have an intial value of 1.
const
The const specifier is used when a constant value is needed for a data item. Once declared, the compiler will not allow this value to be changed. Because the constant cannot be changed, it must be initialised in it's declaration.
const int PI = 3.1415927; // can not be changed from now on.
Any attempt to change a const will result in an error message from the compiler.
EXERCISE 9
A bank PIN number verification program.
- Working in main() declare an auto int variable called num to hold the PIN number to be input. Ask the user to input their PIN number and store it to the variable. Code a while loop around the input statements in such a way as to quit if a numeric 0 is entered.
- Now we start work on the verification part of the program. Code the start of the function. It is to take one integer argument, the variable num, and return nothing. Don't forget the function prototype.
- Inside the function declare a static int variable called count and initialise it to 3 in the same statement. This will store the number of tries. Also declare a const int variable called pin and initialise it to a PIN number of your choice.
- Craft suitable coding to test if the passed variable is equal to the stored PIN number in the function. If it is, print a suitable message to the screen and exit the program. This can be accomplished by the use of the exit(0) function. Remember to include stdlib.h to use this.
- Next test if the count variable is greater than 0 and if so, reduce it by one.
- Now test if the count variable has reached 0. If so, inform the customer that their card has been "eaten" and exit the program.
- Lastly place the instruction in a while loop in main() to call the function.
Besides it being cstdlib, why are the macros EXIT_SUCCESS and EXIT_FAILURE not used. They will always give the same results no matter which compiler and platform is in use. exit(0); may result in a program in terminating, the termination may be different for different platforms
Hmmm, taught in 2001 and still uses
void main()and#include <iostream.h>and omits thenamespaceto be used.This example is worse still as there are no forms of checking that the user hasn't entered a letter, sentence or a non-numerical character