When writing computer programs, especially large or complex ones, it is important to organize the code in a way that is easy to understand, maintain, and reuse. This approach is called modular programming. Modular programming breaks a program into smaller, manageable parts called modules. Two fundamental building blocks of modular programming are functions and procedures.
Both functions and procedures are named blocks of code designed to perform specific tasks. They help programmers divide a big problem into smaller pieces, making the program easier to write, test, and debug. By using functions and procedures, you avoid repeating the same code multiple times, which saves time and reduces errors.
In this chapter, we will explore what functions and procedures are, how they differ, and why they are essential in programming. We will also learn how to create and use them effectively with examples, flowcharts, and pseudocode.
Before diving deeper, it's important to understand the difference between a function and a procedure. Although both are blocks of code designed to perform tasks, their key difference lies in whether they return a value.
| Aspect | Function | Procedure |
|---|---|---|
| Return Value | Returns a value after execution | Does not return a value |
| Purpose | Computes and gives back a result | Performs a task or action |
| Typical Use Case | Calculations, data processing, value retrieval | Displaying output, modifying data, performing operations |
| Example | Function to calculate factorial of a number | Procedure to print student details |
| Syntax (Generic) | function functionName(parameters) returns dataType // code return valueend function | procedure procedureName(parameters) // codeend procedure |
Why this difference matters: When you want your code block to produce a result that can be used elsewhere in the program, use a function. When you want to perform an action without needing a result, use a procedure.
Understanding the parts that make up functions and procedures helps in writing and using them correctly.
graph TD Call[Caller Program] -->|Pass Arguments| Func[Function/Procedure] Func -->|Execute Code| Process[Process Inputs] Process -->|Return Value (if Function)| Call Process -->|End Execution| Call
This flowchart shows the general flow when a function or procedure is called:
When passing arguments to functions or procedures, there are two common methods:
| Aspect | Call by Value | Call by Reference |
|---|---|---|
| How Data is Passed | A copy of the actual value is passed | A reference (address) of the actual variable is passed |
| Effect on Original Variable | Original variable remains unchanged | Original variable can be modified |
| Use Case | When you do not want the original data to change | When you want the function/procedure to modify the original data |
| Example | Calculating a value without changing inputs | Swapping two numbers by modifying their values |
Using functions and procedures offers many benefits:
Functions and procedures are essential tools in programming that help organize code into logical blocks, making programs easier to write, read, test, and maintain.
Step 1: Understand factorial: \( n! = n \times (n-1) \times (n-2) \times \ldots \times 1 \). For example, \( 5! = 5 \times 4 \times 3 \times 2 \times 1 = 120 \).
Step 2: Define a function named factorial that takes an integer parameter n and returns an integer.
Step 3: Use a loop or recursion inside the function to calculate factorial.
Step 4: Call the function with argument 5 and print the result.
Answer: The factorial of 5 is 120.
Step 1: Define a procedure named printStudentDetails with parameters: name (string), rollNo (integer), and marks (integer).
Step 2: Inside the procedure, print the values in a readable format.
Step 3: Call the procedure with sample data, for example, name = "Amit", rollNo = 23, marks = 85.
Answer: The procedure will display:
Name: Amit
Roll Number: 23
Marks: 85
Step 1: Understand that call by reference passes the address of variables, so changes inside the procedure affect the original variables.
Step 2: Define a procedure swap that takes two parameters by reference.
Step 3: Inside the procedure, use a temporary variable to swap the values.
Step 4: Call the procedure with two variables, for example, a = 10 and b = 20.
Answer: After the procedure call, a = 20 and b = 10.
Step 1: Recall Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, ... where each number is the sum of the two preceding ones.
Step 2: Define a function fibonacci(n) that returns 0 if \( n = 0 \), returns 1 if \( n = 1 \), else returns fibonacci(n-1) + fibonacci(n-2).
Step 3: Call the function with \( n = 6 \).
Answer: The 6th Fibonacci number is 8.
Step 1: Use the formula for simple interest: \( SI = \frac{P \times R \times T}{100} \).
Step 2: Define a function calculateSimpleInterest(P, R, T) that returns the calculated interest.
Step 3: Call the function with P = 10000 INR, R = 5%, T = 3 years.
Answer: Simple interest = \( \frac{10000 \times 5 \times 3}{100} = 1500 \) INR.
When to use: To avoid confusion and errors when writing modular code.
When to use: For tasks like swapping values or updating variables.
When to use: When solving problems using recursive functions.
When to use: Especially useful in exams and complex problems.
When to use: In larger programs or exam questions with repetitive tasks.
Progress tracking is paywalled — subscribe to mark subtopics as understood and save your streak.
Go to practice →