Академический Документы
Профессиональный Документы
Культура Документы
1 / 24 3 / 24
2 / 24 4 / 24
Function Call and Return Statements Procedure: Multiple Function Output
5 / 24 7 / 24
Useful for complex printing tasks Solution #2: Get a procedure to do it. Does this work?
#include <stdio.h>
#include <stdio.h> void splitTime(int t, int h, int m, int s);
void printRowOfStars(int n); void printRowOfStars(int n) { int main(void) {
void printPattern(int n); if (n == 0) { int t, h, m, s;
printf("\n");
int main(void) { } else { printf("Enter duration (secs): ");
int n; printf("*"); scanf("%d", &t);
scanf("%d", &n); printRowOfStars(n - 1); splitTime(t, h, m, s);
printPattern(n); } printf("Duration: %d:%d:%d\n", h, m, s);
return; return 0;
return 0; } }
} /*
void printPattern(int n) { void splitTime(int t, int h, int m, int s) {
if (n > 0) { h = t / 3600;
printRowOfStars(n); m = (t % 3600) / 60;
printPattern(n - 1); s = t % 60;
} return;
return; }
6 / 24 8 / 24
Pass-by-Value Revisited Variable Access Across Functions
Recall pass-by-value To enable function output via the parameters, use pointers
to store address values (e.g. scanf)
main splitTime
int main(void) {
9999 t 9999 t int t, h, m, s;
?a h
*
?a h printf("Enter duration (secs): ");
?b m
9999,?a,?b,?c
?b m
scanf("%d", &t);
?c s ?c s
splitTime(t, &h, &m, &s);
splitTime(t,h,m,s);
printf("Duration: %d:%d:%d\n", h, m, s);
return 0;
}
How to define parameters of the splitTime function?
9 / 24 11 / 24
main splitTime
void func-id ( void ) func-body
9999 t 9999 t
?a h 2 h type parameter-decl
*
9999,?a,?b,?c
?b m 46 m ,
?c s 39 s
parameter-decl
splitTime(t,h,m,s);
input-parameter-decl
type var-id
output-parameter-decl
What happens to the variables in the main function? type * var-id
10 / 24 12 / 24
Function Output Parameter Function Output Parameter
Declared as a pointer in the function header Just before splitTime function returns
Accessed via dereferencing in the function body
main splitTime
/* t 9999 9999 t
Function splitTime takes t in seconds, splits and
h 2 h
outputs the hours, minutes and seconds through the
output parameters h, m and s. m 46 m
Precondition: t >= 0 s 39 s
*/
void splitTime(int t, int *h, int *m, int *s) { splitTime(t,&h,&m,&s); -
*h = t / 3600; 9999,&h,&m,&s
*m = (t % 3600) / 60;
*s = t % 60; Values are “returned” through the function output
return; parameters; does not violate lexical scoping
}
13 / 24 15 / 24
Calling splitTime with function output parameters Using main function to swap the contents of two variables
14 / 24 16 / 24
Example: Swapping Variable Contents Example: Calculate Taxi Fare (Revisited)
Using a swap function Calculate the taxi fare based on the following fare structure:
– Know what a function does, not how it is done scanf("%d%d", &type, &dist);
fare = computeFare(type, dist);
printFare(fare);
Structured programming return 0;
}
– Programs having a logical structure that makes them
int computeFare(int type, int dist) {
easy to read, understand and modify return 1022;
}
– Adopt a top-down modular design methodology:
void printFare(int fare) {
⊲ Start with the “large” task and break into sub-tasks printf("$%d.%d0\n", fare / 100, (fare % 100) / 10);
return;
⊲ Break these sub-tasks into smaller sub-tasks until the }
smallest ones have trivial solutions Since computeFare might require further decomposition,
⊲ Compose all sub-tasks to solve the original problem replace with a “stub”, so that program can still be tested
18 / 24 20 / 24
Breaking down computeFare Principles in Modular Design
int computeFare(int type, int dist) {
int fare; Abstraction
if (dist == 0) {
fare = 0; – Know what it does but don’t care how it is done
} else if (dist <= 1000) {
fare = stage1Fare(type); Reusability
} else if (dist <= 10000) {
fare = stage1Fare(type) + stage2Fare(type, dist-1000); – Identify modules that can be used repeatedly
} else {
fare = stage1Fare(type) + stage2Fare(type, 9000) + – One function definition; many calls to the same function
stage3Fare(type, dist-10000);
} High cohesion
return fare;
} – Do only one thing, and do it well
Breakdown computeFare into fare stages via stubs Loose coupling
int stage1Fare(int type) { int stage2Fare(int type, int stage3Fare(int type,
– Minimize the use of “output parameters” that allows one
printf("stage1\n");
return 0;
int dist) {
printf("stage2\n");
int dist) {
printf("stage3\n"); module to directly change properties (variables) of
} return 0; return 0;
} } another module
21 / 24 23 / 24