DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Ryan
Ryan

Posted on • Updated on

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

TL;DR

When you pass by value what you’re doing is passing some data and the function then makes a copy of that data inside that function. This variable is scoped to the function block. Modifying this variable only affects the variable inside the function because it only exists as a copy inside the function. It’s scoped.

With pass by reference you are passing the address or the actual location of where your data lives. By doing this you can update the actual value of whatever data is at the memory address. You don’t have a copy of the object, you have the location of the actual object and therefore can directly modify it from inside the function.

(This TL;DR is contributed by @codebytesfl - Thank you!)

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (2)

Collapse
 
codebytesfl profile image
codebytesfl

You’re on the right track, but your TLDR can be explained more accurately. It’s not about whether or not a value MIGHT change, it’s about whether or not you’re working with a copy or a reference to the actual object.

When you pass by value what you’re doing is passing some data and the function then makes a copy of that data inside that function. This variable is scoped to the function block. Modifying this variable only affects the variable inside the function because it only exists as a copy inside the function. It’s scoped.

With pass by reference you are passing the address or the actual location of where your data lives. By doing this I can update the actual value of whatever data is at the memory address. I don’t have a copy of the object, I have the location of the actual object and therefore can directly modify it from inside the function.

Collapse
 
ryannz profile image
Ryan Author

That makes more sense. Thank you for your constructive comment. I also added this to the article.

πŸ€” Did you know?

🌚 You can turn on dark mode in Settings