CS0004: Introduction to Programming Subprocedures and Modular Design.

Slides:



Advertisements
Similar presentations
Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
Advertisements

Sub and Function Procedures
An Introduction to Programming with C++ Fifth Edition
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
Chapter 4 General Procedures
Example 2.
Sub Programs To Solve a Problem, First Make It Simpler.
Chapter 4 - Visual Basic Schneider
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Chapter 4 - VB.Net by Schneider1 Chapter 4 General Procedures 4.1 Sub Procedures, Part I 4.2 Sub Procedures, Part II 4.3 Function Procedures 4.4 Modular.
Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore,
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
VB .NET Programming Fundamentals
Introduction to Methods
Apply Sub Procedures/Methods and User Defined Functions
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
Chapter 5 - VB 2008 by Schneider1 Chapter 5 - General Procedures 5.1 Sub Procedures, Part I 5.2 Sub Procedures, Part II 5.3 Function Procedures 5.4 Modular.
1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
© 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 © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
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.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
Sub procedures School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 6, Friday 2/21/03)
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
CPS120: Introduction to Computer Science Functions.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
CPS120: Introduction to Computer Science Lecture 14 Functions.
B065: PROGRAMMING Sub Procedures I. Starter  Identify the separate tasks to be performed in the programming task below (break it down into numbered sections).
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
1 CS161 Introduction to Computer Science Topic #9.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Chapter 6 Methods Chapter 6 - Methods.
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
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.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CS0004: Introduction to Programming
Subprograms Functions Procedures.
Chapter 9: Value-Returning Functions
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Methods Chapter 6.
Chapter 5 - Functions Outline 5.1 Introduction
Lesson #6 Modular Programming and Functions.
Starting Out with Programming Logic & Design
Chapter 4 void Functions
6 Chapter Functions.
Procedures Brent M. Dingle Texas A&M University
Chapter 9: Value-Returning Functions
Lesson #6 Modular Programming and Functions.
Starting Out with Programming Logic & Design
Presentation transcript:

CS0004: Introduction to Programming Subprocedures and Modular Design

Review  The difference between arguments and parameters is…  Arguments are the values you pass to a function  Parameters are the local variables used in the function header.  General form of a function: Function FunctionName (ByVal var1 As Type, ByVal var2 As Type) As ReturnDataType … some code … Return expression End Function

Review  A function definition is…  The code for the function (or the header and body)  A function header is…  From the word Function to the return type  A function body is…  Between the function header and the words End Function. In other words the code that is executed when it is called.  Scope is…  Where a variable can be used (seen).

Subprocedures  Subprocedures are functions that DO NOT return a value.  They are used to break down your code into logical sections (modularize).  General Form: Sub SubprocedureName (ByVal var1 As Type, ByVal var2 As Type) … some code … End Sub  Notice that there is no return type or return statement  All the rules that apply to functions apply to sub procedures EXCEPT that you cannot (therefore you do not have to) return a value

Arguments and Parameters  How are arguments are parameters related?  Subprocedure Definition Sub mySub(ByVal a As String, ByVal b As String) MessageBox.show(a,b) End Sub  Subprocedure Call mySub(“Eric”, “Who is the best?”)  What does this function call do?  It displays a message box with the message “Eric” and the title “Who is the best”  Why?  Because the parameter a gets the value “Eric” from the argument in the function call and the parameter b gets the value “Who is the best?” from the other argument in the call.

Subprocedure Example  New Topics:  Subprocedures

Procedures Calling Other Procedures  Subprocedures or functions can call other subprocedures or functions, even themselves. Sub mySub1() line of code 1 mySub2() line of code 3 End Sub Sub mySub2() line of code 4 line of code 5 End Sub

