A First Book of ANSI C, Fourth Edition 1 Functions for Modularity 04/24/15
A First Book of ANSI C, Fourth Edition 1
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.
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.
A First Book of ANSI C, Fourth Edition 1
Objectives
• Use Function and Parameter Declarations.
• Return a Value from a Function.
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
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
A First Book of ANSI C, Fourth Edition 1
Function Call May Pass Data
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
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
A First Book of ANSI C, Fourth Edition 1
Function Definition
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
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
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
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.
A First Book of ANSI C, Fourth Edition 1
Participation 1
Write a function mult() that takes two numbers and
prints out their product.
A First Book of ANSI C, Fourth Edition 1
Returning a Value
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.
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
A First Book of ANSI C, Fourth Edition 1
Returning a Value (continue)
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.
A First Book of ANSI C, Fourth Edition 1
Example
• ch6/height.c
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.
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.
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.
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();
A First Book of ANSI C, Fourth Edition 1
Example
• ch6/house.c