42. OCR A Level (H046-H446) SLR8 - 1.2 Introduction to programming part 3 procedures & functions

Craig'n'Dave
29 Sept 202010:36

Summary

TLDRIn this segment of a programming video series, the focus is on subroutines, procedures, and functions. Subroutines are blocks of code with unique names that simplify complex problems into manageable parts, aiding in code writing, debugging, and reuse. Procedures are subroutines that perform tasks without returning values, while functions perform tasks and return values. In object-oriented programming, these are known as methods. The video uses a Python program to illustrate how subroutines are called and executed, highlighting their role in making code modular, reusable, and easier to test. It also touches on pseudocode formatting for exams and the benefits of using subroutines.

Takeaways

  • 💡 A subroutine is a block of code with a unique name that simplifies programming by breaking down complex problems into smaller, manageable tasks.
  • 🔍 The term 'subroutine' encompasses two concepts: 'procedures', which are blocks of code that perform a task, and 'functions', which perform a task and return a value.
  • 📚 In object-oriented programming, subroutines are referred to as 'methods', which can either return a value or not, similar to functions and procedures.
  • 👀 Subroutines in a program are typically defined at the top, with the main program starting at the bottom, making the structure clear and organized.
  • 🔑 The main program calls subroutines, which are executed in sequence until completion, after which control returns to the main program.
  • 🎯 Procedures and functions are fundamental to programming, with functions being a type of subroutine that returns a value, useful for calculations and data manipulation.
  • 🧩 Subroutines promote code reusability, making it easier to maintain and test programs, as they can be grouped into libraries for use across different programs.
  • 📝 The script introduces a Python program example to illustrate how subroutines are defined and called within a program, emphasizing practical application.
  • 🔄 Flowcharts use specific symbols to represent the calling of subroutines, showing the flow of control between different parts of a program.
  • 📖 For exams, students are not expected to memorize pseudocode syntax but should be familiar with it, as exam questions might use unfamiliar syntax.

Q & A

  • What is a subroutine?

    -A subroutine is a block of code given a unique, identifiable name within a program. It is used to break down a larger problem into smaller, more manageable problems, making the code easier to write, debug, and reuse.

  • What is the difference between a procedure and a function?

    -Procedures are blocks of code that carry out a set task, while functions are blocks of code that carry out a set task and also return a value.

  • In the context of object-oriented programming, what are procedures and functions called?

    -In object-oriented programming, procedures and functions are referred to as methods. Methods can either return a value or not.

  • How are subroutines identified in a program?

    -Subroutines can be identified as they start with either the word 'procedure' or 'function' and end with 'end procedure' or 'end function'.

  • What is a procedure call?

    -A procedure call is when a program jumps to a subroutine with the same name and starts executing the code inside it until it reaches the end, at which point it returns to the place where it left off originally.

  • Why are subroutines useful in programming?

    -Subroutines are useful because they make programs easier to write and debug, create reusable components, can be grouped in libraries for easy reuse across different programs, and make programs easier to test.

  • How does a function differ from a procedure in terms of its execution?

    -A function differs from a procedure in that it returns a value, which is typically the contents of a variable at the point of return, whereas a procedure does not return a value.

  • What is the purpose of the 'import random' line in the provided Python program?

    -The 'import random' line imports the random library of functions into the program, allowing the use of its functionalities within the program.

  • What is pseudocode and why is it used in exams?

    -Pseudocode is a high-level description of the operating logic of a program or other algorithm. It is used in exams to provide a familiar format for learners, regardless of the programming language they have been learning, to demonstrate their understanding of programming concepts.

  • Why is it important for students to be familiar with the language guide used in external assessments?

    -It is important for students to be familiar with the language guide used in external assessments to understand the format and syntax that procedures and functions will be displayed in during the exams, even if they are not expected to memorize or replicate the exact syntax.

  • How does the use of subroutines affect the main program's code?

    -The use of subroutines typically results in the main program having very little code, with most of the functionality contained in separate blocks or subroutines, which are called from within the main program.