Passing by Value vs. Passing by Reference  Remember the ByVal keyword in front of the parameters?  This keyword indicates that all arguments are passed to this parameter by value.  Passing by value says that a variable argument passed to a function will always retain its value at the time it was passed. Dim a As Integer = 24 mySub(a) MessageBox.show(a) Sub mySub(ByVal inA As Integer) inA = 999 End Sub  Literals, expressions, and variables can be passed by value

Passing by Value vs. Passing by Reference  In addition to ByVal there is also ByRef  This keyword indicates that all arguments are passed to this parameter by reference.  Passing by reference says that a variable argument passed to a function will retain the value that the FUNCTION SETS THE CORRESPONDING PARAMETER.  In other words, whatever happens to the parameter, happens to the argument. Dim a As Integer = 24 mySub(a) MessageBox.show(a) Sub mySub(ByRef inA As Integer) inA = 999 End Sub  Only variables can be passed by reference

Pass by Value/Pass by Reference Example  New Topics:  Passing by Value  Passing by Reference

Pass By Reference Notes  If you do not need an argument to take a value from what is done in the procedure, then pass it by value, otherwise you may have unintended consequences happen.  If you only have one parameter that needs to take a value from what is done in the procedure, it is good programming practice to simply make the procedure a function procedure and return that value.  Passing by value can be seen as being able to return multiple values from a procedure.

A Few More Notes on Scope…  If a variable or constant is declared in a procedure, its lifetime is from the time it is declared to the end of the procedure.  Lifetime of a variable is the period where it still resides in memory.  A variable or constant (or a parameter, really) declared in a procedure is called a local variable or local constant (local to the procedure), and is said to have local scope.  A variable not declared in a procedure, but in a class definition is said to have class-level scope.  A variable declared in an if statement, select case statement, or a loop (next chapters subject) is said to have block-level scope. Also, its lifetime is untill the block (if, select case, etc.) ends  Local scope – can be referenced anywhere in the procedure in which it was declared in  Class-level scope – can be referenced anywhere in the class (including its procedures) in which it was declared in  Block-level scope – can be referenced anywhere in the block in which it was declared.

Modular Design  Many programs can be broken down into smaller sub- tasks.  Remember the Hierarchy Charts…

Hierarchy Charts Postage Stamp Program Read SheetsCalculate StampsDisplay Stamps Get sheets Make sure sheets is a positive integer. Display: "You will need: " # of stamps Set stamps = sheets / 5 Round stamps up to next whole number Must be number Must be positive Must be integer

Modular Design  Here we took a problem and kept breaking it down into smaller and smaller parts. This is called stepwise refinement.  Stepwise refinement is a part of a design methodology called top-down design.  Top-down design general tasks occur near the top of the design and tasks representing their refinement occur below  When using top-down design, the goal is to break down a problem into individual tasks called modules. Thus, when problem is broken down into smaller subtasks, the design is said to be modular.  Modular design is simply a way to design a solution. The implementation of that design is where it appears in the code.

Modular Design  If you implement a program from a modular design, your program should then have the following characteristics:  It is easy to write  It makes a problem easy to conceptualize  If you write modular procedures, it is possible that you may need the same code elsewhere. It is then said this code is reusable.  It is easy to debug  After you write a modular procedure, you can then test it by itself for bugs. When it is bug free, you do not have to worry about it adversely affecting other modules with bugs it may have.  It is easy to understand  It is easy to change  If a specific part of a program needs to be changed, simply go to that procedure and change it

Recursion  Recursion (in computer science) is a method where the solution to a problem depends on solutions to smaller instances of the same problem.  This is often implemented as a function calling itself in programming.  Without a base-case, recursion would go on endlessly.  A base-case is the situation where a recursive function returns a result NOT dependent on the result from another recursive call.  This happens naturally often in math  Factorial  Fibonacci Numbers  Ackermann Function

Recursion Example 1 Function Factorial(ByVal n As Integer) As Integer If n > 1 Then Return n * Factorial(n-1) Else Return 1 End If End Function

Recursion Example 2  Fibonacci in VB