> I am soo not good with functions.. I read lots but don't quite get it..
That is what you need to get first. See if this helps; it may be a different way of looking at functions from what you have read so far.
A function is conceptually a set of pairs - the first component of each pair is an input value and the second component is the associated output value. Note: In mathemaics, what we call functions in C++ are called relations; an input is related to its associated output.
For example, the following set of pairs is conceptually defines a function:
F = { (1,2), (3,4), (5,6), (7,8) }. |
The domain of the function is the set of all possible input values; in the example above the domain is:
.
The range of a function is the set of all possible output values; in the example above the range is:
.
We could write the above function as
y = F(x)
where
x is an input value in the domain and
y is the associated output or result value (which is in the range)
Or equivalently,
F(x) = x+1 for x in { 1, 3, 5, 7 }
.
In this example, the domain is a set of four integers, and so is the range. In C++, we would write the above function as:
int F( int x ) ;
It takes an integer as input and returns the associated integer as output.
So we could now write:
1 2
|
int input = 5 ;
int output = F(input) ;
| |
When we call the function, we need to pass the input value and the result of the call is the associated output value. In the above example, the input value is the integer
5 and the corresponding output value is
6. The caller of the function supplies the input to the function when it is called, and the function returns the output associated with the input that was passed to it.
int F( int x ) ;
only declares the function. To use it we must also define it.
1 2 3 4 5 6
|
int F( int x )
{
int y ; // output or result ;
// map the input x to the associated output and place it in y
return y ; // return the output/result to the caller
}
| |
In this case, mapping input to output is easy:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
int F( int x )
{
int y ;
if( x == 1 ) y = 2 ;
else if( x == 3 ) y = 4 ;
else if( x == 5 ) y = 6 ;
else if( x == 7 ) y = 8 ;
else
{
// there is an error made by the caller of the function
// input is not in the domain of the function
}
return y ; // return the result to the caller
}
| |
Alternatively,
1 2 3 4 5 6 7 8 9 10 11
|
int F( int x )
{
int y ;
if( x==1 || x==3 || x==5 || x==7 ) y = x+1 ;
else
{
// there is an error made by the caller of the function
// input is not in the domain of the function
}
return y ; // return the result to the caller
}
| |
If we write
1 2 3 4 5
|
int F2( int x )
{
int y = x+1 ;
return y ;
}
| |
The domain and the range of the function
F2 is the set of all possible values that an int can hold; it still maps an input to an output.
For the function
bool is_odd( int x ) { return x%2 == 1 ; }
the domain is the the set of all possible values that an int can hold; the range is
{ true, false }
(the values that a bool can hold). The caller of the function must pass an integer as the input eg. is_odd(23) and we return the output as the result.
What you need to do for this exercise is
Write a function where
a. the input is a double value, say
x - a temperature in Celsius
b. the output is also double value, say
y - the associated temperature in Fahrenheit
c. the two are related by
y = 1.8 * x + 32.0
for example,
double to_fahrenheit( double x ) ;
Then write main which will accept the input value from the user, call the function passing the input value to it, and then print out the result which it returns.
Note: In the simple examples above, the input is a single value. It is also possible to write functions where the input itself is a set of values. For example, in
double function( int a, int b, int c ) ;
the input is a set of three integers.
We'll cross that bridge when we come to it.