Skip to content

Input/Output and Procedures & Functions

標準~29 min5 terms11 past questions

Goals of this unit

  • Trace how values pass between a program and the outside world, and between its parts, by following the flow of input/output
  • Tell a procedure from a function by whether it hands its result back to the caller
  • Read the types of the arguments and the return value from a pseudo-language declaration line, and work out the value a call returns
Terms at a glance (5)
TermIn one lineCategoryHow to tell it apart
入出力Input takes data in from outside; output passes results outBasic ideaWhether data comes in from outside or goes out
手続A named block of processing steps that returns to the caller without giving back a valueBuilding block (subprogram)No type before the name in the declaration line
関数A block of processing that hands its result value back to the callerBuilding block (subprogram)The type of the returned value comes before the name in the declaration line
引数A value passed to a block when it is calledValue passedListed inside the parentheses after the name
戻り値The value a function gives back to the caller as its resultValue passedWritten after return

Terms

入出力Input/Output (I/O)

Open in glossary 入出力

Input/output (入出力) is the combined name for input (入力), where a program takes in data from outside, and output (出力), where it passes the processed result back out.

  1. Input: read in 2 numbers
  2. Process: calculate the average
  3. Output: show the average on screen

The calculated average is sent to the screen. This is the output.

3 / 3

Reading from the keyboard or a file counts as input, while showing something on screen or writing to a file counts as output. If you picture a program running in the order input → process → output, it becomes easy to follow which data comes in and goes out at each point. Even when the processing is split into several parts, you can ask the same question of each part: what goes in and what comes out.

AnalogyIt works like a restaurant: the order slip comes in, the kitchen cooks, and the finished dish goes out to the table.

How to rememberThe short form is I/O: the I of Input and the O of Output joined by a slash, packing both directions, in and out, into one word.

Exam points

  • ExamWhen processing blocks are chained in series, the output (出力) of one block becomes the input (入力) of the next as it is. Work out the values in order, starting from the first block.

1 past questions on this term 入出力

A procedure (手続) is a named set of processing steps; calling it runs the steps inside, and control then returns to the caller without bringing back a result value.

  1. The caller writes the procedure's name
  2. The steps inside the procedure run in order
  3. Control returns to the caller's next step

At the end of the procedure, control goes back to the step after the call, carrying no value with it.

3 / 3

The Japanese word 手続 can also mean a sequence of processing steps in general, not only a building block. In the pseudo-language, a procedure is declared with the type and name of each value it receives inside the parentheses after its name, and no type before the name. When the same processing is needed many times, writing the name is enough to call it.

AnalogyIt is like asking someone to lock up: they check each window and lock in turn and come back, but hand you nothing.

How to rememberThe English word procedure comes from a word meaning to go forward, so think of it as something that moves the processing forward one step at a time.

Exam points

  • ExamAn algorithm (アルゴリズム) is a set of steps that fixes which processing to do, and in what order, to get the result you want.
  • ExamIn a procedure (手続) that swaps the values of 2 variables, the value that would be lost when overwritten is first saved to a work variable, and at the end that saved value is put into the other variable.
  • ExamTo work back from a converted result to the original value, replace each reversible operation such as adding or subtracting with its opposite and trace the steps from the last one backward. Taking a remainder cannot be reversed to a single value, so pick the one that falls within the range the original value could take.

2 past questions on this term 手続

A function (関数) is a named block of processing that hands its result back to the caller as a value.

  1. The caller passes values and calls the function
  2. Inside the function, the received values are processed
  3. Control returns to the caller with the result value
  4. The caller uses the returned value in an assignment or calculation

The caller stores the returned value in a variable or uses it in the next calculation.

4 / 4

A function receives values when it is called, uses them in its processing, and returns the result to the caller. In the pseudo-language it is declared in the form ○整数型: functionName(整数型: name), where 整数型 is the exam notation for integer type, with the return type before the name and the type and name of each received value inside the parentheses after it. Inside the function, return gives back the value written after it and ends the processing.

AnalogyIt is like the square root key on a calculator: enter a number, press the key, and the result of a fixed calculation comes back.

How to rememberIn Japanese it was once written 函数, and 函 means box. Picture a box that gives back a result when you put a value in.

Exam points

  • ExamIf return runs partway through a loop, the function (関数) ends at that moment and the remaining iterations do not run.
  • Mix-upA procedure (手続) returns no value when called, but a function (関数) does, so a function call can be written on the right-hand side of an assignment or inside an expression.

2 past questions on this term 関数

Procedure vs. Function

AspectProcedureFunction
Key differenceResult valueNot returnedReturned to the caller with return
Declaration line○procedure name(values received)○return type: function name(values received)
How it is calledCalled as a statement of its ownCan be written on the right of an assignment or inside an expression

An argument (引数) is a value you pass to a procedure or function when you call it, for it to use in its processing.

PositionName in the declaration's parenthesesValue passed in the call
1stx8
2ndy4

Seen from the block's side, arguments are its input. The declaration lists the names to receive inside its parentheses, and the caller writes values in that same order to pass them in. Changing the values passed in gets a different result from the same block.

AnalogyIt is like setting the original and the number of copies on a copier and getting exactly that output. The original and the copy count are the arguments.

Exam points

  • ExamA block can take more than one argument (引数) and can combine their values to choose between different processing paths.
  • ExamAn argument (引数) can be an array as well as a single number. Inside the block, each element of the received array is referred to by its element number.

2 past questions on this term 引数

戻り値Return value

Open in glossary 戻り値

A return value (戻り値) is the value a function gives back to the caller as its result, and it is the block's output.

Type written before the name in the declarationExample of a returned value
Integer type42
Boolean typetrue or false
String type"ABC"

The type of the return value matches the type written before the name in the function's declaration line. The caller can assign the returned value to a variable or use it for display or the next calculation.

AnalogyIt is like clothes left at a dry cleaner coming back finished: the item you get back is the return value.

How to rememberIn English it is a return value, the same word as return, the pseudo-language instruction that gives a value back, so look after return to see what comes back.

Exam points

  • ExamIn a function (関数) that returns only after its loop ends, the return value (戻り値) is what you get by following the changes through every pass of the loop to the end.
  • ExamA return value (戻り値) is not limited to numbers. Some functions (関数) return a Boolean (論理型) value meaning true or false, or a string (文字列型) value made of a sequence of characters.
  • Mix-upAn argument (引数) is a value that goes from the caller into the block, and a return value (戻り値) is a value that comes back from the block to the caller.

2 past questions on this term 戻り値

Argument vs. Return value

AspectArgumentReturn value
Key differenceDirection of the valueFrom the caller into the blockFrom the block back to the caller
Where it is writtenInside the parentheses after the name, in both the declaration and the callAfter return inside the function
Role in input/outputInput to the blockOutput from the block

Key points

  • A program processes input from outside and outputs the result, and when blocks are connected, the output of one block is the input of the next.
  • Once processing is named as a block, writing its name is enough to use the same processing again and again.
  • In a pseudo-language declaration line, the type before the name is the return type, and the parentheses after the name hold the type and name of each value received.
  • Values written in a call go into the arguments in declaration order, and the value a function returns is whatever is written after return.

Check questions

5 past questions from this unit

Start the check questions

All past questions in this topic