Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15."— Presentation transcript:

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

2 A First Book of ANSI C, Fourth Edition1 Final Exam Friday, May 8 th, 12:30-2:30 Accumlative Emphasis on Loops and Functions, Chapters 5 & 6 You may bring a half sheet of notes to the exam.

3 A First Book of ANSI C, Fourth Edition1 Programs Program 6 is graded. Program 9 is page 297, #4 a&b. – Extra point for screen shot of evaluation proof.

4 A First Book of ANSI C, Fourth Edition1 Objectives Use Function and Parameter Declarations. Return a Value from a Function.

5 A First Book of ANSI C, Fourth Edition1 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

6 A First Book of ANSI C, Fourth Edition1 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

7 A First Book of ANSI C, Fourth Edition1 Function Call May Pass Data

8 A First Book of ANSI C, Fourth Edition1 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

9 A First Book of ANSI C, Fourth Edition1 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

10 A First Book of ANSI C, Fourth Edition1 Function Definition

11 A First Book of ANSI C, Fourth Edition1 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

12 A First Book of ANSI C, Fourth Edition1 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

13 A First Book of ANSI C, Fourth Edition1 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 directives symbolic constants function prototypes can be placed here int main()‏ { function prototypes can be placed here variable declarations; other executable statements; return value; } Function definitions

14 A First Book of ANSI C, Fourth Edition1 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.

15 A First Book of ANSI C, Fourth Edition1 Participation 1 Write a function mult() that takes two numbers and prints out their product.

16 A First Book of ANSI C, Fourth Edition1 Returning a Value

17 A First Book of ANSI C, Fourth Edition1 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.

18 A First Book of ANSI C, Fourth Edition1 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

19 A First Book of ANSI C, Fourth Edition1 Returning a Value (continue)‏

20 A First Book of ANSI C, Fourth Edition1 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.

21 A First Book of ANSI C, Fourth Edition1 Example ch6/height.c

22 A First Book of ANSI C, Fourth Edition1 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.

23 A First Book of ANSI C, Fourth Edition1 Another Example Write a function that raises an integer to a positive integer power. It will return the result.

24 A First Book of ANSI C, Fourth Edition1 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.

25 A First Book of ANSI C, Fourth Edition1 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();

26 A First Book of ANSI C, Fourth Edition1 Example ch6/house.c


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

Similar presentations


Ads by Google