Knowlet

Unit 5: Return Statement and Arrays

Return Statement

The return statement is used inside a function to end its execution and (optionally) send a value back to the caller.

Two Main Uses:

  1. Returning a value:

    In a function with a non-`void` return type (like int or float), return sends a value back.

    int add(int a, int b) { return a + b; // Returns the calculated sum } 
  2. Terminating a `void` function:

    In a void function (which returns nothing), return; can be used to exit the function early.

    void printNumber(int n) { if (n < 0) { printf("Number is negative.\n"); return; // Exits the function here } printf("Number is: %d\n", n); } 

Arrays

An array is a collection of data items of the same data type, stored in contiguous (one after another) memory locations. This allows you to store a list of values (like 100 student scores) under a single variable name.

One-Dimensional Arrays

A 1D array is a simple list of elements.

Declaration:
data_type array_name[size];
Example: int scores[5]; (This reserves space for 5 integers).

Initialization:
int scores[5] = { 98, 87, 92, 79, 100 };

Accessing Elements (0-based Indexing):
Array elements are accessed using an index in square brackets []. The index always starts at 0.

  • scores[0] is 98
  • scores[1] is 87
  • ...
  • scores[4] is 100

Arrays are most powerfully used with for loops to iterate over the elements.

for (int i = 0; i < 5; i++) { printf("Element %d is %d\n", i, scores[i]); } 

Multi-Dimensional Arrays (2D Arrays/Matrices)

A multi-dimensional array is an "array of arrays." The most common is a 2D array, which is perfect for representing a matrix or a grid.

Declaration:
data_type array_name[rows][columns];
Example: int matrix[2][3]; (A matrix with 2 rows and 3 columns).

Initialization:
int matrix[2][3] = { { 1, 2, 3 }, // Row 0 { 4, 5, 6 } // Row 1 };

Accessing Elements (row, col):
You need two indices: matrix[row][col].

  • matrix[0][0] is 1
  • matrix[0][2] is 3
  • matrix[1][1] is 5

2D arrays are traversed using nested for loops (one for rows, one for columns).

// Loop through each row for (int i = 0; i < 2; i++) { // Loop through each column in that row for (int j = 0; j < 3; j++) { printf("Element (%d, %d) is %d\n", i, j, matrix[i][j]); } } 

Code Examples (from Practicals)

Practical #8: Sorting an Array (Bubble Sort)

#include <stdio.h> #define SIZE 5 int main() { int arr[SIZE] = { 5, 1, 4, 2, 8 }; int i, j, temp; printf("Original array: "); for (i = 0; i < SIZE; i++) printf("%d ", arr[i]); printf("\n"); // Bubble Sort Algorithm for (i = 0; i < SIZE - 1; i++) { for (j = 0; j < SIZE - i - 1; j++) { if (arr[j] > arr[j + 1]) { // For ascending order // Swap elements temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } printf("Sorted array: "); for (i = 0; i < SIZE; i++) printf("%d ", arr[i]); printf("\n"); return 0; } 

Practical #9: Matrix Addition (2D)

#include <stdio.h> #define ROWS 2 #define COLS 3 int main() { int A[ROWS][COLS] = { {1, 2, 3}, {4, 5, 6} }; int B[ROWS][COLS] = { {9, 8, 7}, {6, 5, 4} }; int C[ROWS][COLS]; // Result matrix int i, j; // Add corresponding elements for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { C[i][j] = A[i][j] + B[i][j]; } } // Print the result matrix C printf("Resulting Matrix C (A+B):\n"); for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { printf("%d\t", C[i][j]); } printf("\n"); // Newline after each row } return 0; } 

Practical #10b: Determinant of a 3x3 Matrix (2D)

#include <stdio.h> int main() { int A[3][3]; long det; int i, j; printf("Enter the 9 elements of the 3x3 matrix:\n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("Enter element A[%d][%d]: ", i, j); scanf("%d", &A[i][j]); } } // Calculate determinant by expanding along Row 0 // det = a(ei - fh) - b(di - fg) + c(dh - eg) det = A[0][0] * (A[1][1] * A[2][2] - A[1][2] * A[2][1]) - A[0][1] * (A[1][0] * A[2][2] - A[1][2] * A[2][0]) + A[0][2] * (A[1][0] * A[2][1] - A[1][1] * A[2][0]); printf("The determinant of the matrix is: %ld\n", det); return 0; } 

Unit 5: Exam Quick Tips

  • 0-Based Indexing: This is the most important concept. An array of size 5, arr[5], has elements arr[0], arr[1], arr[2], arr[3], and arr[4]. There is no such thing as arr[5]. Accessing it is a major bug (a "buffer overflow").
  • Loops and Arrays: The for loop is the natural partner for an array.
    for (int i = 0; i < SIZE; i++) is the standard, error-free way to loop through a 1D array.
  • 2D Arrays (Matrices): Always use nested loops. The outer loop iterates the rows (index i). The inner loop iterates the columns (index j).
    for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { ... A[i][j] ... } }
  • `return` Statement: Remember, as soon as a `return` statement is hit, the function stops. No code after it in that function will run.

Did this resource help you study?

Share feedback or report issues to help improve this resource.