🟡 Intermediate  ·  Lesson 18

Function Arguments & Return

Function Arguments

Function Arguments (Parameters)

When you call a function, you can pass data to it. The values you pass are called arguments. The variables that receive them in the function definition are called parameters.

In C, there are two ways to pass arguments:

  • Pass by Value — a copy of the value is passed (default)
  • Pass by Reference — the address (pointer) is passed, so the original can be modified

Pass by Value

A copy of the argument's value is passed. Changes inside the function do NOT affect the original variable.

C Language – Pass by Value Demo
#include <stdio.h>

void doubleIt(int n) {    // n is a COPY of the argument
    n = n * 2;
    printf("Inside function: n = %d\n", n);
}

int main() {
    int x = 5;
    printf("Before call: x = %d\n", x);
    doubleIt(x);              // Passes a COPY of x
    printf("After call:  x = %d\n", x); // x unchanged!
    return 0;
}
Before call: x = 5 Inside function: n = 10 After call: x = 5
💡 Why x didn't change?

When you call doubleIt(x), C copies the value of x (which is 5) into the parameter n. The function works on its own local copy n, not on the original x. So x remains 5 after the call.

Pass by Reference (Using Pointers)

Pass the address of the variable. The function receives a pointer and can modify the original variable through it.

C Language – Pass by Reference Demo
#include <stdio.h>

void doubleIt(int *n) {   // n is a POINTER to the original
    *n = *n * 2;           // Modify value at the address
    printf("Inside function: *n = %d\n", *n);
}

int main() {
    int x = 5;
    printf("Before call: x = %d\n", x);
    doubleIt(&x);            // Pass ADDRESS of x
    printf("After call:  x = %d\n", x); // x IS modified!
    return 0;
}
Before call: x = 5 Inside function: *n = 10 After call: x = 10

Classic Example: swap() Function

C Language – Correct Swap
#include <stdio.h>

// WRONG: pass by value — doesn't work!
void swapWrong(int a, int b) {
    int temp = a; a = b; b = temp;  // Only local copies swapped
}

// CORRECT: pass by reference — works!
void swapCorrect(int *a, int *b) {
    int temp = *a; *a = *b; *b = temp;
}

int main() {
    int x = 10, y = 20;
    swapWrong(x, y);
    printf("After swapWrong:   x=%d, y=%d\n", x, y);  // Still 10, 20
    swapCorrect(&x, &y);
    printf("After swapCorrect: x=%d, y=%d\n", x, y);  // 20, 10
    return 0;
}
After swapWrong: x=10, y=20 After swapCorrect: x=20, y=10

Arrays as Function Parameters

Arrays are always passed by reference (as a pointer to the first element). The function can modify the original array.

C Language – Array as Parameter
#include <stdio.h>

void printArray(int arr[], int n) {  // arr[] same as int *arr
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
}

void doubleArray(int arr[], int n) {
    for (int i = 0; i < n; i++)
        arr[i] *= 2;  // Modifies ORIGINAL array!
}

int main() {
    int nums[] = {1, 2, 3, 4, 5};
    printf("Before: "); printArray(nums, 5);
    doubleArray(nums, 5);
    printf("After:  "); printArray(nums, 5);
    return 0;
}
Before: 1 2 3 4 5 After: 2 4 6 8 10

Returning Multiple Values via Pointers

A function can only return ONE value. But using pointers, you can "return" multiple values.

C Language – Min and Max in one call
#include <stdio.h>

void minMax(int arr[], int n, int *minVal, int *maxVal) {
    *minVal = *maxVal = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] < *minVal) *minVal = arr[i];
        if (arr[i] > *maxVal) *maxVal = arr[i];
    }
}

int main() {
    int arr[] = {85, 32, 97, 45, 11, 78};
    int n = 6, minV, maxV;
    minMax(arr, n, &minV, &maxV);
    printf("Min = %d, Max = %d\n", minV, maxV);
    return 0;
}
Min = 11, Max = 97

Summary

FeaturePass by ValuePass by Reference
What is passedCopy of the valueAddress (pointer) of variable
Original modified?❌ No✅ Yes
Syntax (caller)func(x)func(&x)
Syntax (function)void f(int n)void f(int *n)
ArraysAlways by reference automatically
  • Default in C is pass by value — function gets a copy
  • Use pass by reference (pointers) when you want to modify the original
  • Arrays are always passed by reference (as pointer to first element)
  • Pass pointers to "return" multiple values from a function

Function Arguments (Parameters)

Function call करते समय जो data pass करते हैं वो arguments कहलाते हैं। Function definition में जो variables उन्हें receive करते हैं वो parameters कहलाते हैं।

C में arguments pass करने के दो तरीके:

  • Pass by Value — value की copy pass होती है (default)
  • Pass by Reference — address (pointer) pass होता है, original modify हो सकता है

Pass by Value

Argument की value की copy pass होती है। Function के अंदर changes original variable को affect नहीं करते।

C Language
void double_karo(int n) {   // n, argument की COPY है
    n = n * 2;               // केवल local copy बदली
    printf("Function mein: %d\n", n);
}
int main() {
    int x = 5;
    double_karo(x);            // x की copy pass
    printf("Baad mein: %d\n", x); // x नहीं बदला!
}
Function mein: 10 Baad mein: 5

Pass by Reference (Pointers)

Variable का address pass होता है। Function pointer के ज़रिये original variable को modify कर सकता है।

C Language – Swap Correct
#include <stdio.h>

void swap(int *a, int *b) {  // Pointer receive करता है
    int temp = *a; *a = *b; *b = temp;
}

int main() {
    int x = 10, y = 20;
    printf("Pehle: x=%d, y=%d\n", x, y);
    swap(&x, &y);              // Address pass
    printf("Baad:  x=%d, y=%d\n", x, y);
    return 0;
}
Pehle: x=10, y=20 Baad: x=20, y=10

Multiple Values Return करना

C Language – Min Max ek call mein
void minMax(int arr[], int n, int *mn, int *mx) {
    *mn = *mx = arr[0];
    for(int i=1; i<n; i++) {
        if(arr[i] < *mn) *mn = arr[i];
        if(arr[i] > *mx) *mx = arr[i];
    }
}

int main() {
    int a[] = {85,32,97,45,11}, mn, mx;
    minMax(a, 5, &mn, &mx);
    printf("Min=%d, Max=%d\n", mn, mx);
}
Min=11, Max=97

सारांश

FeaturePass by ValuePass by Reference
क्या pass होता हैValue की copyAddress (pointer)
Original बदलता है?❌ नहीं✅ हाँ
Caller syntaxfunc(x)func(&x)
Function syntaxvoid f(int n)void f(int *n)
Arraysहमेशा automatically by reference
  • Default: pass by value — function copy पर काम करता है
  • Original modify करने के लिए: pass by reference (pointers)
  • Arrays हमेशा by reference pass होते हैं (first element का pointer)
  • Multiple values "return" करने के लिए pointers use करें
← Back to C Tutorial