Outlines

00:00

💻 Understanding Subroutines in Programming

This paragraph introduces the concept of subroutines in programming, explaining that they are blocks of code with unique names used to simplify complex problems into smaller, more manageable tasks. It differentiates between procedures, which are subroutines that perform tasks, and functions, which are subroutines that also return a value. The paragraph further clarifies that in object-oriented programming, these are known as methods. It also discusses the typical structure of a program where subroutines are defined at the top and the main program at the bottom, illustrating how a program executes by calling these subroutines. The paragraph concludes with an example of a program with four procedures and explains how the main program interacts with these subroutines through procedure calls.

05:00

🎲 Exploring Procedures and Functions in Python

The second paragraph delves into a Python program to demonstrate the practical application of procedures and functions. It explains how a function differs from a procedure by returning a value. The paragraph walks through the program's execution, highlighting how the program jumps to different subroutines upon function calls and returns to the main program after completing the subroutine's execution. It also touches on the benefits of using subroutines, such as ease of writing and debugging, reusability, and the ability to group them into libraries. The paragraph emphasizes the importance of understanding the flow of control in programs, including the use of flowcharts to visualize the calling of subroutines. It concludes with a note on the language guide used in external assessments, advising students to familiarize themselves with the syntax of procedures and functions as they may appear in exams, even if they are not required to memorize or replicate the exact syntax.

10:00

📚 Additional Resources for Programming Education

The final paragraph offers guidance on additional resources for learning programming, suggesting that students download a copy of the specification and print out the appendix for reference. It mentions that schools with a specific subscription can provide students with a student learning and exam support guide, which contains valuable reference materials. The paragraph concludes with a musical note, indicating the end of the video script.

Mindmap

Keywords

💡Subroutine

A subroutine is a segment of code that can be reused throughout a program. It is defined by a unique name and can be called from different parts of the program to perform a specific task. In the context of the video, subroutines are used to break down complex problems into smaller, more manageable ones, thereby simplifying the coding process. The script mentions that subroutines are written at the top of the program and are called from the main program, which is typical programming practice.

💡Procedure

A procedure is a type of subroutine that performs a specific task but does not return a value. It is a block of code that is executed when called. In the video script, procedures are described as a broad concept that includes methods in object-oriented programming. The script provides examples of procedures named 'initialize', 'output 1', 'output 2', and 'adjust', which are called from the main program.

💡Function

A function is a type of subroutine that not only performs a task but also returns a value. This is a crucial aspect that differentiates functions from procedures. In the video, functions are shown to be called within the main program and other subroutines, with examples like 'output throw' and 'order dice', which return values and thus influence the flow of the program.

💡Object-Oriented Programming (OOP)

Object-oriented programming is a programming paradigm that uses objects and classes. In OOP, procedures and functions are often referred to as methods. Methods are associated with objects and can manipulate the object's data. The video script mentions that in OOP, whether a method returns a value or not is a key consideration, similar to the distinction between procedures and functions.

💡Method

In the context of object-oriented programming, a method is a function or procedure that is associated with an object. Methods can modify the state of an object or return information about an object. The video script explains that methods can be thought of in similar terms to procedures and functions, but they are used within the context of classes and objects.

💡Main Program

The main program is the primary section of code that runs when a program is executed. It often contains the entry point of the program and coordinates the execution of various subroutines. In the video, the main program is depicted as starting at the bottom of the code and calling various subroutines, such as 'initialize' and 'output 1', to perform tasks.

💡Procedure Call

A procedure call is the act of invoking a procedure from within a program. This causes the program to pause its current execution, jump to the procedure's code, and execute it. The video script illustrates this with the 'initialize' procedure call, which is the first line of executable code in the program, highlighting how the program execution jumps to the subroutine and then returns.

💡Return Statement

