0% found this document useful (0 votes)
29 views3 pages

Subroutines Definition Types and Examples in C

Subroutines

Uploaded by

mforytb1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

Subroutines Definition Types and Examples in C

Subroutines

Uploaded by

mforytb1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Subroutines in C++

A subroutine, also known as a procedure, function, or method, is a self-contained block of code


that performs a specific task. It's a fundamental concept in programming that promotes
modularity, reusability, and code organization.

Definition

In C++, a subroutine is typically defined using the void keyword for procedures and a return type
for functions. The syntax is as follows:

C++

return_type function_name(parameter_list) {
// Function body
}

● return_type: Specifies the data type of the value returned by the function. If the function
doesn't return a value, use void.
● function_name: A unique identifier for the subroutine.
● parameter_list: A comma-separated list of parameters (variables) that the function receives
from the caller.

Types of Subroutines in C++

1. Functions:
○ Return a value.
○ Used for calculations, data retrieval, and other tasks that produce results.
○ Example:
C++
int calculate_area(int length, int width) {
return length * width;
}

2. Procedures (Void Functions):


○ Do not return a value.
○ Used for performing actions, modifying data, or controlling program flow.
○ Example:
C++
void greet(std::string name) {
std::cout << "Hello, " << name << "!" << std::endl;
}
Subroutine Parameters and Arguments

● Parameters: Variables declared within the subroutine's parentheses to receive values from
the caller.
● Arguments: Actual values passed to the subroutine when it's called.
● Example:
C++
void greet(std::string name) { // name is a parameter
std::cout << "Hello, " << name << "!" << std::endl;
}

greet("Alice"); // "Alice" is an argument

Scope of Variables

● Local variables: Declared within a subroutine, accessible only within that subroutine.
● Global variables: Declared outside subroutines, accessible from anywhere in the program.
● Example:
C++
int global_var = 10;

void my_function() {
int local_var = 5;
std::cout << global_var << std::endl; // Accesses global
variable
// std::cout << local_var << std::endl; // Would cause an error
if used outside the function
}

Recursion

● A subroutine that calls itself directly or indirectly is called recursive.


● Used for solving problems that can be broken down into smaller instances of the same
problem.
● Example:
C++
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
Best Practices for Subroutines

● Use meaningful names for subroutines and parameters.


● Keep subroutines concise and focused on a single task.
● Avoid excessive nesting of subroutines.
● Use comments to explain the purpose of subroutines and their parameters.
● Test subroutines thoroughly to ensure they work as expected.

By following these guidelines, you can effectively write well-structured and maintainable code
using subroutines in C++.

You might also like