Functions S10
Functions S10
C ++ Function Definition:
A module or function in C++ performs a specific task.
-It is a set of C++ statements.
-It accepts inputs
-Processes it and provides a single output.
Advantages of using functions:
Modular way for program design makes it easy to:
Maintain since we know which part of the program
performs which task.
double a= sqrt(900.0);
Function definition:
return_type function_name (data_type var1,
data_type va2, ..)
{
function_body
}
function_name
-The function name should be a valid C++ identifier.
-The name should be straightforward and describe what the
function does.
-If you cannot find a suitable name for your function, then
maybe its task is not well defined.
For example, a function that calculates power three of an
integer will be called power_three.
(data_type var1, data_type va2, ..):
-These are the arguments or parameters of the function. They
are of the form:
datatype variable_name,
for example: int a, float b
- They describe the input data that the function uses to perform
the task.
-Each argument should have its corresponding datatype.
{..}
-A function is defined in a block.
-The beginning and ending of a block are noted with braces.
An opening brace { for the beginning of the block, and a
closing brace } for the end of the block.
function_body
A set of C++ declarations and instructions that perform the
task.
-It contains the declaration for the variables that the function
uses.
-It contains the statements that do the work.
The power_three functions body is:
int y = pow(number,3);
return y;
}/*end function */
int y = pow(number,3);
return y;
}/*end function */
Variables in functions:
Lets consider the statement:
int y = pow(number,3);
For example:
int find_max (int a, int b)
{
if ( a > b)
return a;
else return b;
}
int main(void) {
int y;
y=find_max(3,5);
cout<<y;
return 0;}
Calling a function:
Once the function has been defined, how do we use it?
In order to use a function, we invoke it or call it.
At the call f(a) copy of the value of a is made and used in the
function call.
Value of a
Before function call 2
After function call 2
Value of copy of
a
2
3
Changes are made to the copy and do not effect the value of the
caller variable.
Call by value should be used whenever the function does not
need to modify the value of the callers original variables.
However, you may not apply long to float because long has the
same meaning as double.
-Applying the unsigned modifier to floating-point types (float,
double) is not recommended.
Type
char
unsigned char
signed char
int
unsigned int
signed int
short int
unsigned short int
signed short integer
long int
Size in
bytes(bits)
1 (8)
1 (8)
1 (8)
2 (16)
2 (16)
2 (16)
2(16)
2 (16)
2 (16)
4 (32)
4 (32)
4 (32)
4 (32)
8 (64)
16 (128)
Range
-127 to 127
0 - 255
-127 to 127
-32,767 to 32,767
0 - 65,535
Same as int
Same as int
0 65,535
Same as int
-2,147,483,647 to
2,147,483,647
Same as long int
0 to 4,294,967,295
Six digits of precision
Ten digits of precision
Ten digits of precision
Type conversion
In library function calls:
If a library function is called with an argument of a different
type than the one it has been defined with, it converts the
arguments into the data type of its definition, this is called
coercion of arguments.
Note that:
Converting from a higher precision to a lower precision results
in a loss of precision.
Converting from lower precision to a higher precision does not
result is more accuracy rather it is a different representation.
Original type
int
long int
long int
float
Target type
char
char
int
int
double
float
long double
double
Loss
High order 8-bits
High order 24 bits
High order 16 bits
Fractional part or
more
Precision, result
rounded
Precision, result
rounded
Symbolic Constants:
In C++, constants refer to fixed values that the program may
not alter.
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(void) {
cout<< RAND_MAX;
return 0;
}
Program output is:
32767.
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(void) {
int i;
for (i=1; i<=20; i++) {
cout<<\t<<1+ (rand() % 6)<<\t;
if (i%5 == 0)
coutn<<endl;
} /*end for */
return 0;
}/*end main */
Sample output:
5
#include <iostream>
#include <stdlib.h>
using namespace std;
main() {
int face,roll, frequency1=0, frequency2=0,
frequency3=0, frequency4=0,
frequency5=0, frequency6=0;
for (roll=1; roll<=6000; roll++) {
face = 1+ rand() % 6;
switch (face) {
case 1: ++frequency1;break;
case 2:++frequency2;break;
case 3:++frequency3; break;
cout<<
1 << frequency1<<endl;
cout<<
cout<< "
cout<< "
Frequency
987
984
1029
975
1003
1022
Game of craps:
A player rolls two dice.
After the dice have come to rest, the sum of the two
upward faces is calculated.
If the sum is 7 or 11, the player wins.
If the sum is 2, 3, or 12, the player loses.
If the sum is any other number, this becomes the players
point.
To win, the player must keep rolling until they make their
points, that is score the same point again in a roll of dice.
If the player rolls a 7 before making the point, they loose.
Output:
Game won, game lost, or play again:
-we need a variable that will take 3 different values depending
on the game status.
Game_status =1 if game is won,
Game_status = 2 if game is lost
Game_status = 0 if game is still on play again
We need a variable that will store the sum of points at each
throw
We need a variable that will store the player score in case the
player needs to play again to match this score.
Pseudocode:
Simulate dice throw
Calculate sum
If sum = 7 or sum =11
Then
Game_status = 1 (Game is won)
Else If sum = 2 or sum =3, or sum =12
Then
Game_status= 2, (Game is lost),
Automatic
External
Static
Automatic:
Variables defined within the function body are called automatic
variables. Auto is the keyword used to declare automatic
variables. By default and without the use of a keyword, the
variables defined inside a function are automatic variables.
For instance:
void exforsys( )
{
auto int x;
auto float y;
is same as
void exforsys( )
{
int x;
float y;
//Automatic Variables
Static:
The static automatic variables, as with local variables, are
accessible only within the function in which it is defined. Static
automatic variables exist until the program ends in the same
manner as external variables. A static variable is initialized
only once, when the function in which it has been defined is
called for the first time, it maintains its value between function
calls.
For example:
#include <iostream.h>
int example(int);
void main( )
{
int in,out;
in =1;
while(in!=0)
{
cout<<Enter input value:;
cin>>in;
out=example(in);
cout<\nResult:<<out;
}
cout<\n End of Program<<out;
}
int example(int x)
{
static int a=0;
static int b=0;
a++;
b=b+x;
return(b/a);
}
input
value:5
input
value:7
input
value:0