A return statement is used in functions to send back a value to the caller. It is a key feature of functions that differentiates them from procedures. In the video script, the 'output throw' function is shown to use a return statement to pass the value of a variable back to the main program, demonstrating how functions can influence the program's logic through returned values.

💡Code Reusability

Code reusability refers to the ability to use the same code in different parts of a program or in different programs. Subroutines, procedures, and functions enhance reusability by encapsulating functionality, which can then be called multiple times without rewriting the code. The video emphasizes the benefits of subroutines in creating reusable components that can be grouped into libraries for easy reuse.

💡Flowchart

A flowchart is a diagrammatic representation of the flow of a program, showing the steps and decisions in a visual format. In the video, flowcharts are mentioned as a way to represent the calling of subroutines, with a specific symbol indicating where the program should jump to execute another flowchart and then return to the calling flowchart.

💡Pseudocode

Pseudocode is an informal high-level description of the operating principle of a computer program or other algorithm. It is used to outline the structure of a program without getting into the details of the actual coding syntax. The video script notes that learners are not expected to memorize the syntax of pseudocode but should be familiar with it to understand exam questions, which may use unfamiliar syntax.

Highlights

Subroutines are blocks of code with a unique name used to break down larger problems into smaller ones.

Subroutines make code easier to write, debug, and reuse.

The term 'subroutine' covers procedures and functions.

Procedures are blocks of code that carry out a task.

Functions are blocks of code that carry out a task and return a value.

In object-oriented programming, procedures and functions are referred to as methods.

Methods can return a value or not, similar to functions and procedures.

Subroutines are identified by starting with 'procedure' or 'function' and ending with 'end procedure' or 'end function'.

The main program typically starts at the bottom of the code, after the subroutines.

A procedure call is used to execute code within a subroutine.

When a subroutine is called, the program jumps to it, executes its code, and then returns to the main program.

The main program contains logic and calls to various subroutines.

Subroutines help to minimize the amount of code in the main program.

Procedures and functions are demonstrated in a worked example using Python.

OutputThrow is a function that returns a value and includes calls to other subroutines.

RollADice and OrderDice are functions that return values based on dice outcomes.

Subroutines allow for code reusability and can be grouped into libraries.

The 'random' library is imported to provide additional functions for the program.

Subprograms are called from within flowcharts using a specific symbol indicating a jump to another chart.

Exam questions may use unfamiliar syntax, but understanding the concept is key.

Appendix 5d of the specification provides a guide to the format of pseudocode used in exams.

Learners are not expected to memorize pseudocode syntax but should understand its meaning.

A student learning and exam support guide is available for additional reference.

Transcripts

play00:00

this is part 3 of 6 of our video series

play00:03

on the introduction to programming

play00:06

in this video we look at subroutines

play00:08

procedures and functions

play00:16

so what actually is a subroutine well

play00:19

it's nothing more

play00:20

than a block of code given a unique

play00:24

identifiable name within a program we

play00:30

use subroutines to help us break down a

play00:32

larger problem

play00:33

into a series of smaller more manageable

play00:35

problems

play00:36

thus making the code easier to write

play00:40

debug and reuse

play00:44

the word subroutine is a generic term

play00:47

and in a broad sense it covers two

play00:49

concepts you need to know about

play00:51

procedures those are blocks of code that

play00:54

carry out a set task

play00:56

and functions blocks of code that carry

play00:59

out a set task

play01:00

and in addition return a value

play01:06

if you're thinking about

play01:07

object-orientated programming we still

play01:10

have procedures and functions

play01:12

but in that context they're referred to

play01:14

as methods

play01:16

methods can either return a value or not

play01:19

return a value

play01:20

and so you can think of them in very

play01:21

similar terms

play01:25

but let's just have a brief look at a

play01:27

program here

play01:31

the first thing you will notice is there

play01:33

are multiple

play01:34

blocks of code or subroutines in fact we

play01:38

have

play01:38

