If you have taken precalc, you are familiar with the term function. In math, a function takes a input and gives you an
output. A functions is basically a set of direction thats lead from the input to the output. For example, f(x) = 3x+1. The
function takes the input, x, and follows the directions given by 3x+1, and thus gives an output. For example, suppose we input
one. The function takes one, multiples by three, adds one and gives you the result, which in this case f(1) = 4.
In C++,
a function has a similar role. A function is a block of instructions that is executed when it is called from some other point
of the program. The following is its format:
type name ( argument1, argument2, ...)
{
statement
}
where:
*
type is the type of data returned by the function.
* name is the name by which it will be possible to call the function. The naming of a function
follows a similar convention to that of a variable. It cannot start with a number, it cannot contain special characters, etc
*
arguments (as many as wanted can be specified). Each argument consists of
a type of data followed by its identifier, like in a variable declaration (for example, int x) and which acts within the function
like any other variable. They allow passing parameters to the function when it is called. The different parameters are separated
by commas.
* statement is the function's body.
A function is declared and code outside of main. Thats because, main is also function.
For example:
#include <iostream.h>
#include <iomanip.h>
int function1(int a)
{
int r;
r = 3 * a + 1;
return r;
}
int main()
{
int x, y;
cout<<Please enter
a number <<endl;
cin>>y;
x = function1(y)
cout << "The result is " <<
x;
return 0;
}
function1 does the same thing as f(x)=3x+1 in math. It has 1 argument, int a. It first declares a local variable, int
r. A local variable is a variable that has been declared inside of a function. That means that it could only be used within
that function. If we use the variable r in main, for example, then the compiler will report that r has been undeclared in
main. A global variable is declared outside of a function at the top of the program, before all the functions but after the
includes. These variables can be used in all the functions of the program. We will get to this type of variable later.
function1
gives r the value of 3a+1. return r means that the function is outputting r. For example, if the argument passed into the
function (int a) had the value of 1, then r would be 4 and thus the function would output a value of 4.
In the main function,
two variables are declared. The user inputs into y. y is then passed into function1. Note that the value being passed in does
not have to be the same variable name as the argument name, since we see here that y and a are different variable names. x
is given the value of function1, or whatever function1 returned or outputted. Next, the program prints out x.
So suppose
the user inputted 3. y = 3. function1(y) = function(3) = 3(3)+1 = 10. x = 10. Thus, the output on the screen would be: The
result is 10.
A function in C++ can also accept two or more inputs.
For example:
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
double quadratic(int x, int y, int z)
{
double r;
r = (double)(-y + sqrt(y*y - 4*x*z))/2;
return r;
}
int main()
{
int a, b, c;
cout<<"Please enter
a, b, c"<<endl;
cin>>a>>b>>c;
cout << "The larger root of x is " <<
quadratic(a, b, c);
return 0;
}
Note that you may directly send the output of the function to cout,
inside of to x in the first example. Also note the double in front of quadratic(int x, int y, int z). That means that quadratic
returns a double, which is true, since r is a double and quadratic returns r. The function sqrt does what it sounds like.
It takes a number and gives you the square root of it. It is located in math.h, thus I have #include
<math.h> at the top. The (double) in r
= (double)(-y + sqrt(y*y - 4*x*z))/2; is called a cast. It forces r to be a double.
Otherwise, r will still be an integer. Try the code without the (double)
and see what happens.
A function also does not have to return anything and/or have arguments. The following function takes no arguments and
returns nothing (almost like main).
#include <iostream.h>
void function1()
{
cout << "This is a function";
}
int main ()
{
function1();
return 0;
}
void just means it returns nothing. This program just says: This is a function.
Now, we are ready for the first project.