Transcript LECT20.PPT

Functions II
Functions that return a value
CMSC 104
1
A function that returns
a value
/***************************************************
** AverageTwo takes two integers arguments
** and returns their average as a float
****************************************************/
float AverageTwo (int num1, int num2)
{
float average;
average = (num1 + num2) / 2.0;
return average;
}
CMSC 104
2
Using AverageTwo
#include <stdio.h>
float AverageTwo (int num1, int num2);
main ( )
{
float average;
int num1 = 5, num2 = 8;
average = AverageTwo (num1, num2);
printf (“The average of %d and %d is %f\n”, num1, num2, average);
}
float AverageTwo (int num1, int num2)
{
float average;
average = (num1 + num2) / 2.0;
return average;
CMSC 104 }
3
Local Variables




CMSC 104
Functions only “see” their own local variable.
This includes main ( )
The variables that are passed to the function
are matched with the formal parameters in
the order they are passed
The parameters are declarations of local
variables. The values passed are assigned to
those variables
Other local variables can be declared within
the function
4
Local Variables
#include <stdio.h>
float AverageTwo (int num1, num2)
float AverageTwo (int num1, int num2); {
main ( )
float average;
{
float average;
average = (num1 + num2) / 2.0;
int num1 = 5, num2 = 8;
return average;
}
average = AverageTwo (num1,
num2);
printf (“The average of “);
printf (“%d and %d is %f\n”,
num1, num2, average);
}
num1
num2 average
num1
num2
average
CMSC 104
5
8
int
int
float
int
int
float
5
More about Local Variables
#include <stdio.h>
void AddOne (int num1);
main ()
{
int num1 = 5;
AddOne (num1);
printf (“In main: “);
printf (“num1 = %d\n”, num1);
}
num1
5
int
CMSC 104
void AddOne (int num1)
{
num1++;
printf (“In AddOne: “);
printf (“num1 = %d,num1);
}
num1
int
OUTPUT
In AddOne: num1 = 6
In main: num1 = 5
6
Data Types and
Conversion Specifiers
Data Type
float
double
long double
int
long int
unsigned int
unsigned long int
short int
char
CMSC 104
printf
conversion
%f
%f
%Lf
%d
%ld
%u
%lu
%hd
%c
scanf
conversion
%f
%lf
%Lf
%d
%ld
%u
%lu
%hd
%c
7
Header Files

Header files contain function prototypes for all
of the functions found in the corresponding
library

It also contains definitions of constants and
data types used in that library
CMSC 104
8
Commonly Used Header Files
header file Contains function prototypes for
<stdio.h>
the standard input/output library functions
& information used by them
<math.h>
the math library functions
<stdlib.h>
the conversion of number to text, text to
number, memory allocation, random
numbers and other utility functions
<time.h>
manipulating time and date
<ctype.h>
functions that test characters for certain
properties and that can convert case
others
see page 159 of text
CMSC 104
9
Math Library





CMSC 104
double sqrt (double x);
o returns the square root of x
double pow (double x, double y)
o x raised to the y power
 pow (3.0, 2.0) is 9.0
 pow (8.0, 0.33) is 2.0
double sin (double x)
o trigonometric sine of x (x in radians)
All math library functions take doubles as arguments and return
doubles
others on page 151 of text
10
Common stdlib functions



CMSC 104
void exit (int x);
o prematurely ends program execution
void srand (unsigned int x);
o “seeds” the random number generator with an unsigned
integer that is used to start the calculations that generate the
pseudo-random number
 srand (200);
int rand (void);
o returns an unsigned pseudo-random integer in the range of 0
to 65535 or 0 to 4294967295 depending on the size of an
integer on the system your on
o num = rand();
11
Manipulating what rand() returns




CMSC 104
Since rand() returns unsigned integers in a large
range, we often have to manipulate the return value
to suit our purposes
Suppose we want only random numbers in the range
from 0 to 5
o num = rand () % 6
How about 1 to 6?
o num = 1 + rand( ) % 6;
How about 5 to 20?
o num = 5 + rand ( ) % 16;
12
srand ( ) and rand ( )



CMSC 104
The pseudo-random number generator needs
an unsigned int as it’s seed
Although it produces what appear to be
random numbers, if we use the same seed,
we get the same sequence of random
numbers
To get different random numbers each time
we run our program, we have to give a
different seed each time
13
srand ( ) and rand ( )
#include <stdio.h>
include <stdlib.h>
#define SEED 67
main ( )
{
int i, num;
srand (SEED);
for (i = 0; i < 5; i++)
{
num = rand ( );
num = 1 + num % 6;
printf (“%d\n”, num);
}
CMSC 104
}
Since we are always
using the value 67
generator, the same
numbers will be
produced whenever we run our
program
14
<time.h>




One of the most useful functions in the time
library is the time() function
It returns the time of day as seconds
Since this number is different every time we
call it, it’s greatest use is as a seed for the
random number generator
Each time we run our program, a different
sequence of random numbers will be
produced

CMSC 104
srand (time ( NULL) );
15