A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
An Introduction to Programming with C++ Fifth Edition
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2007 Pearson Education, Inc. All rights reserved C Functions.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
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.
Chapter 6: User-Defined Functions I
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Chapter 6: Functions.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
A First Book of ANSI C Fourth Edition
1 Lecture 3 Part 1 Functions with math and randomness.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
C++ for Engineers and Scientists Third Edition
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
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.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
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 6: Repetition Continued. 2 Validity Checks What’s weak about the following code ? do { s1 = JOptionPane.showInputDialog (“Enter a number: ”);
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Programming Fundamentals Enumerations and Functions.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Chapter 6 Modularity Using Functions
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
User-Written Functions
Chapter 6: User-Defined Functions I
Chapter 3 Assignment and Interactive Input.
© 2016 Pearson Education, Ltd. All rights reserved.
Functions, Part 2 of 2 Topics Functions That Return a Value
A First Book of ANSI C Fourth Edition
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
User-Defined Functions
C++ for Engineers and Scientists Second Edition
Chapter 5 - Functions Outline 5.1 Introduction
A First Book of ANSI C Fourth Edition
Functions, Part 2 of 3 Topics Functions That Return a Value
A First Book of ANSI C Fourth Edition
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I

A First Book of ANSI C, Fourth Edition2 Objectives Function and Parameter Declarations Returning a Value Case Study: Calculating Age Norms Standard Library Functions Common Programming and Compiler Errors

A First Book of ANSI C, Fourth Edition3 Function and Parameter Declarations A function that is called into action by its reference in another function is a called function A function that calls another function is referred to as the calling function

A First Book of ANSI C, Fourth Edition4 Function and Parameter Declarations (continued)

A First Book of ANSI C, Fourth Edition5 Function and Parameter Declarations (continued)

A First Book of ANSI C, Fourth Edition6 Function Prototypes The declaration statement for a function is referred to as a function prototype –Declares the data type of the value that will be directly returned by the function –Declares the data type of the values that need to be transmitted to the called function when it is invoked –returnDataType functionName(argument data types); Function prototypes allow the compiler to check for data type errors –If the function prototype does not agree with data types specified when the function is written, an error message (typically TYPE MISMATCH) will occur

A First Book of ANSI C, Fourth Edition7 Calling a Function Arguments: items enclosed in parentheses in a function call statement –Other terms used as synonyms for arguments are actual arguments and actual parameters Pass by value: when a function receives copies of the values in each argument and must determine where to store them before it does anything else –Also referred to as call by value

A First Book of ANSI C, Fourth Edition8 Calling a Function (continued)

A First Book of ANSI C, Fourth Edition9 Calling a Function (continued)

A First Book of ANSI C, Fourth Edition10 Function Header Line Function header: identifies the data type of the return value, provides the function with a name, and specifies the number, order, and type of values expected by the function Function body: operates on the passed data and returns, at most, one value The argument names in the header line are known as parameters or formal parameters and formal arguments

A First Book of ANSI C, Fourth Edition11 Function Header Line (continued)

A First Book of ANSI C, Fourth Edition12 Function Header Line (continued)

A First Book of ANSI C, Fourth Edition13 Function Header Line (continued) main() must adhere to the rules required for constructing all C functions Some programmers prefer to put all called functions at the top of a program and make main() the last function listed Each C function is a separate and independent entity with its own parameters and variables –Nested functions are not permitted The function’s prototype, along with pre- and postconditions should provide all the information necessary to call the function successfully

A First Book of ANSI C, Fourth Edition14 Ends with a semicolon Does not end with a semicolon Function Header Line (continued)

A First Book of ANSI C, Fourth Edition15 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; }

A First Book of ANSI C, Fourth Edition16 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 Edition17 Returning a Value (continue)

A First Book of ANSI C, Fourth Edition18 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 then automatically converted to the return value’s data type as specified in the function’s header line before being sent back to the calling function Failure to exactly match the return value with the function’s declared data type can lead to undesired results –Return value is converted to the data type declared in the function’s header line

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

A First Book of ANSI C, Fourth Edition20 Value is automatically converted from double to float (it may also generate a compiler warning message) printf("The Celsius equivalent is %5.2f\n", tempConvert(fahren)); Returning a Value (continue)

