0% found this document useful (0 votes)
7 views8 pages

Converted Text

The document discusses arrays and functions in programming, highlighting their characteristics, types, operations, advantages, and limitations. Arrays are collections of homogeneous elements with fixed sizes, while functions are reusable code blocks that perform specific tasks. The document also provides examples and a summary table for better understanding.
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)
7 views8 pages

Converted Text

The document discusses arrays and functions in programming, highlighting their characteristics, types, operations, advantages, and limitations. Arrays are collections of homogeneous elements with fixed sizes, while functions are reusable code blocks that perform specific tasks. The document also provides examples and a summary table for better understanding.
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/ 8

Class Notes: Arrays and Functions

1. Arrays

An array is a collection of elements of the same data type, stored in contiguous memory
locations. Arrays are used to store multiple values in a single variable.

A. Characteristics of Arrays

Fixed Size: The size of the array is determined when it is declared.

Homogeneous: All elements of the array must be of the same data type (e.g., integers, floats).

Indexed: Array elements are accessed via indices, starting from 0 (zero-based index).

B. Types of Arrays
1.
One-dimensional Array (1D Array):

A simple list of elements.

Example: int arr[5] = {1, 2, 3, 4, 5};

2.
Two-dimensional Array (2D Array):
A matrix or table of elements (rows and columns).

Example: int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};

3.
Multi-dimensional Array:

Arrays with more than two dimensions, useful for storing complex data.

Example: int arr[3][3][3];

C. Array Operations

Accessing Elements: Using the index notation.

Example: arr[0] accesses the first element.

Traversing: Iterating through array elements.

Example: Loop through a 1D array:

cpp

CopyEdit
for(int i = 0; i < 5; i++) { printf("%d ", arr[i]); }

Array Initialization:

1D Array: int arr[] = {10, 20, 30};

2D Array: int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};

D. Advantages and Limitations of Arrays

Advantages:

Efficient in terms of memory.

Fast access to elements using indices.

Limitations:

Fixed size (in most languages).

Homogeneous (cannot store different data types in the same array).


2. Functions

A function is a block of code designed to perform a specific task. Functions help break down
programs into smaller, reusable, and organized pieces.

A. Characteristics of Functions

Return Type: The type of value the function will return (e.g., int, float, void).

Function Name: The identifier used to call the function.

Parameters: Values passed into the function.

Return Statement: Specifies what the function will return.

B. Types of Functions
1.
Standard Library Functions:

Predefined functions in libraries, such as printf(), sqrt(), etc.

2.
User-defined Functions:

Functions created by the programmer for specific tasks.


C. Syntax for Defining Functions (in C/C++)

cpp

CopyEdit

return_type function_name(parameters) { // Body of the function return value; // Only if the return
type is not void }

Example:

cpp

CopyEdit

int add(int a, int b) { return a + b; }

D. Calling a Function

To call a function, use its name followed by the parameters (if any):

cpp

CopyEdit

int result = add(5, 3); // Calls the 'add' function


E. Types of Functions Based on Return and Parameters
1.
Functions with No Arguments and No Return Value:

cpp

CopyEdit

void printMessage() { printf("Hello World"); }

2.
Functions with Arguments and No Return Value:

cpp

CopyEdit

void displayMessage(char* message) { printf("%s", message); }

3.
Functions with Arguments and Return Value:

cpp

CopyEdit

int multiply(int a, int b) { return a * b; }


F. Advantages of Using Functions

Modularity: Code is divided into smaller chunks.

Reusability: Functions can be reused multiple times in a program.

Maintainability: Easier to maintain and debug smaller sections of code.

Abstraction: Hides implementation details and provides a clear interface.

3. Example: Combining Arrays and Functions

Here is an example of using an array inside a function to find the sum of its elements:

cpp

CopyEdit

#include <stdio.h> int sumArray(int arr[], int size) { int sum = 0; for(int i = 0; i < size; i++) { sum +=
arr[i]; } return sum; } int main() { int arr[] = {1, 2, 3, 4, 5}; int result = sumArray(arr, 5); // Call the
function with the array printf("Sum of array elements: %d", result); return 0; }

4. Summary Table:
Concept Definition Example/Usage
Array Collection of elements of the same type int arr[] = {1, 2, 3};
Function Block of code that performs a specific task int add(int a, int b) { return a + b; }
Array Index Index used to access array elements arr[0] (first element)
Function Call Executing a function with arguments add(5, 3)

5. Conclusion

Arrays and functions are fundamental concepts in programming. Arrays allow us to handle
multiple data items, and functions provide modularity and reusability in code.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy