Coding Concepts (Sub- Programs)

Slides:



Advertisements
Similar presentations
Classes, methods, and conditional statements We’re past the basics. These are the roots.
Advertisements

1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Hello, world! Dissect HelloWorld.java Compile it Run it.
Python quick start guide
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Controlling Program Flow with Decision Structures.
CompSci 230 S Programming Techniques
Math operations 9/19/16.
Information and Computer Sciences University of Hawaii, Manoa
Topics Designing a Program Input, Processing, and Output
Chapter 7: Expressions and Assignment Statements
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Object Oriented Programming
Chapter 7: Expressions and Assignment Statements
Debugging and Random Numbers
Variables and Arithmetic Operators in JavaScript
Intro to C Tutorial 4: Arithmetic and Logical expressions
Introduction to C++ October 2, 2017.
Fundamental of Java Programming Basics of Java Programming
CompSci 230 Software Construction
Functions.
JavaScript: Functions.
Variables, Printing and if-statements
Engineering Innovation Center
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Teaching London Computing
Arithmetic operations, decisions and looping
Variables ICS2O.
Fundamentals of Programming
Introduction to Java, and DrJava part 1
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.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Coding Concepts (Data Structures)
PHP.
Topic 1: Problem Solving
We’re moving on to more recap from other programming languages
Coding Concepts (Basics)
Variables Title slide variables.
Logical Operations In Matlab.
Coding Concepts (Data- Types)
Objective The student will be able to:
Using Functions
Problem Solving Designing Algorithms.
Introduction to Java, and DrJava
SELECTIONS STATEMENTS
Chapter 3: Selection Structures: Making Decisions
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 3: Selection Structures: Making Decisions
Javascript Chapter 19 and 20 5/3/2019.
Introduction to Java, and DrJava part 1
Just Enough Java 17-May-19.
Using C++ Arithmetic Operators and Control Structures
CISC101 Reminders Assignment 3 due today.
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Controlling Program Flow
Software Development Techniques
COMPUTING.
Introduction to Python
Introduction to Computer Science
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

Coding Concepts (Sub- Programs) Topic 2: Programming Coding Concepts (Sub- Programs)

Creating Sub-Programs We mentioned when designing solutions that we can break our solution into smaller pieces Known as sub-programs We can do the same thing in a program We can take pieces of logically separate code And put them into separate places These sub-programs are referred to as procedures and functions Programming: Sub-Programs

Creating Sub-Programs There is a difference between these two terms Procedures: sub-programs that do something Functions: sub-programs that return information We create procedures any time we want to run a series of instructions multiple times Like outputting data to the console We create functions any time we want to calculate something multiple times Like calculating the distance between two points Programming: Sub-Programs

Programming: Sub-Programs Creating Procedures Let’s start by creating a procedure Java Python In Python, we start procedures with def (define). We then give the procedure a name and some brackets. The code indented after belongs to the procedure and is run when the procedure is run. C# and Java use the same syntax for making a procedure, but use curly braces instead of indentation for its code. C# Programming: Sub-Programs

Programming: Sub-Programs Create a program that contains two procedures Procedure 1 Asks for the user’s name, and says hello to them Procedure 2 Asks for the user’s height, and outputs whether they are too short or tall enough to ride a rollercoaster The rollercoaster has a height requirement of 1.4 metres Programming: Sub-Programs

Programming: Sub-Programs Creating Functions The difference between a procedure and a function is that a function needs to return some information Typically, functions will return a value of some kind (relevant to the function) For example, we’re making a function that calculates whether a number is prime Since this is a “yes or no” question, the function should return a boolean Programming: Sub-Programs

Programming: Sub-Programs Creating Functions Here’s an example of creating a function that randomly generates either true or false Java C# Python The important bit here is the use of the keyword return. This takes the value to its right and moves it to where this function was called. Programming: Sub-Programs

