PROGRAMMING Sub Procedures I.

Slides:



Advertisements
Similar presentations
Unit 6 Assignment 2 Chris Boardley.
Advertisements

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Understanding the Mainline Logical Flow Through a Program (continued)
Starter  Identify the separate tasks to be performed in the programming task below (break it down into numbered sections).  A program is needed to prompt.
Code Regions and XML Comments. Code Regions The code editor automatically puts little minus signs next to the header line for each Sub or Function. You.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Apply Sub Procedures/Methods and User Defined Functions
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
CS0004: Introduction to Programming Subprocedures and Modular Design.
LO To practice our methods of division. 11- Nov-14 Write a comment or make a statement about this prompt.
PROGRAMMING ITERATION 2. Starter  Put the following code in order (write down the line numbers).  The program should display the numbers 1-24 on screen.
B065: PROGRAMMING Sub Procedures I. Starter  Identify the separate tasks to be performed in the programming task below (break it down into numbered sections).
The Software Development Process
COPYRIGHT 2010: Dr. David Scanlan, CSUS OBJECTIVES: How to write classes How to create an object from a class using the "New" keyword. How to communicate.
Procedures Subs and Functions. Procedures Before OOP, subroutines were the primary high-level way to organize a program. In OOP, this role has been taken.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
04/02/ Procedures Top-down approach / Stepwise Refinement & Sub Procedures.
JavaScript 101 Lesson 6: Introduction to Functions.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
JavaScript/ App Lab Programming:
Subprograms Functions Procedures.
Chapter 9: Value-Returning Functions
Introduction to Computing Science and Programming I
Structured Programming
COMPUTATIONAL CONSTRUCTS
INTRODUCTION TO PROBLEM SOLVING
MODULAR PROGRAMMING Many programs are too large to be developed by one person. programs are routinely developed by teams of programmers The linker program.
Lesson #6 Modular Programming and Functions.
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
Chapter 2: Input, Processing, and Output
Lesson #6 Modular Programming and Functions.
Lesson 16 Sub Procedures Lesson 17 Functions
Loop Structures.
Worked Examples - Incremental Software Development Worked Example
Algorithm development
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.
CS 108 Computing Fundamental Notes for Thursday, October 5, 2017
Incrementing ITP © Ron Poet Lecture 8.
Understand the Programming Process
Problem Solving (design of programs) CS140: Introduction to Computing 1 8/26/13.
Lesson #6 Modular Programming and Functions.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Designing and Debugging Batch and Interactive COBOL Programs
Structured Programming
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Number and String Operations
Chapter 4 void Functions
Top-down approach / Stepwise Refinement & Sub Procedures
If selection construct
Do While (condition is true) … Loop
Algorithm Discovery and Design
Unit 6 Assignment 2 Chris Boardley.
If selection construct
Understand the Programming Process
Text / Serial / Sequential Files
Procedures Brent M. Dingle Texas A&M University
3.1 Iteration Loops For … To … Next 18/01/2019.
Software Development Process
Lesson #6 Modular Programming and Functions.
Week 4 Lecture-2 Chapter 6 (Methods).
Chapter 2: Input, Processing, and Output
Arrays.
10.3 Procedures Function Procedures 07/06/2019.
3.2 Working with Data Scope of variables 29/07/2019.
10.2 Procedures Passing Parameters 30/08/2019.
Macro theory © EIT, Author Gay Robertson, 2019.
Presentation transcript:

PROGRAMMING Sub Procedures I

Starter Identify the separate tasks to be performed in the programming task below (break it down into numbered sections). A program is needed to prompt a user for their name and monthly wage. From this it should then calculate their yearly salary and yearly pension deductions (8% of their yearly salary). This summary should be written to file and displayed on screen. TASKS: Obtain Name and Monthly Wage Calculate Salary and Deductions. Write name, monthly wage, salary and deductions to file Display name, monthly wage, salary and deductions on screen

Objectives Understand the importance of modular programming. Know the role of procedures within programming. Understand the concept of parameters; ByRef and ByVal Use procedures within your programming.

Modular Programming Going back to our previous sessions on Algorithms we noted that you could break things down into smaller chunks, or sub-tasks to make things easier to follow or make the problem easier to understand. This is the same case in the actual programming of an algorithm. As you add more code, your program gets longer and longer. Changing one part of a long program which is all line after line, could have devastating effects, causing the whole program to stop functioning If you wanted to re-do things over and over again. E.g. calculate an average, display it on screen, every time a different set of numbers was entered (at different times – so a loop is no good). Normally, you would need the same code every time you wanted to do this This causes excessive amounts of the same code to be needed, repeatedly.

Why Go Modular Big problems are generally easier to solve if we break them up into smaller problems, for two reasons: smaller problems are inherently easier than the larger problem of which they are a part; a team of people can work on the smaller problems, each member concentrating on one, or just a few, of them. A procedure is the code equivalent of a solution to one of the smaller problems Code can be re-used over and over again Allows you to focus on what needs to be done, rather than how to do it. Procedures make a program easy to understand and easier to maintain.

What is a procedure? Every program you have written so far is one! A procedure, then, is a small part of a program, which has its own name, performs a specific job (usually a single task, or a small group of related tasks), and is executed when it is "asked for". Sub Main – is the one you have written everything in so far. You will have noticed your code has gradually become longer and longer – this is not good! Everything in one sub is BAD.

New Procedures can go above or below your existing Sub Main. Declaring a Procedure Sub Name() statement(s) End Sub The word Sub is essential, and must appear exactly like that. Name is any legal VB identifier and should be chosen carefully to indicate what the procedure does. The parentheses () are also obligatory - you cannot omit them. Statement(s) represents the actual code that you write to perform the required task. End Sub (two separate words) is also essential. New Procedures can go above or below your existing Sub Main.

Writing a Procedure You should always ensure that the first thing in the code for any procedure you write is a comment which explains what the procedure does In the example here, we have declared a procedure called AverageCalc. It calculates the average and displays it on screen. Notice how it is just doing ONE job – that is calculating the average and displaying it.

Using a Procedure Call Name will result in the code for the procedure Name being executed. In Visual Studio 2008 call is not needed but it does make good sense to use it. We can call the procedure from anywhere (within our Main procedure or within other procedures). For example: Let’s look as Sub Example 1

Tasks Have a go at the classroom exercise worksheet on procedures on the intranet.

Objectives Review Understand the importance of modular programming. Know the role of procedures within programming. Understand the concept of parameters; ByRef and ByVal Use procedures within your programming.

Plenary What is meant by modular programming? Why is it important to break down a program into smaller parts – what are the benefits? What is a procedure? How do you declare them? How do you use them?