Overview of C part 3 (functions, storage classifications)

advertisement
clark
cs1713
syllabus
lecture notes
programming assignments
Functions
In C, functions are used to modularize coding. A function has the following
syntax:
dataType functionName (parameterList)
{
functionBodyStatements
return expression;
}
If the function doesn’t functionally return a value, we specify
void functionName (parameterList)
{
functionBodyStatements
}
homework
set up
double calculateAverage(double dScoreM[], int iScoreCount)
{
int i;
double dSum = 0.0;
if (iScoreCount <= 0)
return 0.0;
for (i = 0; i < iScoreCount; i++)
{
dSum += dScoreM[i];
}
return dSum / iScoreCount;
}
void showScores(char *pszName, double dScoreM[]
, int iScoreCount)
{
int i;
printf("Scores for %s\n", pszName);
for (i = 0; i < iScoreCount; i++)
{
printf("%3lf\n", dScoreM[i]);
}
}
void showScores(char szName[], double dScoreM[]
, int iScoreCount)
{
int i;
printf("Scores for %s\n", szName);
for (i = 0; i < iScoreCount; i++)
{
printf("%3lf\n", dScoreM[i]);
}
}
Parameter passing:
 To change the value of a parameter to return it through the
parameter list, an address must be passed.
 Numeric variables are passed by value. To pass the address:
// prototype declaration so
// determineMinMax needs as
// returns
void determineMinMax(double
, double *pdMin, double
that the compiler knows what the
parameters and what it functionally
dScoreM[], int iScoreCount
*pdMax);
o

In the calling function, you must precede the variable name
with a & reference operator.
o In the invoked function, the parameter must be declared to be
pointer to receive the address. When referencing the value, it
must be dereferenced using a *
Arrays pass the address.
int main()
{
double dMinimum;
double dMaximum;
char szName[] = "Marcus Absent";
double dScoreM[] = { 40, 0, 30, 55, 0, 25 };
int iScoreCount = 6;
determineMinMax(dScoreM, iScoreCount, &dMinimum, &dMaximum);
printf("For %s, minimum = %lf and maximum = %lf"
, szName, dMinimum, dMaximum);
return 0;
}
void determineMinMax(double dScoreM[], int iScoreCount
, double *pdMin, double *pdMax)
{
int i;
*pdMin = 200.0; // arbitrary high value
*pdMax = 0.0;
for (i = 0; i < iScoreCount; i++)
{
if (dScoreM[i] < *pdMin)
*pdMin = dScoreM[i];
if (dScoreM[i] > *pdMax)
*pdMax = dScoreM[i];
}
}

Structures pass a copy of the structure. To change the values and have
those changes known in the caller, you must pass the address of the
structure.
o To pass the address, you must precede the variable name with
a &.
o In the invoked function, the parameter must be declared to be
pointer to receive the address.
o When referencing the attributes within the structure, you
must use pointer notation (->).
// typedefs and prototypes
typedef struct
{
char
szName[31];
double dScoreM[50];
int
iScoreCount;
} StudentScores;
void determineMinMax(StudentScores *pstudentScores, double *pdMin
, double *pdMax);
int main()
{
double dMinimum;
double dMaximum;
StudentScores studentOne = { "Marcus Absent"
, { 40, 0, 30, 55, 0, 25 }
, 6};
studentOne.iScoreCount =6;
determineMinMax(&studentOne, &dMinimum, &dMaximum);
printf("For %s, minimum = %ld and maximum = %ld"
, studentOne.szName, dMinimum, dMaximum);
return 0;
}
void determineMinMax(StudentScores *pstudentScores, double *pdMin,
double *pdMax)
{
int i;
*pdMin = 200.0; // arbitrary high value
*pdMax = 0.0;
for (i = 0; i < pstudentScores->iScoreCount; i++)
{
if (pstudentScores->dScoreM[i] < *pdMin)
*pdMin = pstudentScores->dScoreM[i];
if (pstudentScores->dScoreM[i] > *pdMax)
*pdMax = pstudentScores->dScoreM[i];
}
}
Exercise
Create the function, calculateTotalCost, which is passed two arrays and a
count of the number of entries. Does it need another parameter?
dUnitPriceM[] - each entry represents a price per unit
lOrderQuantiyM[] - each entry represents an order quanity
As a functional value, it should return the total cost which is the sum of the
cost (unit price * order quantity) of all of the entries.
// Code in the caller
int main()
{
double dUnitPriceM[]={19.99, 50.50, 2.10};
long lOrderQuantityM[] = {10, 2, 4};
int iItemCount = 3;
double dTotalCost;
dTotalCost = calculateTotalCost(dUnitPriceM
, lOrderQuantityM, iItemCount);
printf("Total cost is %10.2lf\n", dTotalCost);
}
// code for calculateTotalCost function ??
double calculateTotalCost(double dUnitPriceM[], long lQuantityM[]
, int iItemCount)
{
}
Exercise
Same as previous exercise, but return that total cost as a parameter instead of
the functional value.
// the call of calculateTotalCost is different
calculateTotalCost(dUnitPriceM
, lOrderQuantityM, iItemCount, &dTotalCost);
printf("Total cost is %10.2lf\n", dTotalCost);
// code for calculateTotalCost function ??
void calculateTotalCost(double dUnitPriceM[], long lQuantityM[]
, int iItemCount, double *pdTotalCost)
{
}
Exercise
Show code for the function findStudentById which is passed a array of Student
structures, a count of the number of students, and a student id. It finds the
specified student ID in the array and returns its subscript. If not found, it
returns -1.
typedef struct
{
char szStudentId[7];
char szMajor[4];
char szClassification[3]; // one of FR, SO, JR, SR
} Student;
Student studentM[100];
int iStudentCnt;
int iLoc;
char szId[7];
// assume studentM and iStudentCnt have been populated
// assume szId has been assigned an Id to find.
iLoc = findStudentById(studentM, iStudentCnt, szId);
// code for findStudentById
Download