English:Functions and Procedures

Functions and Procedures
Introduction
In computer programming, large problems become easier when you split them into smaller jobs. A function or procedure is a named block of instructions that you can call when you need that job to be done. Programmers also use the general words subroutine or subprogram for this idea.
For this course, we use a common school convention: a function sends a value back to the part of the program that called it, while a procedure performs a task without returning a value. Real programming languages do not all use these words in exactly the same way. Some call both kinds functions, and some prefer words such as method, subroutine, or procedure.

The block-programming example above shows how a larger program can contain a reusable named block. You can use the same idea in Scratch-style environments, Blockly, Python, JavaScript, and many other languages.
Learning Goals
By the end of this aiMOOC, you should be able to explain why programmers use functions and procedures, identify definitions and calls, distinguish parameters from arguments, trace a return value, use local variables sensibly, divide a problem into reusable parts, and test a small subroutine.
You should also be able to read and write simple pseudocode for reusable blocks and transfer the same ideas to a visual or text-based programming language.
Why Reusable Blocks Matter
Imagine a game that must play the same sound every time the player collects a coin. You could copy the sound instructions into ten places, but that creates ten places to maintain. A better design is to define one procedure called playCoinSound and call it whenever the event happens.
This is an example of modular programming. A program is divided into smaller modules with clear jobs. Good modules help with decomposition, reuse, readability, testing, and maintenance.

A useful subroutine should usually have one clear purpose. Names such as calculateScore, showMenu, or drawSquare tell the reader what the code is meant to do. A vague name such as thing1 gives much less information.
Functions and Procedures
Both functions and procedures are callable units: you define them once and can run them by calling their name. They may receive input through parameters. Their main difference in the school convention used here is what happens after the block finishes.
A procedure carries out an action. For example, it might display a message, move a sprite, draw a shape, save a result, or change a score.
A function calculates or chooses a value and returns that value to the caller. For example, double(7) could return 14, and isEven(12) could return a true value.
A function call can often be used inside a larger expression because it produces a value. A procedure call is usually written as its own instruction because its purpose is the action it performs.
Parameters and Arguments
A parameter is a named input in a subroutine definition. It acts like a placeholder. An argument is the actual value supplied when the subroutine is called.
For example, in FUNCTION double(number), number is a parameter. In the call double(6), the value 6 is the argument. A different call such as double(15) uses the same function but supplies a different argument.
Parameters make code more general. A procedure called greet(name) can greet many people without needing a separate procedure for each name.
Calling and Returning
A definition tells the computer what the subroutine does. A call tells the computer to run it. When a call happens, control moves into the subroutine. When the subroutine finishes, control returns to the instruction after the call.
A function also sends back a return value. The caller can store that value in a variable, display it, compare it, or use it in another expression.
Consider this trace: result ← double(6). The argument 6 is placed into the parameter number. The function calculates number × 2. It returns 12. The variable result therefore receives 12.
Variables and Scope
A variable created inside a subroutine is often a local variable. Its scope is normally limited to that subroutine or block. Local variables help prevent one part of a program from accidentally changing data used by another part.
A global variable can be available to a wider part of a program. Globals can be useful, but heavy use of them can make a program harder to understand and test. For beginner programs, passing needed information through parameters and returning results often makes the flow of data clearer.
Decomposition and Program Design
Before writing code, ask what separate jobs the program must perform. A quiz program might need blocks for showing a question, checking an answer, updating the score, and showing the final result. Each job can become a candidate function or procedure.
A flowchart can help you see the order of actions and decisions before you choose exact code.

Good decomposition is not simply making as many tiny blocks as possible. Each block should have a clear responsibility, a useful name, and an interface that is easy to understand.
Pseudocode Examples
Pseudocode lets you focus on logic without worrying about the exact rules of one programming language. The following examples use a simple school-style notation.
Procedure example
PROCEDURE greet(name)
DISPLAY "Hello, " + name
ENDPROCEDURE
greet("Maya")
The procedure performs an action: it displays a greeting. It receives the name through a parameter.
Function example
FUNCTION double(number)
RETURN number * 2
ENDFUNCTION
result ← double(6)
DISPLAY result
The function calculates a value and returns it. The returned value is stored in result.

From Visual Blocks to Text Code
Visual programming systems often show parameters as spaces in a custom block. Text-based languages place parameters in a function or procedure header. The surface syntax changes, but the idea is the same: name a reusable operation, define its inputs, and call it when needed.

