Presentation is loading. Please wait.

Presentation is loading. Please wait.

Coding Concepts (Sub- Programs)

Similar presentations


Presentation on theme: "Coding Concepts (Sub- Programs)"— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20


Download ppt "Coding Concepts (Sub- Programs)"

Similar presentations


Ads by Google