42. OCR A Level (H046-H446) SLR8 - 1.2 Introduction to programming part 3 procedures & functions
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
💻 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.
🎲 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.
📚 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
💡Procedure
💡Function
💡Object-Oriented Programming (OOP)
💡Method
💡Main Program
💡Procedure Call
💡Return Statement
💡Code Reusability
💡Flowchart
💡Pseudocode
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
this is part 3 of 6 of our video series
on the introduction to programming
in this video we look at subroutines
procedures and functions
so what actually is a subroutine well
it's nothing more
than a block of code given a unique
identifiable name within a program we
use subroutines to help us break down a
larger problem
into a series of smaller more manageable
problems
thus making the code easier to write
debug and reuse
the word subroutine is a generic term
and in a broad sense it covers two
concepts you need to know about
procedures those are blocks of code that
carry out a set task
and functions blocks of code that carry
out a set task
and in addition return a value
if you're thinking about
object-orientated programming we still
have procedures and functions
but in that context they're referred to
as methods
methods can either return a value or not
return a value
and so you can think of them in very
similar terms
but let's just have a brief look at a
program here
the first thing you will notice is there
are multiple
blocks of code or subroutines in fact we
have
four in the exam
you'll be able to spot subroutines as
they will start with either the word
procedure
or function and end with either
end procedure or end function
after the initial word you can then see
a name is given to that procedural
function
which uniquely identifies it in the
program
so this program has four procedures
called initialize
output 1 output 2 and adjust
you'll notice the actual main program
starts down the bottom
and this is quite typical that the
blocks of code the subroutines are
written at the top of the program
and your main program starts towards the
end so when we actually run this program
it starts at this line or strictly
speaking this is a comment so
it will start at the next line
the very first line of code in this
program therefore
is initialize open brackets close
brackets
this is what we call a procedure call
a call to another procedure
as soon as the program hits this line of
code it
searches for a subroutine with the same
name and then it
jumps to the code inside that subroutine
the program carries on executing the
code in that subroutine
until it reaches the end at which point
it returns to the place where it left
off originally
so in this case we go back to the main
part of the program at the bottom there
and we continue with the next line which
is the while line
in a similar fashion you can see the
main program
contains three other calls to procedures
and again each time we hit one of those
we would jump off to the appropriate
subroutine execute the code inside it
and then return in this way you'll
notice the actual main program itself
now has very little code it has some
logic in the form of a while loop
but most of the code is actually
contained elsewhere
in blocks of code this is a typical way
of programming
okay so now we've gone over the basics
let's actually look at a worked example
of procedures and functions
in the following python program so a
quick recap then we have two main types
of subroutines
there's procedures that carry out a task
and functions that carry out a task and
return a value
so the first thing to notice here with
this program is it actually
starts towards the very bottom of the
code listing
under the comments where it says main
program
the rest of the program is constructed
blocks
or subroutines
now you'll notice one of the very first
lines of code that's run
is print output throw
output throw is a call
to a subroutine
so what happens is when we reach output
throw
we search the rest of the code for a
subroutine with a matching name
we find one and so the code jumps
to that section of the program and
starts executing in sequence from that
point on
when the program reaches the end we can
see
it says return a value return the
contents of the variable
role at this point the program jumps
back
to where it left off before it entered
this subroutine because we're returning
a value
we know that this subroutine is actually
a function
you'll notice that as part of executing
the function
output throw we also have a call
to another sub program roll a dice
so again we would jump off at that point
to the subroutine roll a dice and we can
see again here
that roller dice returns a value
thus again making this a function
again a little bit further in the output
throw function
we see another call to a subroutine
this one called order dice so as soon as
we hit that line of code
the program jumps off to the order die
subroutine
and starts executing the code in there
again depending on the result of the if
statement if d1 is greater than equal d2
we hit one of two return statements
one of the other is going to get
executed and when we do that
we'll jump back into the output throw
function
and of course once again because order
dice returns a value
this is a function and not a procedure
so what are the advantages then of using
subroutines such as those shown
well programs are easy to write and
they're easy to debug
it creates reusable components
these functions can also be grouped in a
library
for easy reuse across many different
programs
indeed you do that in this program here
and you may have done something similar
in your own programs
the first line of code says import
random
and this imports the random library of
functions
into our program so we can then make use
of them
finally programs are simply easier to
test
if they're written in modules and blocks
of code
here we can see how subprograms are
called from inside flowcharts
there's a specific symbol used in
flowcharts
which tells you that you need to jump
off and start
executing another flowchart and when you
reach the
end of that flowchart you'd return from
the one that called it
and carried on so this is the flow chart
for the previous program we showed you
in python
pause the video for a second and just
work through it slowly
so you can see how you jump back and
forwards between the different flow
charts
as they're being called
now the example we work through here is
in python and you may all be using a
different language at a level such as c
sharp or java
but it's worth pointing out the format
that procedures and functions
will be displayed in in the ocr exams
so here you can see the syntax that they
will use for a procedure
and indeed for a function
so having watched this video you should
be able to answer the following key
question
what is the difference between a
procedure and a function
we're just going to go over a quick note
now about the language guide that's
used in your external assessments
so remember the example don't know what
language you've been learning to program
so exam questions might use an
unfamiliar syntax
towards the back of the specification
for both a s and a level you'll find
appendix 5d where the exampled state
the following guide shows the format
pseudo code will appear
in the examined components is provided
to allow
you to give learners familiarity before
the exam
learners are not expected to memorize
the syntax of the pseudocode
and when asked may provide answers in
any style of pseudo code they choose
providing its meaning could be
reasonably inferred
by a competent programmer
so although you don't have to answer in
the specific syntax shown in the exam
papers so you are familiar not thrown in
the exam
it's worth downloading a copy of the
specification and printing out the
appendix
if your school is a craving day
subscriber then ask your teacher for a
copy of our student learning
and exam support guide this provides all
the information you need
in a set of handy reference sheets
[Music]
you
5.0 / 5 (0 votes)