Procedures, functions and modular programs
விளையாடிக் கற்றுக்கொள்ளுங்கள்
ஆற்றல் சம்பாதிக்க இந்த கேள்விகளுக்குப் பதிலளியுங்கள், பின்னர் மீன் பிடித்து ஆராயுங்கள். கணக்கு தேவையில்லை.
பாட குறிப்புகள்
What are procedures and functions?
- A procedure is a named block of code that performs a specific task but does not return a value.
- A function is a named block of code that performs a task and returns a value to the caller.
- Both are examples of callable units – pieces of code that can be invoked (called) multiple times.
- They allow you to decompose a large, complicated problem into smaller, manageable chunks.
- Each chunk can be given a meaningful name, making the code easier to read and understand.
A subroutine is a named, reusable block: the main program calls it, it does its task, and it returns.

Why use modular programs?
- Modularity makes programs easier to build, test, and debug because each part can be developed and checked separately.
- Code can be reused – write a procedure once and call it many times, avoiding duplication.
- It reduces the cognitive load – you don't have to hold the whole program in your head at once.
- It improves quality and reliability – well-tested modules are less likely to contain errors.
- It makes maintenance easier – you can change one module without affecting others.
A solution split into focused modules, each with one responsibility, coordinated by a main program.

Defining and calling procedures
- To define a procedure, you give it a name and a block of code that runs when it is called.
- To call a procedure, you write its name followed by parentheses (e.g., `drawSquare()`).
- A procedure can be called multiple times from different parts of the program.
- Procedures can take parameters – values passed into the procedure to make it more flexible.
- Example: `drawSquare(size)` uses the `size` parameter to draw a square of that size.
The main solution calls a subroutine with inputs, receives a result, and continues its own task.

Parameters and arguments
- Parameters are the variables listed in the procedure/function definition.
- Arguments are the actual values passed when you call the procedure/function.
- Parameters allow the same code to work with different data each time it is called.
- Example: `calculateArea(length, width)` – `length` and `width` are parameters; `calculateArea(5, 3)` passes arguments 5 and 3.
- Some languages allow default parameters – if you don't pass an argument, a default value is used.
Return values
- A function returns a value using a `return` statement.
- The returned value can be assigned to a variable or used in an expression.
- Example: `result = add(2, 3)` – the function `add` returns 5, which is stored in `result`.
- Procedures do not return a value; they just perform an action.
- In some languages, a procedure without a `return` still returns `None` or `null` implicitly.
Scope of variables
- Variables defined inside a procedure/function are local – they can only be used within that block.
- Variables defined outside all procedures are global – they can be accessed anywhere in the program.
- Local variables are created when the procedure is called and destroyed when it ends.
- Using local variables helps avoid naming conflicts and makes code more self-contained.
- Global variables can be changed from anywhere, which can lead to bugs – use them sparingly.
Procedures vs functions – key differences
- Procedures perform a task but do not return a value; functions return a value.
- In some languages (e.g., Python), procedures are just functions that don't have a `return` statement.
- In others (e.g., C), all callable units are called functions, even if they return nothing (void).
- Some languages use the term method for functions defined inside classes (object-oriented programming).
- The choice of terminology depends on the programming language you are using.
Examples in Python
- Procedure example: `def greet(name): print('Hello', name)` – it prints but returns nothing.
- Function example: `def add(a, b): return a + b` – it returns the sum.
- Calling a procedure: `greet('Alice')` – prints 'Hello Alice'.
- Calling a function: `result = add(3, 4)` – result becomes 7.
- You can use functions inside expressions: `print(add(1, 2)×3)` prints 9.
Testing and debugging modular programs
- Because each module is small, you can test it independently before integrating it into the whole program.
- Debugging is easier – you know which module contains the error.
- You can write test cases for each function to check it works correctly.
- Modularity supports teamwork – different people can work on different modules simultaneously.
- It also allows you to reuse tested modules in other programs.
History (brief)
- The concept of subroutines was developed in the 1940s, with early contributions by Alan Turing, John Mauchly, and Kay McNulty.
- Maurice Wilkes, David Wheeler, and Stanley Gill are credited with the formal invention of the closed subroutine.
- Early computers often used a call sequence – a series of instructions – to call subroutines.
- The return address (where to continue after the subroutine) was stored in various ways, often on a stack.
- Modern languages provide built-in support for procedures and functions, making them easy to use.
ஸ்லைடுகள்
Sign up free to view the lesson slides
Step through every slide for this topic — plus flashcards and revision notes — with a free account.
பயிற்சி கேள்விகள்
இலவச முன்னோட்டம் — 57-இல் 8 கேள்விகள். அனைத்தையும் பார்க்க பதிவு செய்யவும்.
1.What is a procedure in programming?
Easy- AA named block of code that performs a specific task
- BA type of variable that stores multiple values
- CA loop that repeats a block of code
- DA data structure for storing numbers
2.Which of the following is the main difference between a function and a procedure in languages like COBOL and BASIC?
Easy- AFunctions return a value, procedures do not
- BProcedures return a value, functions do not
- CFunctions can be called multiple times, procedures only once
- DProcedures are used for calculations, functions for output
3.A procedure can be called multiple times from different parts of a program.
EasyTrue or false?
4.Which of the following are benefits of using procedures and functions in programming? (Select all that apply)
Easy- AThey make programs easier to debug
- BThey allow code to be reused
- CThey make programs run faster
- DThey make programs easier to understand
5.In the context of modular programming, what is a parameter?
Medium- AA value that is passed into a procedure or function when it is called
- BA value that is returned from a procedure or function
- CA variable that is used only inside a procedure
- DA type of loop used to repeat a block of code
6.In some programming languages, the term 'method' is used to refer to a function that is defined inside a class.
MediumTrue or false?
7.Match the programming term to its correct description.
Medium- Function
- Procedure
- Method
- A callable unit that returns a value
- A callable unit that does not return a value
- A function defined inside a class
8.Put the steps of using a procedure in a program in the correct order.
Medium- Define the procedure
- Call the procedure
- Execute the code inside the procedure
- Return to the point after the call
Unlock all 57 questions, flashcards & more
இந்த தலைப்பிற்கான ஒவ்வொரு கேள்வியையும், ஸ்லைடுகளையும், ஃப்ளாஷ் கார்டுகளையும் மற்றும் திருப்புதல் குறிப்புகளையும் பார்க்க இலவச கணக்கை உருவாக்கவும்.