Study Web

Weekly Programming Topics

Procedures, Functions and Structure Charts

Lesson 2 · Procedures, Functions and Structure charts

Procedures, Functions and Structure Charts

Procedures, Functions and Structure charts

What is in this lesson?

Procedures Functions Documenting code Local and Global scoping Methods, attributes and classes Hierarchy charts 10th January 2026 Tram Tran 2

Procedures

▶ Procedure is a sequence of instructions designed to perform a specific task. ▶ Normally, it is in the form of a block of code. ▶ Any interaction a procedure performs with the outside world is called a side effect. 10th January 2026 Tram Tran 3

Passing Arguments

▶ Procedures can take arguments. ▶ Arguments are data that is sent (passed) from one block of code to another. ▶ Arguments are given names (like a variable name) in the receiving procedure. ▶ A procedure can take multiple arguments. ▶ Variables can be used to pass values to a procedure. 10th January 2026 Tram Tran 4

Functions

Divide and Conquer [1, Ch. 5] ▶ Most programs are large enough to be broken down into smaller pieces known as functions. ▶ A program that has been written with each task in its own function is called a modularised program. 10th January 2026 Tram Tran 5

▶ Simpler code

▶ Code reuse ▶ Better testing ▶ Faster development ▶ Teamwork

Figure: Using functions to divide and: conquer a large task
  • 10th January 2026 Tram Tran 6
  • Functions
  • Function
  • A function is a group of statements that exist within a program for
  • the purpose of performing a specific task.

10th January 2026 Tram Tran 6

Functions

Function A function is a group of statements that exist within a program for the purpose of performing a specific task.

Figure: The function definition and the function call: 10th January 2026 Tram Tran 7
  • Function with arguments [1, Ch. 5]
  • Argument
  • An argument is any piece of data that is passed into a function
  • when the function is called.
  • Parameter

Function with arguments [1, Ch. 5] Argument An argument is any piece of data that is passed into a function when the function is called. Parameter A parameter is a variable that receives an argument passed into a function. 10th January 2026 Tram Tran 8

10th January 2026 Tram Tran 9

Void Functions and Value-Returning Functions [1, Ch. 5] ▶ A Void function executes the statements it contains and then terminates without returning a value. ▶ A Value-returning function executes the statements it contains and then returns a value back to where it was called. ▶ A value-returning function has areturn statement that returns a value back to the part of the program that called it. 10th January 2026 Tram Tran 10

10th January 2026 Tram Tran 11

Indentation in Python [1, Ch. 5] ▶ In Python, each line in a block must be indented. ▶ In a block, each line has to be indented with the same number of spaces. Otherwise, an error will occur. 10th January 2026 Tram Tran 12

Documenting code

▶ Comments explain the code in plain words. ▶ Comments are ignored during compilation and execution. Use comments before functions and procedures to explain: ▶ How to use it ▶ What arguments it takes ▶ What it returns Try to avoid explaining how your code works. 10th January 2026 Tram Tran 13

Local and Global scoping

Local Variables [1, Ch. 5] Concept A local variable is created inside a function and cannot be accessed by statements that are outside the function. ▶ An error will occur if a statement in one function tries to access a local variable of another function. ▶ Different functions can have local variables with the same names. 10th January 2026 Tram Tran 14

Scope and Local Variables [1, Ch. 5] A variable’s scope is the part of a program in which the variable may be accessed. 10th January 2026 Tram Tran 15

Global Variables [1, Ch. 5] Concept A global variable is accessible to all the functions in a program. 10th January 2026 Tram Tran 16

Restrict the use of global variables [1, Ch. 5] ▶ Any statement in the program can change the value of a global variable. ▶ Functions that use global variables are usually dependent on those variables. ▶ Global variables make a program hard to understand. 10th January 2026 Tram Tran 17

Global Constants [1, Ch. 5] Concept A global constant is a global name that references a value that cannot be changed. 10th January 2026 Tram Tran 18

Methods, attributes and classes

Procedural programming and Object-oriented programming [1, Ch. 10]

Procedural programming Procedural programming is a method of writing software, centered on the procedures or actions that take place in a program. Object-oriented programming Object-oriented programming (OOP) is centered on objects. 10th January 2026 Tram Tran 19

Methods, Attributes and Classes

▶ Methods: an OOP term for both functions and procedures. ▶ Attributes: variables, also functions or methods that might be used to give values as though you were accessing a variable. ▶ Class: in this unit,class is utilised as a way of grouping variables and methods. ▶ Objects: are created from abstract data types that encapsulate data and functions. 10th January 2026 Tram Tran 20

Hierarchy charts

One way to think about and represent modularised programs is using a hierarchy chart/ structured chart.

Figure: A hierarchy chart: 10th January 2026 Tram Tran 21
  • ▶ Hierarchy chart gives a visual representation of the
  • relationships between functions.
  • ▶ It uses boxes to represent each function in a program.
  • ▶ The boxes are connected in a way that illustrates the
  • functions called by each function.

▶ Hierarchy chart gives a visual representation of the relationships between functions. ▶ It uses boxes to represent each function in a program. ▶ The boxes are connected in a way that illustrates the functions called by each function. ▶ A hierarchy chart does not show in detail what is performed inside a function. 10th January 2026 Tram Tran 22

Recap

In this lecture, we’ve learned: ▶ Procedures and Functions, and how to pass arguments to a procedure/ function ▶ Void functions and Value-returning functions ▶ Documenting code ▶ Local and Global scoping ▶ Methods, Attributes and Classes ▶ Structure charts 10th January 2026 Tram Tran 23

Further Reading

Gaddis, TonyStarting out with Python. Pearson Education Limited. Sixth Edition, 2024. 10th January 2026 Tram Tran 24

Any Question?