CP1020 - Week 10 Modularising programs using Procedures.

Slides:



Advertisements
Similar presentations
Chapter 6, Slide 1Starting Out with Visual Basic 3 rd Edition Chapter 6 Sub Procedures And Functions.
Advertisements

COMPUTER PROGRAMMING Task 1 LEVEL 6 PROGRAMMING: Be able to use a text based language like Python and JavaScript & correctly use procedures and functions.
C Language.
Spring Semester 2013 Lecture 5
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
CS0004: Introduction to Programming Repetition – Do Loops.
Lists Introduction to Computing Science and Programming I.
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
CP Lecture 11 Functions and advanced procedures.
VBA Modules, Functions, Variables, and Constants
Example 2.
CP Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice.
String Variables Visual Basic for Applications 4.
CP Week 2 zReserved words zVariables zInput and output zData types zTesting and Documentation.
CP Week 6 Repetition. Aims and Objectives zUnderstand what loops are and why they are needed zUnderstand the FOR loop used in programming zUnderstand.
CP1020 Week 5 Selection Continued. CP1020 University of Wolverhampton - Steve Garner and Ian Coulson if then else zWe can use if then else statements.
Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters.
Developing Software Applications Introduction to Programming Fundamentals Scoping in VB Simple Ifs in VB.
CP1020 University of Wolverhampton - Steve Garner and Ian Coulson1 Week 1 - Principles of programming Welcome from the Presenters Steve Garner and Dr Ian.
CP Principles of Programming Steve Garner and Ian Coulson CP Week 8 Arrays.
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner CP Week 3 zArithmetic operations zOperator precedence  Simple QBasic functions.
Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.
CP Week 7 Looping constructs Aims and Objectives zUnderstand what the while group of loops are and why they are needed zBe able to design and code.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Introduction to Methods
Apply Sub Procedures/Methods and User Defined Functions
Object Oriented Software Development
Lecture Note 3: ASP Syntax.  ASP Syntax  ASP Syntax ASP Code is Browser-Independent. You cannot view the ASP source code by selecting "View source"
Functions & Objects IDIA 618 Spring 2012 Bridget M. Blodgett.
1 Subroutines and Functions Chapter 6 in Deitel, Deitel and Nieto.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
CS0004: Introduction to Programming Subprocedures and Modular Design.
E0001 Computers in Engineering Procedures: subprograms and functions.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
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.
Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Introduction to Visual Basic Programming. Introduction Simple Program: Printing a Line of Text Another Simple Program: Adding Integers Memory Concepts.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Controlling Program Flow with Decision Structures.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Week 1 Lecture 1 Slide 1 CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton CP2028 Visual Basic Programming 2 v Week.
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.
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,
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.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Lecture 7 Methods (functions and subroutines) Parameter Passing
Chapter 9: Value-Returning Functions
User-Written Functions
Chapter 7: User-Defined Functions II
Functions and Procedures
Microsoft Access Illustrated
Using local variable without initialization is an error.
Computer Science and an introduction to Pascal
Chapter 4 void Functions
Fundamentals of Programming
Coding Concepts (Basics)
If, Subroutines and Functions
The structure of programming
Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3
Programming Techniques
Agenda for Unit 3: Functions
Presentation transcript:

CP Week 10 Modularising programs using Procedures

CP1020 Principles of programming - Steve Garner & Ian Coulson Aims of this lecture zUnderstand why we should modularise a program zUnderstand how QBasic approaches this zCreate some procedures

CP1020 Principles of programming - Steve Garner & Ian Coulson Why modularise a program zIt is much easier to solve any problem, by breaking it down into tasks and solve them one at a time - called procedures zIt also allows these small modules or procedures to be re-used, and cuts down the amount of coding and potential errors zThis approach is fundamental to programming, in fact it is the basis of programming in C++

CP1020 Principles of programming - Steve Garner & Ian Coulson A simple example PRINT “*****************************************” PRINT “** **” PRINT ”** Something **” PRINT “** **” PRINT “*****************************************” PRINT “** **” PRINT ”** More things **” PRINT “** **” PRINT “*****************************************” A program that prints a title banner surrounded by asterisks.

CP1020 Principles of programming - Steve Garner & Ian Coulson The example Here we have the lines PRINT “*****************************************” repeated three times in the program. We could write a small procedure that we use three times to do this. How do we do this?

CP1020 Principles of programming - Steve Garner & Ian Coulson Creating a sub procedure From the Edit menu select the New Sub… option, a dialogue box will appear requesting a procedure name. A new code window opens up and your lines of code are written between SUB Asterisk andprocedure name END SUB In our case this would look like SUB Asterisk PRINT “*****************************************************” END SUB

