Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.

Slides:



Advertisements
Similar presentations
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
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.
Chapter 6: User-Defined Functions I
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Chapter 2: Input, Processing, and Output
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.
Chapter 6: User-Defined Functions I
Chapter 2: Developing a Program Extended and Concise Prelude to Programming Concepts and Design Copyright © 2003 Scott/Jones, Inc.. All rights reserved.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 1 Section 6 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Simple Program Design Third Edition A Step-by-Step Approach
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Chapter 6: User-Defined Functions
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Keeping it Neat: Functions and JavaScript Source Files Chapter 7.
CPS120: Introduction to Computer Science Functions.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Procedural programming in Java Methods, parameters and return values.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 6 Defining Functions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Eighth Edition.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
Chapter 3: User-Defined Functions I
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
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.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
User-Written Functions
Chapter 6: User-Defined Functions I
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 14 Writing Classes part 2 Richard Gesick.
User-Defined Functions
6 Chapter Functions.
1) C program development 2) Selection structure
Chapter 6: User-Defined Functions I
Chapter 2: Input, Processing, and Output
CPS125.
Introducing Modularity
Presentation transcript:

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 7: More About Program Modules and Subprograms

Data Flow Diagrams and Parameters The concept of program modules was introduced in Chapter 2. Program modules do not have access to all of the variables from the other modules. Some data will be required to be shared between modules. Sharing of data is done by passing parameters from one module to another. Passing data to a module is known as exporting data. Passing data into a module is known as importing data. The sharing of data can be described using data flow diagrams.

7-3 Data Flow Diagram using the Sale Price Computation Problem from the text

7-4 Parameters and Arguments Suppose we have designed a module whose purpose is to output the results of some calculations. We need to pass the results of the calculations (the data) to that module so that it can display the output. The syntax for defining a module that accepts data (has parameters) is shown below. Also shown is how to call such a module and how to pass in the data (arguments). Notice that the names of the variables that are used in the calling module do not need to be the same as the names in the called module.

7-5 Parameters and Arguments (continued) When a subprogram is called, the values of the arguments are assigned to corresponding parameters, based on the order of appearance in the two lists. Therefore, it is very important to pass values in correct order!

7-6 Parameters and Arguments (continued) Call Exam(9, 3) Subprogram Exam (time, room) Write “Your History Final” Write “will be at” Write time, “o’clock in” Write “Room Number “, room End Subprogram Display: Your History Final will be at 9 o’clock in Room Number 3 Call Exam(3, 9) Subprogram Exam (time, room) Write “Your History Final” Write “will be at” Write time, “o’clock in” Write “Room Number “, room End Subprogram Display: Your History Final will be at 3 o’clock in Room Number 9 The Subprogram Exam displays the time and place of your History Final. The Final is at 9:00 o’clock in Room Number 3. See what could happen if the arguments are not passed in correct order!

7-7 Why Use Arguments and Parameters? Modules are more valuable since they can be written in a more general fashion and be used over and over. Modules are easier to design and write. They are defined in terms of what data they need from outside and what data they will produce for export. The programmer writing a module is not concerned with details outside of the module being developed. Module testing, or “unit testing” is an important mechanism to debug / maintain large programs.

More About Subprograms Parameters can be passed by value and by reference. Data may be sent from the caller (main program) to the subprogram. This data is passed or imported to a subprogram. Data may also be passed from the subprogram back to the caller. This data is returned or exported to the main program. The correspondence between arguments and parameters does not indicate which way the data is flowing.

7-9 Value and Reference Parameters Value parameters have the property that changes to their values in the subprogram do not affect the value of the corresponding (argument) variables in the calling module. These parameters can only be used to import data into a subprogram. Reference parameters (also called variable parameters) have the property that changes in their values do affect the corresponding arguments in the calling module. They can be used to both import data into and export data from a subprogram.

7-10 Value and Reference Parameters Pass by value: When a variable is passed by value to a submodule, the submodule only receives a copy of that variable. A separate storage location is created and the value of the variable is stored in that location. Therefore, any changes made to that variable in the subprogram do not affect the original variable. Pass by reference: When a variable is passed by reference, the submodule receives the actual storage location of that variable. Therefore, changes made to the variable in the subprogram are also made to the original variable.

7-11 Example: Pass by Val and Pass by Ref Main Program Set Num1 = 1 Set Num2 = 2 Call Switch (Num1, Num2) Write Num1, “ ”, Num2 End Program Subprogram Switch (Number1, Number2 As Ref) Set Number1 = 2 Set Number2 = 1 End Subprogram Num1 is passed by value, and it is not changed by the subprogram. Num2 is passed by reference and it is changed by the subprogram. This program prints: 1

Functions A function is a subprogram whose name can be assigned a value. This allows the calling program to use the name of the function in an expression. Some examples were already introduced in earlier chapters: Sqrt(X) Int(X) Length(S) EOF(FileName) Example: Set X = Sqrt(10*47)+ 101 The function call Sqrt(10*47) evaluates to a numeric value which is then used in the expression. After this statement is executed, the value of X is 571.

7-13 Built-in Functions Most programming languages provide built-in functions. The code for these functions are supplied in separate modules and can be used by any programmer. Some examples of built-in functions are: Abs(X) computes and returns the absolute value. It is of type Real. Round(X) rounds the real number, X, to the nearest whole number. It is of type Integer. Str(X) converts the number X to a corresponding string. It is of type String.

7-14 User-Defined Functions User-defined functions are created by the programmer. The differences between a function and a subprogram are: 1.A function’s name may be assigned a value in the code that defines it. 2.A function is called by placing its name with any arguments anywhere in the program where a constant of the function’s type is allowed.

7-15 Defining Functions Since function calls evaluate to some value, the definition of the Function must include which data type the function evaluates to. The following pseudocode shows the syntax for defining a Function that finds the cube of a number: Main Program Declare Num As Real Set Num = Cube(10) Write Num End Program Function Cube(X) As Real Set Cube = X^3 End Function

Recursion A recursive subprogram is one that calls itself. Some languages do not permit recursion. Recursive subprograms may be an effective way of providing a solution for a specific problem. Problems that are solved with recursion are those that can be easily described in terms of themselves, for example, the sum of the first N integers can be written as: Sum(N) = Sum(N – 1) + N

7-17 Recursive Code Example Function Sum(N) As Integer If N = 1 Then Set Sum = 1 Else Set Sum = Sum(N - 1) + N End If End Function Tracing recursive code is difficult at first. The recursive call must be conditional, or there will be an infinite recursion. This is similar to a looping construct, but no repetition is used; there are only function calls to the function we are defining. Recursive call to Sum

7-18 Pseudocode Language (Ch 7) In this chapter we added some more syntax to our language. We discussed how to pass parameters by value and by reference. We also we saw how to define functions. Defining Subprograms with Parameters Subprogram Name (Param1 as Ref, Param2) End Subprogram Defining Functions Function Name (Param1, Param2) As DataType End Function