0% found this document useful (0 votes)
17 views6 pages

Understanding C Subprograms and Functions

A subprogram in C is a section of code, implemented as functions, designed to perform specific tasks, promoting reusability, encapsulation, and modularity. It consists of a function declaration, definition, and call, allowing for organized and maintainable code. Local variables are confined to their functions, while global variables are accessible throughout the program.

Uploaded by

hashijannat1000
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)
17 views6 pages

Understanding C Subprograms and Functions

A subprogram in C is a section of code, implemented as functions, designed to perform specific tasks, promoting reusability, encapsulation, and modularity. It consists of a function declaration, definition, and call, allowing for organized and maintainable code. Local variables are confined to their functions, while global variables are accessible throughout the program.

Uploaded by

hashijannat1000
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

1/31/2025

Subprogram

A subprogram is a general term for any section of code designed to


perform a specific task. It helps organize and manage large programs by
breaking them into smaller, reusable parts. In C, subprograms are
implemented as functions.

Characteristics of Subprograms

Reusability: Subprograms can be called multiple times with different


inputs.
Encapsulation: (A way to restrict the direct access to some components
of an object )The logic is self-contained within the function.
Structure: Simplifies complex problems by dividing them into smaller
tasks.
Modularity: Facilitates debugging and code maintenance.

1
1/31/2025

Structure of a Subprogram (Function):

Each function in C consists of:

Function Declaration (Prototype):Informs the compiler about the


function's name, return type, and parameters.
Syntax:
return_type function_name(data_type parameter1, data_type parameter2, ...);

Example: int add(int a, int b);

Function Definition: Contains the logic to be executed.


Syntax:

return_type function_name(data_type parameter1, data_type parameter2, ...) {


// Code
return value;
}

int add(int a, int b)


{
return a + b;
}

2
1/31/2025

Function Call: Executes the function from another part of the program.

Syntax:
function_name(arguments);

Example
sum = add(5, 10);

#include <stdio.h>

// Function Declaration
int multiply(int x, int y);

int main() {
int a = 3, b = 4;
int result = multiply(a, b); // Function Call
printf("Result: %d\n", result);
return 0;
}

// Function Definition
int multiply(int x, int y)
{
return x * y;
}

3
1/31/2025

#include <stdio.h> #include <stdio.h>


// Function Prototypes
// Function Prototypes int calculateArea(int length, int width);
int calculatePerimeter(int length, int width);
int calculateArea(int length, int width); int multiply(int a, int b); // Subfunction for multiplication
int calculatePerimeter(int length, int width); int sum(int a, int b); // Subfunction for addition
int main() {
int main() { int length = 5, width = 3;
int length = 5, width = 3; // Calling subprograms
int area = calculateArea(length, width);
Subprogram sub-function

// Calling subprograms int perimeter = calculatePerimeter(length, width);


printf("Area: %d\n", area);
int area = calculateArea(length, width);
Subprogram

printf("Perimeter: %d\n", perimeter);


int perimeter = calculatePerimeter(length, width); return 0;
}
printf("Area: %d\n", area); // Subprogram to calculate area using a subfunction
printf("Perimeter: %d\n", perimeter); int calculateArea(int length, int width) {
return multiply(length, width);
return 0; }
// Subprogram to calculate perimeter using a subfunction
} int calculatePerimeter(int length, int width) {
return sum(multiply(2, length), multiply(2, width));
// Subprogram to calculate area }
int calculateArea(int length, int width) { // Subfunction: Multiplies two numbers
return length * width; int multiply(int a, int b) {
} return a * b;
}
// Subfunction: Adds two numbers
// Subprogram to calculate perimeter int sum(int a, int b) {
int calculatePerimeter(int length, int width) { return a + b;
return 2 * (length + width); }
}

4
1/31/2025

A subprogram is a block of code or a self-contained program segment


designed to perform a specific task. In C programming, subprograms
are implemented as functions. Subprograms help to organize the
program, avoid redundancy

// Subprogram
int add(int a, int b)
{
return a + b; // Returns the sum of two numbers
}

A sub function is a function that is called or used within another function


(including the main function). It acts as a helper to break down a complex
task into smaller, manageable parts.
Difference from Subprogram: A sub function is still a function, but its
main purpose is to assist another function.
long long factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1); // Recursively calls itself as a
subfunction
}

long long nCr(int n, int r) {


return factorial(n) / (factorial(r) * factorial(n - r)); // Calls
factorial as a subfunction
}

5
1/31/2025

A local variable is a variable that is declared inside a function (including


main). It is accessible only within that specific function and cannot be
used or modified outside of it. Local variables exist only during the
execution of the function (i.e., they have local scope).

void example()
{
int localVar = 10; // Local variable
printf("Local Variable: %d\n", localVar);
}

A global variable is a variable that is declared outside all functions. It can


be accessed and modified by any function in the program. It has a global
scope and persists throughout the program's lifetime.

int globalVar = 20; // Global variable

void display() {
printf("Global Variable: %d\n", globalVar);
}

int main() {
display(); // Access globalVar
globalVar = 30; // Modify globalVar
display();
return 0;
}

You might also like