Function
Function
Function
A function is a block of code designed to perform a specific task, improving
code reusability and organization. Instead of writing the same code repeatedly,
you can call a function whenever needed. Functions are also useful for
breaking down complex programs into manageable parts.
• Function are also called modules or procedures
• ADVANTEGES
• Organize the code
• Reusability
Types of Functions in C
1.Library Functions: Predefined functions provided by C's standard libraries, such as printf(), scanf(), sqrt(), etc
.
2.User-defined Functions: Functions created by the programmer to perform specific tasks.
2D array
• A 2D array in C is essentially an array of arrays. It is used to store
tabular data, like a matrix or a grid, where each element is addressed
by two indices: the row index and the column index
• data_type array_name[row_size][column_size];
• #include <stdio.h>
int main() {
// Declaring and initializing a 2D array
int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
// Printing the 2D array
printf("Matrix:\n");
for (int i = 0; i < 3; i++)
{// Row loop
for (int j = 0; j < 3; j++) { // Column loop
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;}
#include <stdio.h> 2D arrays in C
int main() {
int rows, cols; // Input rows and columns
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols); // Declare a 2D array int
array[rows][cols]; // Input elements into the array
printf("Enter the elements of the array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
{ printf("Element [%d][%d]: ", i, j);
scanf("%d", &array[i][j]);
} }
// Calculate and print the sum of each row printf("\
nRow-wise sum:\n");
for (int i = 0; i < rows; i++)
{ int rowSum = 0;
for (int j = 0; j < cols; j++)
{
rowSum += array[i][j];
}
printf("Sum of row %d: %d\n", i + 1, rowSum);
}
return 0;
String with Character Array
The extern storage class is used to share global variables across multiple files. It informs the compiler that the variable
is defined elsewhere, possibly in another file.
File 1:
int count = 0; // Definition
File 2:
extern int count; // Declaration
void func()
{
printf("%d\n", count); // Accessing the variable from File 1
}
3.Static (static):
•Retains the value of a variable across function calls or limits the scope of a global variable to the file in
which it is declared.
•Scope: Local to the block (if declared inside a function) or file (if declared globally).
•Lifetime: Entire runtime of the program.
•Example:
void func()
{
static int counter = 0; counter++;
}
When a variable inside a function is declared as static, its value is retained across multiple calls to the function, even
though its scope is local to the function.
void func()
{
static int counter = 0; counter++;
printf("Counter: %d\n", counter); // Outputs: Counter: 1, Counter: 2, ...
}
4.Register (register):
•Suggests that the variable should be stored in a CPU register for faster access.
•Scope: Local to the block.
•Lifetime: Only exists while the block is being executed.
•Example:
void func()
{
register int i;
}
5. The register storage class suggests to the compiler that the variable should be stored in a CPU register for faster access. This
is particularly useful for variables that are frequently used in loops.
void func()
{
register int i;
for (i = 0; i < 10; i++)
{
printf("%d ", i); // Outputs: 0 1 2 3 4 5 6 7 8 9
}}
• Each storage class has its unique use cases based on variable scope, lifetime, and performance
considerations.