CP1020 Principles of programming - Steve Garner & Ian Coulson Running a procedure We’ve created the procedure and now have to use it! To run our small section of code (procedure) we simply have to use the keyword CALL Procedure Name e.g. Now write this as the main program CALL Asterisk PRINT “** **” PRINT ”** Some inconsequential tripe **” PRINT “** **” CALL Asterisk PRINT “** **” PRINT ”** More things **” PRINT “** **” CALL Asterisk

CP1020 Principles of programming - Steve Garner & Ian Coulson Procedures that use data May need a procedure that uses data from the main body of the program There are two aspects to this 1. We must call the procedure and give it the data 2. The procedure must be able to pick up the data example a procedure that determines a student grade from the results of 2 tests

CP1020 Principles of programming - Steve Garner & Ian Coulson A procedure that uses data SUB Grade(iNum1 AS INTEGER, iNum2 AS INTEGER) DIM iScore AS INTEGER iScore = iNum1 + iNum2 SELECT CASE iScore CASE IS < 40 PRINT “Sorry try harder” CASE 40 TO 59 PRINT “well done you have a pass” CASE 60 TO 100 PRINT “excellent result- you have a merit” END SELECT END SUB procedure Main program INPUT “enter your two test results” ; iTest1, iTest2 CALL Grade(iTest1, iTest2)

CP1020 Principles of programming - Steve Garner & Ian Coulson Passing data to a Procedure zThe call statement in the example shows how we pass data to the procedure CALL Grade(iTest1, iTest2) zThe variables iTest1 and iTest2 are placed in brackets and pass the data across. zThe data is picked up by iNum1 and iNum2 in the procedure SUB Grade(iNum1 AS INTEGER, iNum2 AS INTEGER) zWe can then use iNum1 and iNum2 in the body of the procedure zHowever it’s a bit more complicated!

CP1020 Principles of programming - Steve Garner & Ian Coulson Passing data by reference iNum1 and iNum2 ARE NOT new variables with copies of the data stored in variables iTest1 and iTest2 iNum1 actually refers to the data stored in iTest1 2 iTest1 iNum1 BEWARE - This means any change in the value of iNum1 in the procedure, will result in a change in the value of iTest1 in the main body of the program.

CP1020 Principles of programming - Steve Garner & Ian Coulson Example Main program iVal = 6 CALL Example(iVal) PRINT “iVal =“, iVal Procedure SUB Example(iNumber AS INTEGER) iNumber = iNumber + 10 END SUB What value do you think will be printed for iVal? iVal = 6 or iVal = 16 well the answer is iVal = 16

CP1020 Principles of programming - Steve Garner & Ian Coulson Passing data across by value You may want to pass the data into a procedure and not alter the original value in the main program. We can do this by passing it by value by enclosing the variables (or arguments) in the call statement in brackets e.g. CALL procedure name( ( argument ),( argument ) ) JARGON - an argument is the name given to the list of variables that are placed in the procedure call More JARGON - the corresponding list of dummy variables used to pick up the arguments in the procedure are known as parameters e.g Procedure name ( parameters)

CP1020 Principles of programming - Steve Garner & Ian Coulson example of passing by value Main program iVal1 = 5 iVal2 = 3 CALL Example( ( iVal1 ), ( iVal2 ) ) PRINT “iVal1 = “, iVal1 “and iVal2 = “, iVal2 Procedure SUB Example(iNum1 AS INTEGER, iNum2 AS INTEGER) iNum1 = iNum iNum2 = iNum2 + 7 END SUB Parameters of the function What value do you think will be printed for iVal1 and iVal2? Ival1 = 15 & iVal2 = 10 or iVal1 = 5 & iVal2 = 3 well the answer is -> iVal1 = 5 & iVal2 = 3

CP1020 Principles of programming - Steve Garner & Ian Coulson Some rules governing functions & procedures zAny variables declared within a function or procedure are local - that is the calling program or any other procedure or function cannot see or use the values stored in these variables zExample a procedure that adds up two numbers

CP1020 Principles of programming - Steve Garner & Ian Coulson A program that won’t work - why? SUB AddUp( iNum1 AS INTEGER, iNum2 AS INTEGER) Dim iTotal AS INTEGER iTotal = iNum1 + iNum2 END SUB DIM iNum1, iNum2 AS INTEGER INPUT “enter 2 numbers”, iNum1, iNum2 CALL AddUp(iNum1, iNum2) PRINT “the sum of your two numbers is “; iTotal Because iTotal has been declared within AddUp - therefore does not exist outside of it - so can’t print it’s value out in main program

CP1020 Principles of programming - Steve Garner & Ian Coulson Questions 1 Why do we modularise a program? 2 Write a call statement and procedure that will print the statement “Hello World” 3 Write a procedure to pass 2 numbers across by reference and determine the difference between the two and print it out. 4 rewrite this such that the numbers are passed by value