🟡 Intermediate  ·  Lesson 26

Pointers & Functions

Pointers with Functions

Pointers allow functions to work directly with the original variables by using their addresses. This is useful when a function needs to modify more than one value.

Call by Value

In call by value, a copy of the value is passed. Changes inside the function do not affect the original variable.

C Language
#include <stdio.h>

void change(int x) {
    x = 100;
}

int main() {
    int a = 10;
    change(a);
    printf("a = %d", a);
    return 0;
}
a = 10

Call by Address

In call by address, the address of the variable is passed. The function uses pointer parameters to modify the original value.

C Language
#include <stdio.h>

void swap(int *x, int *y) {
    int temp = *x;
    *x = *y;
    *y = temp;
}

int main() {
    int a = 10, b = 20;
    swap(&a, &b);
    printf("a = %d, b = %d", a, b);
    return 0;
}
a = 20, b = 10

Function with Array Pointer

C Language
#include <stdio.h>

int sumArray(int *arr, int n) {
    int sum = 0;
    for(int i = 0; i < n; i++)
        sum += arr[i];
    return sum;
}

int main() {
    int a[] = {5, 10, 15, 20};
    printf("Sum = %d", sumArray(a, 4));
    return 0;
}
Sum = 50

Returning Pointer from Function

⚠️ Never Return Local Variable Address

A local variable is destroyed when the function ends. Returning its address creates a dangling pointer.

C Language
#include <stdio.h>

int* greater(int *a, int *b) {
    if(*a > *b)
        return a;
    else
        return b;
}

int main() {
    int x = 40, y = 60;
    int *p = greater(&x, &y);
    printf("Greater = %d", *p);
    return 0;
}
Greater = 60

Summary

  • Call by value sends a copy
  • Call by address sends variable address
  • Use * to access or change original value
  • Use & while passing variable address
  • Pointers help functions return or modify multiple values

Frequently Asked Questions

How do you pass a pointer to a function in C?
You declare the function parameter as a pointer, for example void update(int *p), and call it with the address of a variable, such as update(&x). Inside the function you use *p to read or change the original variable directly.
What is call by address in C?
Call by address (also called call by reference) means passing the address of a variable to a function instead of its value. Because the function receives the address, changes it makes through the pointer affect the caller's original variable, unlike call by value which works on a copy.
Why use pointers in function arguments?
Pointers let a function modify the caller's variables, return more than one result, and avoid copying large data such as arrays or structures. Passing an address is cheap and lets the function work on the real data rather than a copy.
Can a function return a pointer in C?
Yes, a function can return a pointer, but it must not return the address of a local variable, because that memory is freed when the function ends. It is safe to return a pointer to memory from malloc or to something that lives beyond the function.
What is the difference between call by value and call by address?
Call by value passes a copy, so the function cannot change the caller's variable. Call by address passes the variable's address, so the function can change the original through the pointer. Use call by address when you need the changes to persist.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.