A First Book of ANSI C, Fourth Edition21 Function Stubs A stub is the beginning of a final function, used as a placeholder until the final function is completed float findMax(float x, float y) { printf("In findMax()\n"); printf("The value of x is %f\n", x); printf("The value of x is %f\n ", y); return 1.0; } A stub must compile and link with its calling module –Stub should display a message that it has been entered successfully and the value(s) of its received arguments

A First Book of ANSI C, Fourth Edition22 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 Edition23 Case Study: Calculating Age Norms

A First Book of ANSI C, Fourth Edition24 Requirements Specification A fairly common procedure in child development is to establish normal ranges for height and weight as they relate to a child’s age These normal ranges are frequently referred to as age norms In this case study, we develop a program for calculating both the expected height of a child between the ages of 6 and 11 and the deviation of this height norm to an actual child’s height

A First Book of ANSI C, Fourth Edition25 Requirements Specification (continued)

A First Book of ANSI C, Fourth Edition26 Requirements Specification (continued)

A First Book of ANSI C, Fourth Edition27 Requirements Specification (continued)

A First Book of ANSI C, Fourth Edition28 Requirements Specification (continued)

A First Book of ANSI C, Fourth Edition29 Requirements Specification (continued)

A First Book of ANSI C, Fourth Edition30 Requirements Specification (continued)

A First Book of ANSI C, Fourth Edition31 Standard Library Functions The standard library consists of 15 header files Before using these functions, you must know –The name of each available function –The arguments required by each function –The data type of the result (if any) returned by each function –A description of what each function does –How to include the library containing the desired function #include

A First Book of ANSI C, Fourth Edition32 Mathematical Library Functions

A First Book of ANSI C, Fourth Edition33 Mathematical Library Functions (continued)

A First Book of ANSI C, Fourth Edition34 The rand() and srand() Functions Random numbers are a series of numbers whose order cannot be predicted Pseudorandom numbers are not really random, but are sufficiently random for the task at hand All C compilers provide two functions for creating random numbers: rand() and srand(), defined in the stdlib.h header file –rand() produces random numbers in the range 0 < rand() < RAND_MAX –srand() provides a starting “seed” value for rand()

A First Book of ANSI C, Fourth Edition35 The rand() and srand() Functions (continued)

A First Book of ANSI C, Fourth Edition36 Scaling The method for adjusting the random numbers produced by a random-number generator to reside within a specified range is called scaling To scale a random number as an integer value between 1 and N: 1 + (int)rand() % N To produce a random integer between the numbers a and b : a + (int)(rand() % (b - a + 1))

A First Book of ANSI C, Fourth Edition37 Coin Toss Simulation

A First Book of ANSI C, Fourth Edition38 Coin Toss Simulation (continued)

A First Book of ANSI C, Fourth Edition39 Coin Toss Simulation (continued)

A First Book of ANSI C, Fourth Edition40 Input/Output Library Functions getchar() can be used for single character input –int getchar() –The reason for returning characters in integer format is to allow the End-Of-File (EOF) sentinel to be returned putchar() expects a single character argument and displays the character passed to it on the terminal –For example, putchar('a')

A First Book of ANSI C, Fourth Edition41 Character Processing Functions

A First Book of ANSI C, Fourth Edition42 Character Processing Functions (continued)

A First Book of ANSI C, Fourth Edition43 Character Processing Functions (continued)

A First Book of ANSI C, Fourth Edition44 Conversion Functions

A First Book of ANSI C, Fourth Edition45 Conversion Functions (continued)

A First Book of ANSI C, Fourth Edition46 Common Programming Errors Passing incorrect data types Omitting a called function’s prototype Terminating a function’s header line with a semicolon Forgetting to include a data type for each parameter listed in a function’s header line Returning a different data type from a function than the data type specified in the function’s header line

A First Book of ANSI C, Fourth Edition47 Common Compiler Errors

A First Book of ANSI C, Fourth Edition48 Common Compiler Errors (continued)

A First Book of ANSI C, Fourth Edition49 Summary A function is called by giving its name and passing any data to it in the parentheses following the name The first line of the function is called the function header A function’s return type is the data type of the value returned by the function Functions can directly return at most a single value to their calling functions

A First Book of ANSI C, Fourth Edition50 Summary (continued) Functions can be declared to all calling functions with a function prototype Arguments passed to a function provide a means of evaluating any valid C expression A set of preprogrammed functions for mathematical calculations, character input and output, character processing, and numerical conversions are included in the standard library provided with each C compiler