TN Online TestSamacheer Kalvi practice

Function - Study Notes

Share this chapter: Telegram

Chapter Summary

Subroutines, also known as functions, are the fundamental building blocks of computer programs. They are small, modular sections of code designed to perform specific tasks repeatedly, which improves code reusability and program structure. A function defines a mapping from inputs to a concrete output, binding values to names. In function specification, parameters act as placeholders in the function definition, while arguments are the actual values passed during execution. Functions can be pure (deterministic and free of side effects) or impure (dependent on or modifying external state).

Learning Objectives

Key Concepts and Definitions

Worked Methods

Recursive Exponentiation

Calculating the power of a number is defined recursively by reducing the exponent. The method is defined as:

let rec pow (a: int) (b: int) : int := if b = 0 then 1 else a * pow a (b - 1)

For any base 'a' and exponent 'b' (where 'b' is greater than or equal to 0), the function multiplies 'a' by the result of the function called with the exponent decreased by 1, until the base case of exponent 0 returns 1.

Greatest Common Divisor (GCD)

The greatest common divisor of two integers can be recursively computed using the Euclidean algorithm:

let rec gcd a b := if b != 0 then gcd b (a mod b) else return a

This function recursively calls itself, passing 'b' as the new 'a' and 'a mod b' as the new 'b', until 'b' is 0, at which point 'a' is returned as the GCD.

Common Exam Traps

Exam Tips

Solved MCQs → Practice test →

More for this chapter

Book Back Questions10 textbook MCQs · solved Additional MCQs15 extra MCQs · solved Practice TestInteractive · instant score Book Back TestTest yourself on the textbook set Additional MCQ TestTest yourself on the extra set Formula SheetAll key formulas

More chapters in Computer Science

View all
2 Data Abstraction 3 Scoping 4 Algorithmic Strategies 5 Python -Variables and Operators 6 Control Structures 7 Python functions 8 Strings and String manipulation 9 Lists, Tuples, Sets and Dictionary 10 Python Classes and objects 11 Database Concepts 12 Structured Query Language (SQL) 13 Python and CSV files 14 Importing C++ programs in Python. 15 Data manipulation through SQL 16 Data visualization using pyplot: line chart, pie chart and bar chart