Structured Programming

Slides:



Advertisements
Similar presentations
Microsoft® Small Basic
Advertisements

Tutorial #6 PseudoCode (reprise)
Flow Charting, Structured English and PseudoCode
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Introduction to Methods
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Python quick start guide
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Python: Modularisation Damian Gordon. Modularisation Remember the prime checker program:
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
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.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Methods We write methods in our programs for many reasons:
Modularisation Damian Gordon. Modularisation Let’s imagine we had code as follows:
C++ Pointers Review. Overview  What is a pointer  Why do I care?  What can be 'pointed to'?  Example.
I Power Higher Computing Software Development High Level Language Constructs.
Python: Sorting - Bubblesort Damian Gordon. Sorting: Bubblesort The simplest algorithm for sort an array is called BUBBLE SORT. It works as follows for.
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
Chapter 6 Methods Chapter 6 - Methods.
Prime Numbers Damian Gordon. Prime Numbers So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version of the prime number checking program:
Recursion Damian Gordon. Recursion Factorial Fibonacci Decimal to Binary conversion Travelling Salesman Problem Knight’s Tour.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
Identify the Appropriate Method for Handling Repetition
Introduction to Python
Comments on the “Three Piles” Problem
Introduction to Python
Pseudocode (Revision)
Functions Review.
IF statements.
Linked Lists Damian Gordon.
CS21b: Structure and Interpretation
Recursion 12-Nov-18.
Factors, multiple, primes: Factors from prime factors
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company. 1
Adapted from slides by Marty Stepp and Stuart Reges
Boolean Logic Damian Gordon.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
Lesson 16: Functions with Return Values
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2013.
Recursion 2-Dec-18.
Recursion 2-Dec-18.
Coding Concepts (Sub- Programs)
Logical assertions assertion: A statement that is either true or false. Examples: Java was created in The sky is purple. 23 is a prime number. 10.
Chapter 8 Namespaces and Memory Realities
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2016.
Factors, multiple, primes: Prime factors
Recursion 29-Dec-18.
Python: Algorithms Damian Gordon.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2018.
More on Functions (Part 2)
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Zach Tatlock Winter 2018.
If, Subroutines and Functions
Java Coding 4 (part2) David Davenport Computer Eng. Dept.,
Building Java Programs
Loops and Simple Functions
COMPUTER PROGRAMMING SKILLS
Introduction to C++ Programming Language
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2017.
Some Common Issues: Common Algorithms
Factors, multiple, primes: Multiples
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2019.
REPETITION Why Repetition?
Python Modules.
Coming up Variables Quick look at scope Primitives Objects
Presentation transcript:

Structured Programming Damian Gordon

Structured Programming In this lesson we will cover: Modules Return values Parameter passing Variable scope Local and Global variables.

Modularisation We’ve seen the idea of modules before, it allows us to take a program like this:

Modularisation PROGRAM CheckPrime: Read A; B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; IF (IsPrime = FALSE) THEN Print “Not Prime”; ELSE Print “Prime”; END.

Modularisation And turn it into this:

MODULE PrimeChecker(): Read A; B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; RETURN IsPrime; END. PROGRAM CheckPrime: IF (PrimeChecker() = FALSE) THEN Print “Not Prime”; ELSE Print “Prime”;

Modularisation Modularisation make life easier for a lot of reasons: It easier for someone else to understand how the code works It makes team programming a lot easier, different programmers can work on different methods Can improve the quality of the code Can reuse the same code over and over again (“don’t reinvent the wheel”).

MODULE PrimeChecker(): Read A; B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; RETURN IsPrime; END. PROGRAM CheckPrime: IF (PrimeChecker() = FALSE) THEN Print “Not Prime”; ELSE Print “Prime”;

MODULE PrimeChecker(): Read A; B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; RETURN IsPrime; END. PROGRAM CheckPrime: IF (PrimeChecker() = FALSE) THEN Print “Not Prime”; ELSE Print “Prime”;

Return Values We can use the keyword RETURN to pass a value back from the module into the main program (or whatever module is calling it). We typically only return one value using the RETURN keyword. It is up to the calling program to capture whatever value is passed back to it.

Parameter Passing In this version of the program, the MAIN program calls the MODULE PrimeChecker() which does the reading in of the value, and the checking to see if it is prime or not. We might wish to modify the code so that the MAIN program reads in the value, and then calls the MODULE PrimeChecker() to check if it is prime or not. If we look at it again as it is:

MODULE PrimeChecker(): Read A; B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; RETURN IsPrime; END. PROGRAM CheckPrime: IF (PrimeChecker() = FALSE) THEN Print “Not Prime”; ELSE Print “Prime”;