four in the exam

play01:41

you'll be able to spot subroutines as

play01:43

they will start with either the word

play01:45

procedure

play01:46

or function and end with either

play01:49

end procedure or end function

play01:53

after the initial word you can then see

play01:56

a name is given to that procedural

play01:57

function

play01:58

which uniquely identifies it in the

play02:00

program

play02:01

so this program has four procedures

play02:03

called initialize

play02:04

output 1 output 2 and adjust

play02:10

you'll notice the actual main program

play02:13

starts down the bottom

play02:14

and this is quite typical that the

play02:16

blocks of code the subroutines are

play02:18

written at the top of the program

play02:19

and your main program starts towards the

play02:21

end so when we actually run this program

play02:24

it starts at this line or strictly

play02:27

speaking this is a comment so

play02:28

it will start at the next line

play02:32

the very first line of code in this

play02:35

program therefore

play02:36

is initialize open brackets close

play02:39

brackets

play02:40

this is what we call a procedure call

play02:43

a call to another procedure

play02:46

as soon as the program hits this line of

play02:48

code it

play02:50

searches for a subroutine with the same

play02:52

name and then it

play02:53

jumps to the code inside that subroutine

play02:59

the program carries on executing the

play03:01

code in that subroutine

play03:03

until it reaches the end at which point

play03:06

it returns to the place where it left

play03:08

off originally

play03:10

so in this case we go back to the main

play03:11

part of the program at the bottom there

play03:13

and we continue with the next line which

play03:15

is the while line

play03:20

in a similar fashion you can see the

play03:22

main program

play03:23

contains three other calls to procedures

play03:26

and again each time we hit one of those

play03:29

we would jump off to the appropriate

play03:30

subroutine execute the code inside it

play03:33

and then return in this way you'll

play03:36

notice the actual main program itself

play03:39

now has very little code it has some

play03:42

logic in the form of a while loop

play03:44

but most of the code is actually

play03:46

contained elsewhere

play03:47

in blocks of code this is a typical way

play03:52

of programming

play03:55

okay so now we've gone over the basics

play03:57

let's actually look at a worked example

play03:59

of procedures and functions

play04:01

in the following python program so a

play04:04

quick recap then we have two main types

play04:06

of subroutines

play04:07

there's procedures that carry out a task

play04:10

and functions that carry out a task and

play04:14

return a value

play04:18

so the first thing to notice here with

play04:20

this program is it actually

play04:22

starts towards the very bottom of the

play04:24

code listing

play04:25

under the comments where it says main

play04:27

program

play04:29

the rest of the program is constructed

play04:32

blocks

play04:32

or subroutines

play04:37

now you'll notice one of the very first

play04:39

lines of code that's run

play04:41

is print output throw

play04:45

output throw is a call

play04:49

to a subroutine

play04:52

so what happens is when we reach output

play04:54

throw

play04:55

we search the rest of the code for a

play04:57

subroutine with a matching name

play05:00

we find one and so the code jumps

play05:03

to that section of the program and

play05:05

starts executing in sequence from that

play05:08

point on

play05:12

when the program reaches the end we can

play05:15

see

play05:15

it says return a value return the

play05:18

contents of the variable

play05:20

role at this point the program jumps

play05:24

back

play05:24

to where it left off before it entered

play05:28

this subroutine because we're returning

play05:31

a value

play05:32

we know that this subroutine is actually

play05:35

a function

play05:40

you'll notice that as part of executing

play05:43

the function

play05:44

output throw we also have a call

play05:47

to another sub program roll a dice

play05:52

so again we would jump off at that point

play05:55

to the subroutine roll a dice and we can

play05:59

see again here

play06:00

that roller dice returns a value

play06:04

thus again making this a function

play06:11

again a little bit further in the output

play06:13

throw function

play06:15

we see another call to a subroutine

play06:18

this one called order dice so as soon as

play06:20

we hit that line of code

play06:22