When moving from blocks to text, concentrate first on the structure: definition, parameters, body, call, and possible return value. Then learn the punctuation and keywords required by the language you are using.
Testing and Debugging
A subroutine is easier to test when it has one clear job. For a function, try several inputs and compare the actual result with the result you predicted. For a procedure, check the effect it should produce.
Useful tests include a normal case, a small or boundary case, and a case that might reveal a mistake. For double(number), you could test 4, 0, and a negative number if your class has worked with negative values.
When a test fails, trace the call carefully. Check the argument, the parameter value, each calculation, and the returned result. This is a practical form of debugging.
Collaboration and Reuse
Functions and procedures also support teamwork. Different programmers can work on different parts of a program if they agree on the names, inputs, outputs, and purpose of each block.
A well-designed reusable block hides unnecessary detail. Another programmer can call calculateArea(width, height) without needing to know every step inside it. This is a simple form of abstraction.
Interactive Tasks
Quiz: Test Your Knowledge
What is a subroutine? (A named reusable block of instructions) (!A file that stores only pictures) (!A number that never changes) (!A type of computer screen)
What does a function do in the school convention used in this course? (It returns a value) (!It can never take input) (!It must always draw a picture) (!It runs only once)
What is the main role of a procedure in this course? (It performs a task without returning a value) (!It stores every variable in a program) (!It changes a computer into a server) (!It replaces all loops)
What is a parameter? (A named input in a definition) (!A mistake found during testing) (!A picture used in a flowchart) (!A value printed by every program)
What is an argument? (A value supplied in a call) (!The name of the whole program) (!A local variable that is never used) (!A diagram symbol for a decision)
Why is code reuse useful? (It reduces repeated code) (!It makes every program longer) (!It removes the need for testing) (!It prevents the use of variables)
Where is a local variable normally available? (Inside the subroutine where it is created) (!In every program on the computer) (!Only inside a web browser) (!Only before the program starts)
What does calling a subroutine mean? (Telling the subroutine to run) (!Deleting the subroutine) (!Renaming every variable) (!Turning code into an image)
Which is the clearest name for a block that finds the area of a rectangle? (calculateArea) (!thing1) (!code) (!x)
Which testing approach is most useful for a small function? (Try normal boundary and unusual inputs) (!Test it only when the whole project is finished) (!Use one input and assume all others work) (!Avoid predicting the expected result)
Memory Game
| Function | Reusable block that sends a value back to the caller |
| Procedure | Reusable block mainly used to perform an action |
| Parameter | Named input placeholder in a definition |
| Argument | Actual value supplied during a call |
| Return | Sending a result back to the caller |
| Scope | Region of a program where a name can be used |
| Decomposition | Breaking a large problem into smaller jobs |
| Debugging | Finding and correcting problems in code |
Drag and Drop
| Match the correct terms. | Topic |
|---|---|
| Procedure | Performs an action without returning a value in this course |
| Function | Calculates and returns a value in this course |
| Parameter | Named input written in a definition |
| Argument | Actual input value used in a call |
| Local variable | Data name normally limited to one subroutine |
...
Crossword Puzzle
| Function | Which reusable block returns a value in this course? |
| Procedure | Which reusable block mainly performs an action? |
| Parameter | What is a named input in a definition called? |
| Argument | What is an actual value supplied in a call called? |
| Return | Which keyword idea sends a result back to the caller? |
| Scope | What describes where a variable name can be used? |
LearningApps
Cloze Text
Open-Ended Tasks
Easy
- Function Storyboard: Draw a six-panel storyboard that shows a game character calling the same reusable action several times, and label where the call happens.
- Procedure Card: Write a procedure in pseudocode that displays a three-line welcome message, then mark its name, body, and call.
- Parameter Swap: Create three calls to one greeting procedure using three different arguments, then explain what changes and what stays the same.
- Code Explanation Video: Record a one-minute video in clear English explaining the difference between defining a subroutine and calling it.
Standard
- Reusable Shape Project: In Scratch, Blockly, Python turtle, or another school tool, create a procedure that draws one shape and call it at least four times in different places.
- Function Test Table: Design a function that calculates a simple value, predict at least five outputs, run your tests, and compare predicted and actual results.
- Programmer Interview: Interview a teacher, student programmer, or software professional about how they use functions or procedures, then summarize three practical reasons they gave.
- Flowchart to Pseudocode: Draw a flowchart for a small program with at least two reusable blocks and convert the design into pseudocode.
Advanced
- Mini Quiz Program: Build a short quiz that separates question display, answer checking, score updating, and final reporting into sensible functions or procedures.
- Refactoring Challenge: Take a small program with repeated instructions, redesign it to remove repetition with reusable blocks, and explain why your version is easier to maintain.
- Scope Experiment: Create a safe classroom experiment with local and wider-scope variables, record which values can be accessed in each place, and explain the results.
- Coding Club Field Study: Visit a school computing lab, coding club, makerspace, or virtual coding community, observe how a project is divided into parts, and produce a short report connecting the design to modular programming.
Learning Assessment
- Trace a Function Call: Given a short function with two parameters, trace the values step by step and justify the final returned result.
- Choose the Better Design: Compare a repeated-code solution with a modular solution and explain which is easier to test, change, and read.
- Design an Interface: Propose a clear name, parameters, and expected result for a subroutine that solves a real classroom problem, and justify each choice.
- Debug a Call: Find and correct errors in a short example where arguments are passed in the wrong order, then explain how the mistake changes the result.
- Transfer to a New Language: Translate a simple pseudocode function into a visual or text-based language used in class and identify the equivalent definition, call, parameter, and return features.
- Evaluate Scope Choices: Compare a design that uses many global variables with one that passes data through parameters, and explain which design is easier to reason about.
Evidence of Learning
Knowledge: You can explain callable blocks, functions, procedures, parameters, arguments, return values, local scope, decomposition, reuse, and debugging in your own words.
Skills: You can read a definition, trace a call, predict data flow, write simple pseudocode, choose clear names, divide a problem into modules, and test reusable code.
Products: Strong evidence may include a working mini-program, a flowchart, a set of test cases, a code explanation, a refactored program, or a short interview report.
Transfer: You can recognize the same reusable-block idea in different programming environments and decide when a real problem should be separated into smaller callable parts.
OERs on the Topic
Linked Learning Areas
aiMOOC Projects
NEWSLernweltNOAH fragen