Functions and Function Specifications
Chapter 1 · Tamil Nadu State Board
- 7 min read
- 6-question quiz
Every key point below shows the sentence from your textbook it comes from. Book back answers are solved and checked against the book; answers we could not prove are left out. How we check content
In short
This lesson explains functions as reusable code units, including how they use parameters and arguments and how their types can be specified. It distinguishes an interface, which describes what an object can do, from an implementation, which carries out the instructions. It also compares pure functions, which give the same result for the same inputs, with impure functions that can depend on outside variables or cause side effects. Understanding these ideas helps students describe and reason about how algorithms and programs work.
Written by AI from this lesson. How we check it
Key points
- Subroutines are reusable code sections that perform particular tasks, and programming languages call them functions.
From your textbook
“Subroutines are small sections of code that are used to perform a particular task that can be used repeatedly.”
- A function uses inputs and produces an output.
From your textbook
“A function contains a set of code that works on many kinds of inputs, like variables, expressions and produces a concrete output.”
- Parameters are variables in a function definition, while arguments are values passed to it.
From your textbook
“Parameters are the variables in a function definition and arguments are the values which are passed to a function definition.”
- A function that calls itself is recursive, and its definition uses the keyword rec.
From your textbook
“A function definition which call itself is called recursive function.”
- An interface describes what an object can do, while an implementation carries out its instructions.
From your textbook
“Implementation carries out the instructions defined in the interface”
- Pure functions return the same result for the same arguments and do not cause side effects.
From your textbook
“Pure functions are functions which will give exact result when the same arguments are passed.”
- An impure function can depend on outside variables or change data outside its definition.
From your textbook
“One of the most popular side effects is modifying the variable outside of function.”
Formulas in this chapter
- Function definition syntaxlet rec fn a1 a2 ... an := kExplained here
- Update at each meetinga, b, c := a-1, b-1, c+2Explained here
Functions and Function Specifications
A function is a small, reusable unit of code. It works with inputs and produces an output. Functions help avoid repeating a large set of statements many times.
Definition
- Function specification
- A function specification describes what a function requires and what it returns.
A definition binds a value to a name. For example, a := (24) binds the value 24 to the name a. A definition is not an expression, though definitions and expressions can contain each other.
Remember: A function definition tells how a function is named and what computation it performs.
Parameters, Arguments, Types, and Recursion
Parameters and arguments
| Parameter | Argument |
|---|---|
| A variable in a function definition | A value passed to the function |
A function can state a precondition using requires and a postcondition using returns. In pow, b is a parameter, and the value supplied for b is its argument. The function requires b>=0 and returns a to the power of b.
A compiler may infer data types, so type annotations are not always needed. Writing types explicitly can help when debugging a type error. When types are written for parameters, parentheses are mandatory.
let rec fn a1 a2 ... an := k
fn is the function name, a1 to an are parameter names, and k is the computation. Use rec when the function calls itself.
Definition
- Recursive function
- A recursive function is a function definition that calls itself.
Function type patterns
- x → y takes an input of type x and returns an output of type y.
- x1 → x2 → y takes two inputs, of types x1 and x2, and returns an output of type y.
- x1 → ... → xn → y takes n inputs and returns an output of type y.
Remember: Function definitions are static; there are no dynamic function definitions.
Interface and Implementation
Definition
- Interface
- An interface describes the actions or functions an object can perform.
Definition
- Implementation
- Implementation carries out the instructions described by the interface.
Interface and implementation
| Interface | Implementation |
|---|---|
| Defines what an object can do | Carries out the defined instructions |
| Shows the available actions | Handles how the object is processed and executed |
A class template specifies an interface. An object's attributes and behaviour are controlled by sending functions to it. For a car, the accelerator is the interface between the driver and the engine. The driver requests speed, while the engine handles the internal work.
A function can also implement a calculation. For example, min x y z compares its three arguments and returns the smallest one.
Pure Functions and Side Effects
Definition
- Pure function
- A pure function gives the same result for the same arguments and does not change external variables that affect its behaviour.
The function square x returns x * x, so the same input gives the same result. The mathematical function sin (0) always results 0. Evaluating a pure function does not cause side effects to its output.
A compiler can avoid repeating a call to a pure function when the arguments have not changed. For example, strlen (s) finds the length of s. If s is not updated in a loop, a smart compiler can remove redundant calls to strlen.
Remember: For a pure function, the same arguments produce the same result, and evaluation does not cause side effects.
Pure and impure functions
Definition
- Pure function
- A pure function gives the same result whenever it receives the same arguments, and it does not cause side effects.
Definition
- Impure function
- An impure function depends on variables or functions outside its definition, or causes side effects, so the same call may give different results.
The random() function is impure because its output can differ between calls. A function is also impure if it changes a variable outside its definition. Side effects are not always bad; sometimes they are useful.
Pure and impure functions
| Pure function | Impure function |
|---|---|
| Its return value depends only on its arguments. | Its return value may depend on things beyond its arguments. |
| The same arguments always give the same result. | The same arguments may give different results, as with random() or Date(). |
| It has no side effects. | It may cause side effects or modify arguments passed to it. |
Worked example
Consider y := 0 and inc (x: int): int := y := y + x; return (y). Why is inc impure?
Try it first, then show the solutionSolution
- The function changes y, which is outside its definition.
- Its result can change as the value of y changes.
Answer: inc is impure because it modifies the external variable y.
The gcd function as a pure function
The gcd function finds the greatest common divisor of two positive integers. It calls itself again with new arguments until b becomes 0. This repeated call is called recursion.
Worked example
Find the outputs of gcd 13 27 and gcd 20536 7826.
Try it first, then show the solutionSolution
- Use the gcd function with arguments 13 and 27.
- Use the gcd function with arguments 20536 and 7826.
Answer: gcd 13 27 gives 1. gcd 20536 7826 gives 2.
Remember: gcd is pure because its result is determined by the arguments passed to it.
The monochromatize algorithm
The algorithm starts with equal numbers of two types of chameleons, represented by a and b. At each meeting, a and b each decrease by 1, while c increases by 2. The process continues until a and b are 0.
Worked example
Apply the meetings when A, B, C = 4, 4, 6.
Try it first, then show the solutionSolution
- Start with a = 4, b = 4, c = 6.
- After each meeting, reduce a and b by 1 and increase c by 2.
- Continue until a and b reach 0.
Answer: The final values are a = 0, b = 0, c = 14.
a, b, c := a-1, b-1, c+2
a and b each decrease by 1, and c increases by 2.
Functions, parameters, interfaces and implementations
Key programming terms
- A subroutine is a small code section used to perform a particular task, and it can be used repeatedly.
- A function is a unit of code often defined within a larger code structure. It works on inputs and produces an output.
- A definition is a distinct syntactic block.
- Parameters are variables in a function definition. Arguments are the values passed to the function.
- Parentheses are mandatory in a function definition when type annotations are written.
- An interface describes the actions an object can do. It does not carry out those actions.
- An implementation carries out the instructions defined in the interface.
Interface and implementation
| Interface | Implementation |
|---|---|
| Defines what an object can do. | Carries out the instructions defined in the interface. |
| Describes a set of actions. | Performs those actions. |
Questions students ask
Short, direct answers to what students search for in this chapter.
What is a function in computer science?
A function is a reusable unit of code that works on inputs and produces an output. It performs a particular task and helps avoid repeating the same group of statements many times in a program.
What is the difference between parameters and arguments?
Parameters are the variables written in a function definition. Arguments are the values passed to the function for those variables. For example, b is a parameter in pow, while a value supplied for b is an argument.
What is the difference between interface and implementation?
An interface describes what an object can do. Implementation carries out the instructions that make those actions happen. In a car, the accelerator acts as the interface for the driver, while the engine performs the internal work.
What is a pure function?
A pure function gives the same result whenever it receives the same arguments. It does not change external variables that affect its behaviour, and evaluating it does not cause side effects to its output.
What is an impure function?
An impure function depends on variables or functions outside its definition, or causes side effects. Its result may change even when it is called with the same arguments. The random() function is an example because its output can differ between calls.
What is the difference between pure and impure functions?
A pure function depends only on its arguments, gives the same result for the same arguments, and has no side effects. An impure function may depend on outside variables or functions, cause side effects, or give different results for the same arguments.
Show 2 more questions
Why is gcd a pure function?
gcd is pure because its result is determined by the arguments passed to it. It repeatedly calls itself with new arguments until b becomes 0. Calling it with the same arguments gives the same result.
What is the difference between an interface and an implementation?
An interface defines what actions an object can do, but does not perform them. An implementation carries out the instructions defined in the interface. In short, the interface describes the actions, while the implementation performs them.
Book back answers
17 textbook exercises, solved and checked against the book. Questions we could not prove are left out.
Hands on Practice
Write algorithmic function definition to find the minimum among 3 numbers.
Try it, then show the answerAnswer
let min x y z := if x < y then if x < z then x else z else if y < z then y else z
The function compares the three arguments and returns the smallest one.
From the book: “consider the following implementation of a function that finds the minimum of its three arguments:”
Part - I Choose the best answer (1 Mark)
The small sections of code that are used to perform a particular task is called
- (A) Subroutines
- (B) Files
- (C) Pseudo code
- (D) Modules
Try it, then show the answerAnswer
(A) Subroutines
The chapter calls small, reusable sections of code that perform a particular task subroutines.
From the book: “Subroutines are small sections of code that are used to perform a particular task”
Which of the following is a unit of code that is often defined within a greater code structure?
- (A) Subroutines
- (B) Function
- (C) Files
- (D) Modules
Try it, then show the answerAnswer
(B) Function
The chapter defines a function as a unit of code often defined within a greater code structure.
From the book: “A function is a unit of code that is often defined within a greater code structure.”
Which of the following is a distinct syntactic block?
- (A) Subroutines
- (B) Function
- (C) Definition
- (D) Modules
Try it, then show the answerAnswer
(C) Definition
The chapter explicitly says that definitions are distinct syntactic blocks.
From the book: “Definitions are distinct syntactic blocks.”
Practice
What is the difference between a parameter and an argument?
Exam questions
Practice questions with model answers. These are not from an official paper.
What is a function in a programming language?2 marks
A function is a unit of code that works on inputs and produces an output. It can be used to perform a particular task.
Differentiate between a parameter and an argument.2 marks
A parameter is a variable in a function definition. An argument is a value passed to that function.
Differentiate between an interface and an implementation.2 marks
An interface defines what an object can do but does not carry out the action. An implementation carries out the instructions defined by the interface.
Explain how pure and impure functions differ.5 marks
Pure functions return the same result whenever they are called with the same arguments. Impure functions have side effects or depend on external variables that can alter their behaviour.
- Pure function gives consistent results: A pure function always gives the same result for the same arguments.
- Pure function avoids external influence: A function is pure when no external variable alters its behaviour.
- Example of a pure function: The square function is pure because its result does not vary for the same input.
- Compiler advantage: If a pure function is called repeatedly with the same arguments, the compiler may call it only once.
- Reading external memory: A pure function may read external memory, provided it does not change that memory and its result derives from it.
- Impure function differs: An impure function has side effects or depends on external variables that can change its behaviour or result.
Example: The chapter gives square as a pure function: let square x:= return: x * x.
Thus, pure functions are consistent and do not change external memory, unlike impure functions with side effects.
Watch out
- Do not confuse parameters, which are variables in a function definition, with arguments, which are the values passed to it.
- Do not treat interface and implementation as the same: one describes actions, and the other carries them out.
Still stuck on this lesson?
Get an AI answer grounded in your actual textbook — with the exact page reference.
Ask AI about this topic →