the program jumps off to the order die

play06:24

subroutine

play06:25

and starts executing the code in there

play06:29

again depending on the result of the if

play06:31

statement if d1 is greater than equal d2

play06:35

we hit one of two return statements

play06:38

one of the other is going to get

play06:39

executed and when we do that

play06:42

we'll jump back into the output throw

play06:44

function

play06:45

and of course once again because order

play06:48

dice returns a value

play06:49

this is a function and not a procedure

play06:55

so what are the advantages then of using

play06:58

subroutines such as those shown

play07:00

well programs are easy to write and

play07:02

they're easy to debug

play07:03

it creates reusable components

play07:07

these functions can also be grouped in a

play07:09

library

play07:10

for easy reuse across many different

play07:14

programs

play07:16

indeed you do that in this program here

play07:19

and you may have done something similar

play07:20

in your own programs

play07:22

the first line of code says import

play07:24

random

play07:25

and this imports the random library of

play07:28

functions

play07:29

into our program so we can then make use

play07:31

of them

play07:35

finally programs are simply easier to

play07:38

test

play07:38

if they're written in modules and blocks

play07:41

of code

play07:46

here we can see how subprograms are

play07:48

called from inside flowcharts

play07:51

there's a specific symbol used in

play07:53

flowcharts

play07:54

which tells you that you need to jump

play07:56

off and start

play07:58

executing another flowchart and when you

play08:01

reach the

play08:01

end of that flowchart you'd return from

play08:04

the one that called it

play08:05

and carried on so this is the flow chart

play08:09

for the previous program we showed you

play08:11

in python

play08:12

pause the video for a second and just

play08:14

work through it slowly

play08:15

so you can see how you jump back and

play08:17

forwards between the different flow

play08:19

charts

play08:19

as they're being called

play08:24

now the example we work through here is

play08:25

in python and you may all be using a

play08:27

different language at a level such as c

play08:29

sharp or java

play08:31

but it's worth pointing out the format

play08:34

that procedures and functions

play08:36

will be displayed in in the ocr exams

play08:39

so here you can see the syntax that they

play08:42

will use for a procedure

play08:44

and indeed for a function

play08:47

so having watched this video you should

play08:49

be able to answer the following key

play08:51

question

play08:52

what is the difference between a

play08:54

procedure and a function

play08:57

we're just going to go over a quick note

play08:59

now about the language guide that's

play09:01

used in your external assessments

play09:06

so remember the example don't know what

play09:08

language you've been learning to program

play09:10

so exam questions might use an

play09:12

unfamiliar syntax

play09:14

towards the back of the specification

play09:16

for both a s and a level you'll find

play09:18

appendix 5d where the exampled state

play09:22

the following guide shows the format

play09:24

pseudo code will appear

play09:26

in the examined components is provided

play09:29

to allow

play09:30

you to give learners familiarity before

play09:33

the exam

play09:34

learners are not expected to memorize

play09:36

the syntax of the pseudocode

play09:38

and when asked may provide answers in

play09:40

any style of pseudo code they choose

play09:42

providing its meaning could be

play09:44

reasonably inferred

play09:46

by a competent programmer

play09:51

so although you don't have to answer in

play09:54

the specific syntax shown in the exam

play09:57

papers so you are familiar not thrown in

play10:00

the exam

play10:01

it's worth downloading a copy of the

play10:02

specification and printing out the

play10:04

appendix

play10:05

if your school is a craving day

play10:07

subscriber then ask your teacher for a

play10:09

copy of our student learning

play10:11

and exam support guide this provides all

play10:13

the information you need

play10:15

in a set of handy reference sheets

play10:21

[Music]

play10:35

you

Rate This

5.0 / 5 (0 votes)

関連タグ
ProgrammingSubroutinesProceduresFunctionsCode BlocksDebuggingCode ReusePython ExamplesOOP MethodsPseudocode
英語で要約が必要ですか?