Programming: Sub-Programs Create two functions in your program Function 1 Generates a random number and returns whether it is even or odd Function 2 Generates a random width and height for a rectangle, and then returns the area of that rectangle Programming: Sub-Programs

Programming: Sub-Programs Using Parameters Not all functions can, or need, to generate random numbers Sometimes maybe we want to specify the value a function uses We may want to do the same thing with a procedure This is where we can make use of things called parameters These are values that can be passed in to the procedure/function for it to use Programming: Sub-Programs

Programming: Sub-Programs Using Parameters For example, whenever we generate a random number, we can put an upper/lower bound on it This number that we type in gets passed to a parameter in that function On a side note, we call values that get passed in to parameters arguments The number 101 here is an argument. It gets passed into a parameter of this function Next() Programming: Sub-Programs

Programming: Sub-Programs Using Parameters We can include parameters in procedures/functions simply by adding variables to the brackets After the procedure’s/function’s name This follows the same variable standards/syntax as the rest of the programming language Python: only need to include a name C# and Java: need to include a data-type and a name Programming: Sub-Programs

Programming: Sub-Programs Using Parameters Here is an example of the rectangle area function, but this time using parameters These are all simple functions Some procedures/functions we make may be more complex For example, we could make a function for any of the sorting/searching algorithms Python C# Java Programming: Sub-Programs

Programming: Sub-Programs Add one more function and one more procedure to your program Function Takes a base and a height (integers), and uses them to return the area of a triangle Procedure Takes an array of integers and outputs them, on the same line, to the console (with square brackets [ ] around the numbers) E.g. [1, 5, 3, 7] Programming: Sub-Programs

Programming: Sub-Programs Global Variables In any program, there may be a value we want to keep track of over the whole program Not just in a single procedure/function Any variable we create inside a procedure/function is known as a local variable As it can only be used inside that procedure/function Two variables, with the same name in different procedures/functions, are considered entirely different If we make a variable outside of any procedure/function, it is global Any procedure/function can use it Programming: Sub-Programs

Programming: Sub-Programs Global Variables Python Java C# Note that Python needs to include global before the global variable’s name in a procedure/function. This is only needed if that variable’s value will change (but it’s safe putting it in even when its not). Java and C# make/use the variable as normal, except for the added public static in front of it (like a procedure/function). Programming: Sub-Programs

Programming: Sub-Programs Operators We should also take the time to recap the different operators we can use in Python, C#, and Java They each have access to the same operators, and are handled largely the same way These operators are split into three categories Arithmetic Operators: +, -, *, /, %, // (Python only) Relational Operators: ==, <, >, !=, <=, >= Logical Operators: and/&&, or/||, not/! Programming: Sub-Programs

Programming: Sub-Programs Operators Arithmetic Operators +: Addition (adding the left and right sides) -: Subtraction (subtracts the right side from the left) *: Multiplication (multiplies the left and right sides) /: Division (divides the left side by the right) %: Modulo (calculates the remainder from dividing the left by the right) // (Python only): Integer division (returns the division result as an integer) Logical Operators && (and): Boolean AND (true if both left and right sides are true) || (or): Boolean OR (true if either left or right sides are true) ! (not): Boolean NOT (true if right side is false) Programming: Sub-Programs

Programming: Sub-Programs Operators Relational Operators ==: Equals (returns true if left and right sides are the same) <: Less-than (returns true if the left side is smaller than the right side) >: Greater-than (returns true if the left side is greater than the right side) !=: Not-equal (returns true if the left and right sides are not the same) <=: Less-than or equal-to (returns true if the left side is smaller, or the same, as the right side) >=: Greater-than or equal-to (returns true if the left side is greater, or the same, as the right side) Programming: Sub-Programs

Programming: Sub-Programs Create a program that generates an array of random integers, outputs them, sorts them, and outputs them again However, put the array generation, array output, and array sorting in different functions/procedures Programming: Sub-Programs