Read in value Check if it is prime Print out answer MODULE PrimeChecker(): Read A; B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; RETURN IsPrime; END. PROGRAM CheckPrime: IF (PrimeChecker() = FALSE) THEN Print “Not Prime”; ELSE Print “Prime”; Read in value Check if it is prime Print out answer

Parameter Passing In terms of good module design, we have a rule-of-thumb, that each module should do one thing well, as opposed to two or three things kinda well! At the moment the MODULE PrimeChecker() does two things, reads in the value, and then checks, so we should rewrite so that the MODULE called PrimeChecker() just checks if a number is prime (and that number is passed into it).

MODULE PrimeChecker(): Read A; B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; RETURN IsPrime; END. PROGRAM CheckPrime: IF (PrimeChecker() = FALSE) THEN Print “Not Prime”; ELSE Print “Prime”;

MODULE PrimeChecker(): Read A; B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; RETURN IsPrime; END. PROGRAM CheckPrime: IF (PrimeChecker() = FALSE) THEN Print “Not Prime”; ELSE Print “Prime”;

MODULE PrimeChecker(): Read A; B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; RETURN IsPrime; END. PROGRAM CheckPrime: IF (PrimeChecker() = FALSE) THEN Print “Not Prime”; ELSE Print “Prime”; MODULE PrimeChecker(A): B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; RETURN IsPrime; END. PROGRAM CheckPrime: Read Val; IF (PrimeChecker(Val) = FALSE) THEN Print “Not Prime”; ELSE Print “Prime”;

MODULE PrimeChecker(): Read A; B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; RETURN IsPrime; END. PROGRAM CheckPrime: IF (PrimeChecker() = FALSE) THEN Print “Not Prime”; ELSE Print “Prime”; MODULE PrimeChecker(A): B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; RETURN IsPrime; END. PROGRAM CheckPrime: Read Val; IF (PrimeChecker(Val) = FALSE) THEN Print “Not Prime”; ELSE Print “Prime”;

Check if it is prime Read in value Print out answer MODULE PrimeChecker(A): B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; RETURN IsPrime; END. PROGRAM CheckPrime: Read Val; IF (PrimeChecker(Val) = FALSE) THEN Print “Not Prime”; ELSE Print “Prime”; Check if it is prime Read in value Print out answer

Parameter Passing We can define the module to take in as many parameters as we like between the brackets. def MyModule1(): def MyModule2(a): def MyModule3(a, b): def MyModule4(a, b, c): def MyModule5(a, b, c, d): And when we call the module we have to call it with the same number of parameters.

MODULE PrimeChecker(A): B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; RETURN IsPrime; END. PROGRAM CheckPrime: Read Val; IF (PrimeChecker(Val) = FALSE) THEN Print “Not Prime”; ELSE Print “Prime”;

MODULE PrimeChecker(A): B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; RETURN IsPrime; END. PROGRAM CheckPrime: Read Val; IF (PrimeChecker(Val) = FALSE) THEN Print “Not Prime”; ELSE Print “Prime”;

MODULE PrimeChecker(A): B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; RETURN IsPrime; END. In another program we could call the module with values going directly into the module, e.g. PrimeChecker(23); PrimeChecker(21);

Variable Scope def Method1(): variable1 def Method2(): variableX

Variable Scope The scope of a variable – is the part of a computer program where the binding is valid: where the variable name can be used to refer to the entity. In other parts of the program the variable name may refer to a different entity (it may have a different binding), or to nothing at all (it may be unbound). The scope of a binding is also known as the “visibility” of a variable.

Variable Scope def Method1(): variable1 def Method2(): variableX

Variable Scope MODULE MyMethod(): print Global_var; END. PROGRAM CheckPrime: Global_var = “Global Variable” MyMethod();

Variable Scope Global variable MODULE MyMethod(): print Global_var; END. PROGRAM CheckPrime: Global_var = “Global Variable” MyMethod(); Global variable

Variable Scope MODULE MyMethod(): Global_var = “Local copy” print Global_var; END. PROGRAM CheckPrime: Global_var = “Global Variable” MyMethod();

Variable Scope Local copy Global variable MODULE MyMethod(): Global_var = “Local copy” print Global_var; END. PROGRAM CheckPrime: Global_var = “Global Variable” MyMethod(); Local copy Global variable

Side Effects A call to a Module M has a side effect if either During the call, there is a change to the value of some object that existed before M. I/O occurs during the call. Otherwise the call is side-effect free.

etc.