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

5.2.notes - Modifying Parameters Inside Functions Using

This document explains how to modify variables and arrays in C functions using pointers. It highlights the difference between passing by value and passing by reference, with examples demonstrating how to swap values and modify array elements. Key concepts include the use of pointers for direct data manipulation and best practices for ensuring valid memory references.
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)
3 views3 pages

5.2.notes - Modifying Parameters Inside Functions Using

This document explains how to modify variables and arrays in C functions using pointers. It highlights the difference between passing by value and passing by reference, with examples demonstrating how to swap values and modify array elements. Key concepts include the use of pointers for direct data manipulation and best practices for ensuring valid memory references.
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

Notes: Modifying Parameters Inside Functions

Using Pointers and Arrays as Parameters


For First Year Mechanical Engineering Students

1. Introduction
By default, C passes arguments to functions by value—the function receives a copy of the
data, so changes inside the function do not affect the original variable.
With pointers and arrays, you can allow a function to modify the original data in the calling
context.

2. Modifying Variables Inside Functions Using Pointers

Why Use Pointers?


Passing a pointer (address) to a function lets it directly access and modify the original data,
not just a local copy.

Example: Swap Two Values Using Pointers

void swap(int *x, int *y) {


int temp = *x;
*x = *y;
*y = temp;
}

// Usage
int a = 5, b = 10;
swap(&a, &b);
// Now, a is 10 and b is 5

*x and *y let you modify the variables at those addresses (i.e., the original a and b).

Key Points:
The & operator passes the address (pointer) of the variables.
The * operator (dereference) modifies the value at that address.
3. Passing Arrays as Function Parameters

How Arrays Are Passed


In C, an array name used as a function argument actually passes a pointer to the first
element.
This means functions can modify contents of the array directly.

Example: Modifying Array Elements in a Function

void doubleValues(int arr[], int size) {


for (int i = 0; i < size; i++) {
arr[i] *= 2; // Modifies original array
}
}

// Usage
int data[3] = {1, 2, 3};
doubleValues(data, 3);
// Now, data contains 2, 4, 6

The function modifies the actual elements of data in the calling function.

Alternative Notation

void doubleValues(int *arr, int size) // Equivalent to above

Both notations work since an array name is a pointer to its first element.

4. Tables: Key Differences


Original Data
How What Happens Typical Syntax Example
Changes?

Value Receives a copy No void fun(int x)

Receives the address, can write to void fun(int *px);


Pointer Yes
original fun(&a);

Array (by void fun(int arr[],


Receives pointer to first element Yes
nature) int n)

5. Practical Applications
Functions using pointers: Modify single values or implement swaps, update results, etc.
Functions using arrays: Fill, alter, analyze, or process large datasets directly (e.g., sorting,
searching, data manipulation in engineering tasks).
6. Key Concepts and Best Practices
Use pointers as function parameters when you need to change the original variable(s).
Arrays are always passed by reference; changing elements inside a function changes them
outside as well.
Always ensure pointers and arrays reference valid memory to avoid errors.

7. Quick Reference Code Patterns

// Modify single variable


void setZero(int *p) { *p = 0; }

// Modify array
void invert(int arr[], int len) {
for (int i = 0; i < len; i++) arr[i] = -arr[i];
}

Call as setZero(&a); or invert(data, N);

Summary:
Use pointers or arrays as parameters in C to allow functions to modify original data
directly.
This technique is essential for efficient programming and is widely used in engineering for
handling sensor readings, simulation data, and other mutable values.

You might also like