Explaining pass-by-value and pass-by-reference in C for 1st year IT student

TL;DR

Pass-by-value: value of parameters in a function will NOT be changed after function is called.
Pass-by-reference: value of parameters in a function MIGHT be changed after function is called (when they’re updated in the function).


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Ryan

TL;DR

  • Pass-by-value: value of parameters in a function will NOT be changed after function is called.
  • Pass-by-reference: value of parameters in a function MIGHT be changed after function is called (when they're updated in the function).

Pass-by-value

When you declare a variable, it allocates a memory for that variable. When you call a function foo(variable_name), it means you pass the value of that variable in the memory to a function. Whatever the function does, it doesn't affect to the memory of the variable. Value of the variable remains the same.

Example code:

#include <stdio.h>

void foo(int x);

int main() {
    int a = 1;

    foo(a);

    printf("a: %d", a); //a: 1

    return 0;
}

void foo(int x) {
    x = 5;
}

In the sample code above, when you call foo(a):

  • It actually call foo(1)
  • In the foo(int x) function, it allocates a new memory for x, then sets x to 5
  • In the main function, a is still 1

Pass-by-reference

As mentioned, when you declare a variable, it allocates a memory for that variable. Memory has memory address. When you call a function foo(variable_memory_address), it means you pass the memory address to a function. If you change the value of the memory in the function, you actually update the value in the memory of the variable. That's why variable value will be changed.

Example code:

#include <stdio.h>

void foo(int* px);

int main() {
    int a = 1;

    foo(&a);

    printf("a: %d", a); //a: 5

    return 0;
}

void foo(int* px) {
    *px = 5;
}

In the sample code above, when you call foo(&a):

  • It actually call foo(0xAB) (assumed memory address of variable a is 0xAB)
  • In the foo(int* px) function, it creates a pointer px, and px points to 0xAB
  • When you set *px = 5, it means it sets the value at memory _0xAB _to 5
  • Then in the main function, as value in the memory of variable a was changed to 5, value of a now is 5.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Ryan


Print Share Comment Cite Upload Translate Updates
APA

Ryan | Sciencx (2022-10-01T08:59:12+00:00) Explaining pass-by-value and pass-by-reference in C for 1st year IT student. Retrieved from https://www.scien.cx/2022/10/01/explaining-pass-by-value-and-pass-by-reference-in-c-for-1st-year-it-student/

MLA
" » Explaining pass-by-value and pass-by-reference in C for 1st year IT student." Ryan | Sciencx - Saturday October 1, 2022, https://www.scien.cx/2022/10/01/explaining-pass-by-value-and-pass-by-reference-in-c-for-1st-year-it-student/
HARVARD
Ryan | Sciencx Saturday October 1, 2022 » Explaining pass-by-value and pass-by-reference in C for 1st year IT student., viewed ,<https://www.scien.cx/2022/10/01/explaining-pass-by-value-and-pass-by-reference-in-c-for-1st-year-it-student/>
VANCOUVER
Ryan | Sciencx - » Explaining pass-by-value and pass-by-reference in C for 1st year IT student. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/01/explaining-pass-by-value-and-pass-by-reference-in-c-for-1st-year-it-student/
CHICAGO
" » Explaining pass-by-value and pass-by-reference in C for 1st year IT student." Ryan | Sciencx - Accessed . https://www.scien.cx/2022/10/01/explaining-pass-by-value-and-pass-by-reference-in-c-for-1st-year-it-student/
IEEE
" » Explaining pass-by-value and pass-by-reference in C for 1st year IT student." Ryan | Sciencx [Online]. Available: https://www.scien.cx/2022/10/01/explaining-pass-by-value-and-pass-by-reference-in-c-for-1st-year-it-student/. [Accessed: ]
rf:citation
» Explaining pass-by-value and pass-by-reference in C for 1st year IT student | Ryan | Sciencx | https://www.scien.cx/2022/10/01/explaining-pass-by-value-and-pass-by-reference-in-c-for-1st-year-it-student/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.