Top Banner
A First Book of ANSI C, Fourth Edition 1 Functions for Modularity 04/24/15
26

A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

Jan 04, 2016

Download

Documents

Elwin Stephens
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Functions for Modularity

04/24/15

Page 2: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Final Exam

Friday, May 8th, 12:30-2:30 Accumlative Emphasis on Loops and Functions, Chapters 5 &

6 You may bring a half sheet of notes to the exam.

Page 3: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Programs

Program 6 is graded. Program 9 is page 297, #4 a&b.

– Extra point for screen shot of evaluation proof.

Page 4: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Objectives

• Use Function and Parameter Declarations.

• Return a Value from a Function.

Page 5: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Functions

• Like small programs within the program.• Help to divide the program up to make it easier to

write and debug.– Modularity

• Make it easier to reuse code in new programs.• Example in half.c

Page 6: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Function Call Called function

A function that is called into action by its reference in another function.

Calling function A function that calls another function main() calls half() in half.c

Page 7: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Function Call May Pass Data

Page 8: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Book Example

• Data Passed is receive by a parameter.• A parameter otherwise acts like a variable in the

function.– void findMax(float x, float y)

• prog6.1.c

Page 9: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Function Definition

• Function header: data type of the return value, function name, and values expected by the function

• Function body: operates on the passed data and returns, at most, one value

• The variable names in the header line are known as parameters

• prog6.1.c

Page 10: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Function Definition

Page 11: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Function Parts

void findMax(float x, float y) //Header w/parameters{ //Body starts float maxnum;

if(x >= y) /* find the maximum number */ maxnum = x; else maxnum = y;

printf("\nThe maximum is %f\n", maxnum);}//Body ends

Page 12: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

No Function Nesting

• main() is C function• Each C function is a separate and independent

entity with its own parameters and variables– Nested functions are not permitted

Page 13: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Placement of Statements

• All preprocessor directives, variables, named constants, and functions, except main(), must be either declared or defined before they can be used

• Basic (good) programming structure:preprocessor directivessymbolic constantsfunction prototypes can be placed hereint main(){ function prototypes can be placed here variable declarations; other executable statements; return value;}Function definitions

Page 14: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Write a Function

• Write a C function that accepts a number then outputs a message telling whether it is even or odd.

• Write a main program to input a number then use the function to tell whether it is even or odd.

Page 15: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Participation 1

Write a function mult() that takes two numbers and

prints out their product.

Page 16: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Returning a Value

Page 17: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Returning a Value

• Sometimes you want the function to find a value and send it back to the main program.

• Use the return statement to do that.

Page 18: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Returning a Value

• From its side of the return transaction, the called function must provide:– Data type of the returned value, which is specified in

the function’s header line– Actual value being returned, which is specified by a

return statement

Page 19: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Returning a Value (continue)

Page 20: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Returning a Value (continue)

• To return a value, use a return statement– return (expression); //or, return expression;

– The expression is evaluated first; – Its value is automatically converted to the return

value’s data type.• Unmatched types can cause unexpected results

– Returned to calling function.

Page 21: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Example

• ch6/height.c

Page 22: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Write a function

• Write a function that takes the base and height of a triangle and returns the area.

• Include the function in a working program.

Page 23: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Another Example

• Write a function that raises an integer to a positive integer power. It will return the result.

Page 24: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Participation 2

• Write a C function that takes two mile marker

readings and returns the distance between them.

• Write a main program to ask the user for two mile

marker values, then use the function to compute

the distance between and output it.

Page 25: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Functions with Empty Parameter Lists

• The prototype for a function with empty parameter list requires either writing the keyword void or nothing between the parentheses following the function’s name– int display(void);– int display();

• A function with an empty parameter list is called by its name with nothing written in the parentheses following the function’s name– display();

Page 26: A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.

A First Book of ANSI C, Fourth Edition 1

Example

• ch6/house.c