CSI 101 Elements of Computing Spring 2009 Lecture #10 – Functions and Subroutines Monday, March 16th.

Slides:



Advertisements
Similar presentations
Sub and Function Procedures
Advertisements

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.
Procedural programming in Java
Macro Processor.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
1.
VBA Modules, Functions, Variables, and Constants
CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
PSU CS 106 Computing Fundamentals II VB Statements HM 5/19/2008.
CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.
CSI 101 Elements of Computing Spring 2009 Lecture # 14 – Classes and Objects Wednesday, April 15th, 2009 and Monday, April 20th, 2009.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Functions Gabriel Hugh Elkaim Spring 2012.
Apply Sub Procedures/Methods and User Defined Functions
IE 212: Computational Methods for Industrial Engineering
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
Why to Create a Procedure
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
CS0004: Introduction to Programming Subprocedures and Modular Design.
CSC 221: Computer Programming I Fall 2001  top-down design  approach to problem solving, program design  focus on tasks, subtasks, …  reference parameters.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
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.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
ME 142 Engineering Computation I Using Subroutines Effectively.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
BACS 287 Programming Fundamentals 5. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Procedures Built-in Functions User-defined.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
More on Variables and Subroutines. Introduction Discussion so far has dealt with self- contained subs. Subs can call other subs or functions. A module.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Copyright © 2014 Pearson Education, Inc. Chapter 6 Procedures and Functions.
Subroutines and Functions Chapter 6. Introduction So far, all of the code you have written has been inside a single procedure. –Fine for small programs,
Computer Science Up Down Controls, Decisions and Random Numbers.
Sub Procedures and Functions Visual Basic. Sub Procedures Slide 2 of 26 Topic & Structure of the lesson Introduction to Modular Design Concepts Write.
Lecture 7 Methods (functions and subroutines) Parameter Passing
CS0004: Introduction to Programming
Arrays Chapter 7.
More About Objects and Methods
The need for Programming Languages
A variable is a name for a value stored in memory.
COMPUTATIONAL CONSTRUCTS
How to be generic Lecture 10
Suppose we want to print out the word MISSISSIPPI in big letters.
Method.
Procedures and Functions
CIS16 Application Development Programming with Visual Basic
Sub Procedures and Functions
CS285 Introduction - Visual Basic
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
Methods.
Introduction to Computing Lecture 08: Functions (Part I)
Introduction to Computer Programming IT-104
Presentation transcript:

CSI 101 Elements of Computing Spring 2009 Lecture #10 – Functions and Subroutines Monday, March 16th

2 Procedural Programming  Method of organizing computer program  Breaking up code into smaller segments Makes it more efficient Allows multiple programmers to produce Facilitates reuse  Tie segments together logically Code statements in segment perform a particular service or function

3 How does Procedural Programming work?  Think of it like an assembly line Each part of assembly line works on a specific task or piece of the final product Dedicated workers focus their efforts just on their part of the process Foreman oversees entire process and makes sure it runs smoothly

4 Types of procedural segments  3 types in Visual Basic  Event procedure Invoke when a particular action occurs on a screen form  Subroutine procedure Invoked via a CALL statement  Function procedure Invoked by referring to its name Example: result = Foo()

5 Parameters  Input to procedure  Data type must match general type of procedure Doesn’t have to be exact match For instance, integral values could pass a Long into an Integer if value does not exceed Integer’s maximum  If more than one parameter, corresponds in order

6 Parameter Examples  Foo has 3 inputs: Integer, Integer, String Dim A as Integer, B as Integer, C as Integer Dim S1 as String, S2 as String Dim X As Long, Y as Single, Z as Double  CALL Foo(A,B,S1) works  CALL Foo(S1,A,B) fails – WHY? S1 is 1 st parameter, would correspond to Integer B is 3 rd parameter, would correspond to String

7 Parameter Examples, cont  Foo has 3 inputs: Integer, Integer, String Dim A as Integer, B as Integer, C as Integer Dim S1 as String, S2 as String Dim X As Long, Y as Single, Z as Double  CALL Foo(A,X,S1) Works if X's value is within 2 billion  CALL Foo(A,Y,S2) works – WHY? Removes any fractional value from Y

8 Defining Subroutines  SUB statement begins it Like BEGIN from pseudocode, it must name the subroutine  END SUB ends it Unlike END in pseudocode, does not need name Cannot define another subroutine within a subroutine, so the END SUB would refer to the previous SUB statement

9 Subroutine arguments  Arguments are the subroutine’s corresponding parameters  Parameters are considered what the calling process uses, while arguments are defined by the subroutine  Arguments have variable names and data types just like parameters Uses same structure as DIM without DIM keyword

10 Defining Subroutine example  Let’s define our previous FOO: Sub Foo(InVal1 As Integer, InVal2 As Integer, InStr As String) : End Sub  Note each argument is given a unique name and has a defined data type

11 Parameter passing  2 ways:  Pass by reference Called routine does not make a copy, but works in original register Thus, any change to argument in called routine is reflected in original procedure’s parameter value This is Virtual Basic’s default  Pass by value Called routine makes a copy of data value when called Any change to called routine’s copy is not reflected in the original procedure’s variable

12 Pass by Reference Example  Foo(Int1, Int2, String1) If Int1 > Int2, String1 set to “Higher” If Int1 < Int2, String1 set to “Lower” If Int1 = Int2, String1 set to “Equal”  CALL Foo(A, B, S1) A and B retain value, but S1 changes

13 Pass by Value  Need to specify ByVal in subroutine definition  Note that this is controlled by subroutine, not by calling routine  Thus, could change Foo by: Sub Foo2(Int1 As Integer, Int2 As Integer, ByVal Str1 As String)  Note ByVal keyword goes before argument name

14 Pass by Value Example  Foo2(Int1, Int2, String1) If Int1 > Int2, String1 set to “Higher” If Int1 < Int2, String1 set to “Lower” If Int1 = Int2, String1 set to “Equal”  CALL Foo2(A, B, S1) No variables have their values changed when Foo2 ends

15 Subroutine return values  Pass by reference is how subroutines return a value to its calling procedure  Must use an argument, so calling procedure must have variable of correct data type to use as argument

16 Functions  These are special processing segments that are invoked by using their name  They have a single return value, and it is placed in a variable that is NOT a parameter Computer creates a variable with the same name as the function. By default, that is the variable used to return the function's return value  Visual Basic has many predefined functions, but programmers are free to create their own as well

17 Form of a Function  Begins with FUNCTION keyword  Concludes with END FUNCTION  Arguments defined as in subroutine  Includes definition of return value  Example: Function Foo3(Val1 As Integer, Val2 As Integer) As Integer : End Function

18 Return keyword  Simultaneously sets return value and ends the function Return A + B  Can be placed anywhere before END FUNCTION  Often used with IF statement to end function on particular condition IF index > 255 Then 'Exceed max value Return 0 'Value for error Else 'Continue processing

19 Function Example  Let’s create a function as an example  Our function finds the minimum value of three numbers Call it MIN  We'll restrict our values to integers  Return value is one of them, so it’s also an integer

20 MIN Function Function Min(A As Integer, B As Integer, C As Integer) As Integer If A<B Then 'A smaller If A<C Then Min=A 'Note we can set Min 'Min is the variable created to hold the return value Else Min=C Endif Elseif B<C Then Min=B Else Min=C EndIf End Function 'Note no RETURN statement  RETURN not necessary if variable named for function contains the return value

21 Invoking MIN Dim A,B,C,M as Integer 'At some point, we get values for A, B, and C M = Min(A,B,C) 'M now holds the smallest value