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

Slides:



Advertisements
Similar presentations
Introduction to C++ Functions Chapter 6 Sections 6.1 – 6.3 Computer II.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Overview creating your own functions calling your own functions.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS 201 Functions Debzani Deb.
Introduction to Methods
Chapter 6: Functions.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
C++ for Engineers and Scientists Third Edition
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Topic 3 – The General Form of a C Program. CISC 105 – Topic 3 The General Form of a C Program Now, all of the basic building blocks of a C program are.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Learners Support Publications Functions in C++
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
User defined functions
CSCI 171 Presentation 6 Functions and Variable Scope.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 ICS103 Programming in C Lecture 8: Functions I.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Lecture-3 Functions and Recursion. C Preprocessor Includes header files like stdio.h Expands macros defined Handles conditional compilations PreprocessorProcessor.
Programming Fundamentals Enumerations and Functions.
Chapter 7 Modularity Using Functions: Part II. A First Book of ANSI C, Fourth Edition 2 Variable Scope If variables created inside a function are available.
A First Book of ANSI C Fourth Edition
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Chapter 6 Modularity Using Functions
Chapter 9: Value-Returning Functions
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
Suppose we want to print out the word MISSISSIPPI in big letters.
Chapter 10: Void Functions
User-Defined Functions
Introduction to Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
A First Book of ANSI C Fourth Edition
Chapter 7: User-Defined Functions II
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 42 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Presentation transcript:

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

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.

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.

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

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

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

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

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

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

A First Book of ANSI C, Fourth Edition1 Function Definition

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

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

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

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.

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

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

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.

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

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

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.

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

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.

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.

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.

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();

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