chapter09 - COP 3275 Chapter 09 Jonathan C.L Liu Ph.D CISE...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
COP 3275: Chapter 09 Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Introduction A function is a series of statements that have been grouped together and given a name. Each function is essentially a small program, with its own declarations and statements. Advantages of functions: A program can be divided into small pieces that are easier to understand and modify. We can avoid duplicating code that’s used more than once. A function that was originally part of one program can be reused in other programs. 2
Background image of page 2
Defining and Calling Functions Before we go over the formal rules for defining a function, let’s look at three simple programs that define functions. 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Program: Computing Averages A function named average that computes the average of two double values: double average(double a, double b) { return (a + b) / 2; } The word double at the beginning is the return type of average . The identifiers a and b (the function’s parameters ) represent the numbers that will be supplied when average is called. 4
Background image of page 4
Program: Computing Averages Every function has an executable part, called the body, which is enclosed in braces. The body of average consists of a single return statement. Executing this statement causes the function to “return” to the place from which it was called; the value of (a + b) / 2 will be the value returned by the function. 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Program: Computing Averages A function call consists of a function name followed by a list of arguments. average(x, y) is a call of the average function. Arguments are used to supply information to a function. The call average(x, y) causes the values of x and y to be copied into the parameters a and b . An argument doesn’t have to be a variable; any expression of a compatible type will do. average(5.1, 8.9) and average(x/2, y/3) 6
Background image of page 6
Program: Computing Averages We’ll put the call of average in the place where we need to use the return value. A statement that prints the average of x and y : printf("Average: %g\n", average(x, y)); The return value of average isn’t saved; the program prints it and then discards it. If we had needed the return value later in the program, we could have captured it in a variable: avg = average(x, y); 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Program: Computing Averages The average.c program reads three numbers and uses the average function to compute their averages, one pair at a time: Enter three numbers: 3.5 9.6 10.2 Average of 3.5 and 9.6: 6.55 Average of 9.6 and 10.2: 9.9 Average of 3.5 and 10.2: 6.85 8
Background image of page 8
average.c /* Computes pairwise averages of three numbers */ #include <stdio.h> double average(double a, double b) { return (a + b) / 2; } int main(void) { double x, y, z; printf("Enter three numbers: "); scanf("%lf%lf%lf", &x, &y, &z); printf("Average of %g and %g: %g\n", x, y, average(x, y)); printf("Average of %g and %g: %g\n", y, z, average(y, z)); printf("Average of %g and %g: %g\n", x, z, average(x, z)); return 0; } 9
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Program: Printing a Countdown To indicate that a function has no return value, we specify that its return type is
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/30/2011 for the course COP 3275 taught by Professor Jonathanliu during the Fall '11 term at University of Florida.

Page1 / 48

chapter09 - COP 3275 Chapter 09 Jonathan C.L Liu Ph.D CISE...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online