Functions / Methods in Programming Languages
Functions / Methods in Programming Languages
Learn how functions and methods help programmers organize code, reuse logic, reduce repetition, and build modular programs.
What are Functions / Methods?
A function is a reusable block of code that performs a specific task.
A method is also a reusable block of code, but it is usually connected to an object or class in object-oriented programming.
In simple words, instead of writing the same code again and again, we can put that code inside a function or method and reuse it whenever needed.
Easy Real-Life Example
Function as a Juice Maker
Imagine a juice maker machine. You give it fruits as input, it processes them, and it gives you juice as output.
A function works in a similar way:
Input → Function / Method → Output
Fruits → Juice Maker → Juice
In programming, a function may take input values, perform some processing, and return a result.
Why are Functions / Methods Needed?
Functions and methods are needed because programs can become long and difficult to manage. If we write all logic in one place, the program becomes hard to read, test, debug, and maintain.
Importance of Functions / Methods
- They help divide a large program into smaller parts.
- They reduce code repetition.
- They make code easier to read.
- They make code easier to test and debug.
- They allow the same logic to be reused many times.
- They help organize programs into meaningful sections.
- They improve maintainability.
- They support modular programming.
- They help teams work on different parts of a program independently.
- They make complex programs easier to understand.
General Syntax of a Function
Different programming languages write functions differently, but the general idea is similar.
FUNCTION functionName(parameters)
statements
RETURN value
END FUNCTION
The function has a name, may accept parameters, performs some statements, and may return a result.
General Syntax of a Method
A method is usually written inside a class or object structure.
CLASS ClassName
METHOD methodName(parameters)
statements
RETURN value
END METHOD
END CLASS
A method belongs to a class or object and may work with the data stored inside that object.
Main Parts of a Function / Method
| Part | Meaning | Example |
|---|---|---|
| Function / Method Name | The name used to call the block of code. | calculateArea |
| Parameters | Input variables received by the function or method. | length, width |
| Function Body | The statements that perform the task. | area = length * width |
| Return Value | The output sent back after processing. | RETURN area |
| Function Call | The statement that runs the function or method. | calculateArea(10, 5) |
How a Function Works
A function does not run automatically just because it is defined. It runs only when it is called.
Working Steps
- The function is defined with a name.
- The program calls the function when needed.
- Input values may be passed as arguments.
- The function receives those values as parameters.
- The statements inside the function run.
- The function may return a result.
- The program continues from where the function was called.
Function Execution Flow
START
↓
Define function
↓
Call function
↓
Pass arguments
↓
Function receives parameters
↓
Function executes statements
↓
Function returns result
↓
Continue program
Example 1: Simple Function Without Parameters
This function displays a welcome message.
/*
This function displays a welcome message.
*/
FUNCTION showWelcomeMessage()
DISPLAY "Welcome to Programming Mastery Course"
END FUNCTION
ENTRY POINT
CALL showWelcomeMessage()
END ENTRY POINT
Expected Output
Welcome to Programming Mastery Course
The function does not need any input and does not return any value. It simply performs an action.
Example 2: Function With Parameters
Parameters allow a function to receive input values.
/*
This function greets a user by name.
*/
FUNCTION greetUser(userName)
DISPLAY "Hello, " + userName
END FUNCTION
ENTRY POINT
CALL greetUser("Riya")
CALL greetUser("Aman")
END ENTRY POINT
Expected Output
Hello, Riya
Hello, Aman
The same function works with different input values. This makes the function reusable.
Example 3: Function With Return Value
A function can return a value after completing its task.
/*
This function adds two numbers and returns the result.
*/
FUNCTION addNumbers(firstNumber, secondNumber)
DECLARE sum AS INTEGER = 0
SET sum = firstNumber + secondNumber
RETURN sum
END FUNCTION
ENTRY POINT
DECLARE result AS INTEGER = 0
SET result = addNumbers(10, 20)
DISPLAY "Sum: " + result
END ENTRY POINT
Expected Output
Sum: 30
The function returns the calculated value, and the calling part of the program stores it in result.
Example 4: Function to Calculate Area
/*
This function calculates area of a rectangle.
*/
FUNCTION calculateRectangleArea(length, width)
DECLARE area AS DECIMAL = 0.0
SET area = length * width
RETURN area
END FUNCTION
ENTRY POINT
DECLARE roomArea AS DECIMAL = 0.0
SET roomArea = calculateRectangleArea(10, 5)
DISPLAY "Area: " + roomArea
END ENTRY POINT
Expected Output
Area: 50
Example 5: Function for Pass or Fail
/*
This function returns pass or fail based on marks.
*/
FUNCTION getResult(marks)
IF marks >= 35 THEN
RETURN "Pass"
ELSE
RETURN "Fail"
END IF
END FUNCTION
ENTRY POINT
DECLARE studentResult AS TEXT = ""
SET studentResult = getResult(72)
DISPLAY "Result: " + studentResult
END ENTRY POINT
Expected Output
Result: Pass
Parameters vs Arguments
Beginners often confuse parameters and arguments. They are related but not exactly the same.
| Concept | Meaning | Example |
|---|---|---|
| Parameter | A variable written in the function definition. | name in FUNCTION greet(name) |
| Argument | The actual value passed during the function call. | "Riya" in greet("Riya") |
FUNCTION greet(name)
DISPLAY "Hello, " + name
END FUNCTION
CALL greet("Riya")
Here, name is the parameter and "Riya" is the argument.
Function vs Method
Functions and methods are very similar because both contain reusable code. The main difference is where they belong.
| Feature | Function | Method |
|---|---|---|
| Meaning | A reusable block of code. | A reusable block of code connected to a class or object. |
| Association | Usually independent. | Usually belongs to an object or class. |
| Calling Style | Called directly by name. | Called using an object or class. |
| Common Context | Procedural or general programming. | Object-oriented programming. |
| Example Idea | calculateArea() |
student.calculateGrade() |
Types of Functions
Functions can be classified in different ways depending on how they are created and used.
| Type | Meaning | Example Idea |
|---|---|---|
| Built-in Function | Already provided by the programming language. | print, length, maximum |
| User-defined Function | Created by the programmer. | calculateTotal |
| Function Without Parameters | Does not receive input values. | showMenu() |
| Function With Parameters | Receives input values. | addNumbers(a, b) |
| Function With Return Value | Sends a result back. | calculateArea() |
| Function Without Return Value | Performs an action but does not return data. | displayMessage() |
Reusability with Functions
One of the biggest benefits of functions is reusability. A function can be written once and called many times.
FUNCTION calculateSquare(number)
RETURN number * number
END FUNCTION
ENTRY POINT
DISPLAY calculateSquare(2)
DISPLAY calculateSquare(5)
DISPLAY calculateSquare(10)
END ENTRY POINT
Expected Output
4
25
100
The same function is reused with different values.
Modular Programming
Modular programming means dividing a program into smaller, manageable parts. Functions and methods are important tools for modular programming.
FUNCTION inputMarks()
// input logic
END FUNCTION
FUNCTION calculatePercentage()
// calculation logic
END FUNCTION
FUNCTION displayResult()
// output logic
END FUNCTION
Instead of writing everything in one large block, we divide the program into smaller meaningful sections.
When to Create a Function / Method
You should consider creating a function or method when:
Good Situations
- The same logic is repeated multiple times.
- A task has a clear purpose.
- A program section is becoming too long.
- You want to make code easier to test.
- You want to separate input, processing, and output.
- You want to improve readability.
- You want to make code reusable.
- You want to reduce duplication.
When Not to Create Too Many Functions
Functions are useful, but unnecessary functions can make code confusing.
Avoid Creating Functions When
- The logic is extremely small and used only once.
- The function name does not clearly explain its purpose.
- The function depends on too many unrelated variables.
- The function does multiple unrelated tasks.
- The program becomes harder to follow because of too many tiny functions.
Function / Method Naming Best Practices
Function and method names should clearly describe what they do.
| Poor Name | Better Name | Reason |
|---|---|---|
calc() |
calculateTotal() |
Clearly explains what is calculated. |
doIt() |
printInvoice() |
Explains the actual task. |
x() |
validateAge() |
Meaningful and readable. |
process() |
processPayment() |
More specific. |
calculate, display, validate, print, get, or check.
Common Beginner Mistakes
Mistakes
- Defining a function but never calling it.
- Calling a function before understanding what it returns.
- Confusing parameters and arguments.
- Forgetting to return a value when a result is needed.
- Returning a value but not storing or using it.
- Giving functions unclear names.
- Writing one function that does too many tasks.
- Using global variables unnecessarily instead of parameters.
- Repeating code instead of creating a reusable function.
- Confusing functions with methods in object-oriented programming.
Better Habits
- Call the function after defining it.
- Use clear parameter names.
- Use return values when output is needed.
- Store returned values in meaningful variables.
- Name functions based on their purpose.
- Keep each function focused on one task.
- Use parameters instead of unnecessary global variables.
- Reuse functions instead of duplicating code.
- Test each function separately.
- Understand whether the reusable block is standalone or object-related.
Best Practices for Functions / Methods
Recommended Practices
- Give every function or method a clear purpose.
- Use meaningful names.
- Keep functions small and focused.
- Use parameters to pass required input.
- Use return values for results that need to be reused.
- Avoid unnecessary global variables.
- Avoid repeating the same code in multiple places.
- Test functions independently.
- Write comments only when the logic is not obvious.
- Separate input, processing, and output when possible.
Prerequisites Before Learning Functions / Methods
To understand functions and methods properly, students should already know these concepts:
Basic Prerequisites
- What is programming?
- Variables and constants.
- Data types.
- Input and output.
- Expressions.
- Operators.
- Control flow.
- Decision making.
- Loops and iteration.
- Basic problem-solving logic.
Practice Activity: Identify Function Parts
Identify the function name, parameters, arguments, and return value in the following pseudocode.
FUNCTION multiplyNumbers(firstNumber, secondNumber)
RETURN firstNumber * secondNumber
END FUNCTION
SET result = multiplyNumbers(4, 5)
Your Answer
Function Name:
________________________
Parameters:
________________________
Arguments:
________________________
Return Value:
________________________
Sample Answer
Function Name:
multiplyNumbers
Parameters:
firstNumber, secondNumber
Arguments:
4, 5
Return Value:
20
Mini Quiz
What is a function?
A function is a reusable block of code that performs a specific task.
What is a method?
A method is a function that belongs to a class or object.
What is a parameter?
A parameter is a variable written in the function definition to receive input.
What is an argument?
An argument is the actual value passed to a function during a function call.
Why are functions useful?
Functions reduce repetition, improve readability, support reuse, and make programs easier to maintain.
Interview Questions on Functions / Methods
Define function in programming.
A function is a named block of reusable code that performs a specific task and may accept input values or return a result.
What is the difference between function and method?
A function is usually standalone, while a method is associated with a class or object.
What is the difference between parameter and argument?
A parameter is defined in the function declaration, while an argument is the actual value passed during the function call.
What is a return value?
A return value is the result sent back by a function after it completes its processing.
Why should functions be small and focused?
Small and focused functions are easier to read, test, debug, reuse, and maintain.
Quick Summary
| Concept | Meaning |
|---|---|
| Function | A reusable block of code that performs a specific task. |
| Method | A function connected to a class or object. |
| Parameter | A variable in the function definition. |
| Argument | An actual value passed to a function. |
| Return Value | A result sent back from a function. |
| Function Call | The statement that runs a function. |
| Modular Programming | Dividing a program into smaller reusable parts. |
| Best Practice | Keep functions small, focused, reusable, and clearly named. |
Final Takeaway
Functions and methods are essential building blocks of programming. They help divide large programs into smaller, reusable, and meaningful parts. Functions are usually standalone reusable blocks, while methods are connected to objects or classes. In the Programming Mastery Course, students should understand functions and methods as tools for writing clean, organized, reusable, and maintainable code.
Master This Topic with Smart Practice
Reinforce what you just learned by solving high-quality MCQs. Improve accuracy, boost confidence, and prepare like a topper.