Call by Value ์™€ Call by Reference

๊ถŒํƒœํ˜•ยท2023๋…„ 3์›” 23์ผ
6

์ง€์‹์ •๋ฆฌ

๋ชฉ๋ก ๋ณด๊ธฐ
43/72
post-thumbnail

๐Ÿ˜€๋ฉด์ ‘์„ ์œ„ํ•œ CS์ง€์‹์„ ์ •๋ฆฌํ•˜๋ฉด ์ฝœ ๋ฐ”์ด ๋ฒจ๋ฅ˜(Call by Value)์™€ ์ฝœ ๋ฐ”์ด ๋ ˆํผ๋Ÿฐ์Šค(Call by Reference)์— ๋Œ€ํ•ด ์ •๋ฆฌํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

"์ฝœ ๋ฐ”์ด ๋ฒจ๋ฅ˜(Call by Value)"์™€ "์ฝœ ๋ฐ”์ด ๋ ˆํผ๋Ÿฐ์Šค(Call by Reference)"๋Š” ํ•จ์ˆ˜๊ฐ€ ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ์‹์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
ํ•˜์ง€๋งŒ ๋‘๊ฐ€์ง€๋Š” ์ด๋ฆ„ ์ฒ˜๋Ÿผ ๊ฐ’๊ณผ ์ฐธ์กฐ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ฐจ์ด๋ฅผ ๊ฐ€์ง„๋‹ค.

์ฝœ ๋ฐ”์ด ๋ฒจ๋ฅ˜(Call by Value)

์ฝœ๋ฐ”์ด๋ฒจ๋ฅ˜(Call by Value)๋Š” ํ•จ์ˆ˜๊ฐ€ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ๊ฐ’์„ ๋ณต์‚ฌํ•˜์—ฌ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
์ด๋•Œ, ์ „๋‹ฌ๋œ ๊ฐ’์€ ๊ธฐ๋ณธํ˜•, ์ฆ‰ JS์˜ ์›์‹œ ํƒ€์ž…(primitive type) ๋ฐ์ดํ„ฐ์ธ ๊ฒฝ์šฐ์— ํ•ด๋‹นํ•˜๋ฉฐ, ๋ณ€์ˆ˜๊ฐ€ ๊ฐ€์ง„ ๊ฐ’์„ ๋ณต์‚ฌํ•˜์—ฌ ์ „๋‹ฌํ•˜๋ฏ€๋กœ ํ•จ์ˆ˜ ๋‚ด์—์„œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด๋„ ์›๋ณธ ๊ฐ’์€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ’์˜ ๋ถˆ๋ณ€์„ฑ(Immutability)์„ ์œ ์ง€ํ•˜๋Š” ๋ฐ์— ์šฉ์ดํ•˜๋‹ค.

function addOne(x) {
  x += 1;
  return x;
}

let num = 5;
console.log(addOne(num)); // 6
console.log(num); // 5

์œ„ ์˜ˆ์‹œ์—์„œ num ๋ณ€์ˆ˜๊ฐ€ ๊ฐ€์ง„ ๊ฐ’์„ addOne() ํ•จ์ˆ˜์— ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋ฉด, x๋ผ๋Š” ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜์— ๊ฐ’์ด ๋ณต์‚ฌ๋œ๋‹ค.
addOne() ํ•จ์ˆ˜ ๋‚ด์—์„œ x ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด๋„ ์›๋ณธ num ๊ฐ’์€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š”๋‹ค.

  • ์žฅ์  : ๋ณต์‚ฌํ•˜์—ฌ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•ˆ์ „ํ•˜๋‹ค. ์›๋ž˜์˜ ๊ฐ’์ด ๋ณด์กด์ด ๋œ๋‹ค.

  • ๋‹จ์  : ๋ณต์‚ฌ๋ฅผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์‚ฌ์šฉ๋Ÿ‰์ด ๋Š˜์–ด๋‚œ๋‹ค.


์ฝœ ๋ฐ”์ด ๋ ˆํผ๋Ÿฐ์Šค(Call by Reference)

์ฝœ๋ฐ”์ด๋ ˆํผ๋Ÿฐ์Šค(Call by Reference)๋ž€ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋˜๋Š” ๋ณ€์ˆ˜์˜ ์ฐธ์กฐ ๊ฐ’์„ ํ•จ์ˆ˜ ๋‚ด๋ถ€๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
์ด ๋ฐฉ์‹์—์„œ๋Š” ํ•จ์ˆ˜ ๋‚ด์—์„œ ์ธ์ž๋กœ ์ „๋‹ฌ๋œ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ฉด, ํ˜ธ์ถœํ•œ ์ชฝ์—์„œ๋„ ํ•ด๋‹น ๋ณ€์ˆ˜์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋œ๋‹ค. ์ด๋Š” ์ธ์ž๋กœ ์ „๋‹ฌ๋˜๋Š” ๊ฐ’์ด ๋ณ€์ˆ˜์˜ ์ฃผ์†Œ์ด๋ฏ€๋กœ, ํ•จ์ˆ˜ ๋‚ด์—์„œ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ฉด ํ•ด๋‹น ์ฃผ์†Œ์— ์ €์žฅ๋œ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

function addOne(arr) {
  arr.push(1);
  return arr;
}

let nums = [1, 2, 3];
let result = addOne(nums);
console.log(result); // ์ถœ๋ ฅ: [1, 2, 3, 1]
console.log(nums); // ์ถœ๋ ฅ: [1, 2, 3, 1] (์›๋ณธ ๊ฐ’๋„ ํ•จ๊ป˜ ๋ณ€๊ฒฝ๋จ)

์˜ˆ์‹œ์—์„œ addOne ํ•จ์ˆ˜์— nums ๋ณ€์ˆ˜์˜ ์ฐธ์กฐ ๊ฐ’์„ ์ „๋‹ฌํ•˜๋ฉด, arr ๋ณ€์ˆ˜์—๋„ nums ๋ณ€์ˆ˜์˜ ์ฐธ์กฐ ๊ฐ’์ด ๋ณต์‚ฌ๋œ๋‹ค.
๋”ฐ๋ผ์„œ, ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ arr ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ฉด ์›๋ณธ nums ๊ฐ’๋„ ํ•จ๊ป˜ ๋ณ€๊ฒฝ๋œ๋‹ค.

  • ์žฅ์  : ๋ณต์‚ฌํ•˜์ง€ ์•Š๊ณ  ์ง์ ‘ ์ฐธ์กฐ๋ฅผ ํ•˜๊ธฐ์— ๋น ๋ฅด๋‹ค.

  • ๋‹จ์  : ์ง์ ‘ ์ฐธ์กฐ๋ฅผ ํ•˜๊ธฐ์— ์›๋ž˜ ๊ฐ’์ด ์˜ํ–ฅ์„ ๋ฐ›๋Š”๋‹ค.(๋ฆฌ์Šคํฌ)

์ฝœ ๋ฐ”์ด ๋ ˆํผ๋Ÿฐ์Šค ๋‹จ์ ์˜ ๋ณด์™„

๐Ÿ˜€์›๋ž˜ ๊ฐ’์ด ์˜ํ–ฅ์„ ๋ฐ›๋Š” ๋‹จ์ ์„ ๋ณด์™„ํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?

์ฝœ ๋ฐ”์ด ๋ ˆํผ๋Ÿฐ์Šค๋Š” ์ฐธ์กฐ ํƒ€์ž…์„ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ์ฐธ์กฐ ํƒ€์ž…์˜ ๊ฒฝ์šฐ ๊ฐ์ฒด์˜ ์ฐธ์กฐ ๊ฐ’์ด ๋ณต์‚ฌ๋˜๋ฏ€๋กœ, ๊ฐ์ฒด ๋‚ด๋ถ€์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋ฉด ์›๋ณธ ๊ฐ์ฒด๋„ ํ•จ๊ป˜ ๋ณ€๊ฒฝ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์šฐ๋ฆฌ๋Š” ์›๋ณธ์˜ ๋ถˆ๋ณ€์„ฑ์€ ์ง€ํ‚ฌ ์ˆ˜ ์—†๊ฒŒ๋œ๋‹ค. ๋ฐ์ดํ„ฐ์˜ ์›๋ณธ์ด ์ˆ˜์ •๋˜๋Š” ๋ถˆ์ƒ์‚ฌ๊ฐ€ ๋ฐœํ–‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์ด๋Ÿฌํ•œ ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?

์ด์ „์— ํฌ์ŠคํŒ…ํ•œ ์–•์€๋ณต์‚ฌ ๊นŠ์€๋ณต์‚ฌ์—์„œ ๊นŠ์€๋ณต์‚ฌ(deep copy)๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

๊นŠ์€๋ณต์‚ฌ(deep copy)๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•˜์˜€์„ ๋•Œ ์›๋ณธ์— ์˜ํ–ฅ์„ ์ฃผ์ง€์•Š๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค.

  • โ›”๋ฌผ๋ก  ๋‹จ์ ์˜ ๋ณด์™„์„ ์œ„ํ•ด ๋ณต์‚ฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์†Œ๋ชจํ•˜๊ฒŒ ๋˜๊ณ , ์†๋„์˜ ์žฅ์ ์„ ์žƒ์–ด๋ฒ„๋ฆด ์ˆ˜ ์žˆ๋‹ค.
    ๋”ฐ๋ผ์„œ ๊นŠ์€ ๋ณต์‚ฌ์™€ ์ฝœ๋ฐ”์ด๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ ์ ์ ˆํžˆ ์„ ํƒํ•ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค.

    ์›๋ณธ๋ณ€๊ฒฝ์„ ํ•ด์•ผํ•˜๋Š” ์ธ์ˆ˜๋ผ๋ฉด ๊ทธ๋Œ€๋กœ ๋™์ž‘์‹œํ‚ค๊ณ , ๋ถˆ๋ณ€์„ฑ์„ ์ง€์ผœ์•ผ ํ•œ๋‹ค๋ฉด ๊นŠ์€๋ณต์‚ฌ๋ฅผ ์ด์šฉํ•ด์„œ ๋™์ž‘์‹œํ‚ฌ ํ•„์š”์„ฑ์ด ์ƒ๊ธธ ๊ฒƒ์ด๋‹ค.

์•„๋ž˜๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋˜๋Š” ๋ณ€์ˆ˜์˜ ์ฐธ์กฐ ๊ฐ’์„ ํ•จ์ˆ˜ ๋‚ด๋ถ€๋กœ ์ „๋‹ฌํ•˜๋˜ ์ฐธ์กฐ ๊ฐ์ฒด๋ฅผ ๊นŠ์€๋ณต์‚ฌ๋ฅผ ํ†ตํ•ด ์ „๋‹ฌํ•˜์—ฌ, ์›๋ณธ์˜ ๋ถˆ๋ณ€์„ฑ์„ ์ง€ํ‚ค๋Š” ์˜ˆ์‹œ์ด๋‹ค.

function changeName(obj) {
  obj = JSON.parse(JSON.stringify(obj)); // ๊นŠ์€ ๋ณต์‚ฌ
  obj.name = 'John';
  return obj;
}

let person = { name: 'Jane', age: 25 };
let result = changeName(person);
console.log(result); // ์ถœ๋ ฅ: { name: 'John', age: 25 }
console.log(person); // ์ถœ๋ ฅ: { name: 'Jane', age: 25 } (์›๋ณธ ๊ฐ’์€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์Œ)

์ฐธ๊ณ ์ž๋ฃŒ(์ถœ์ฒ˜)
Velog -younoah ํฌ์ŠคํŒ… 'Call by value'์™€ 'Call by reference' (feet. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ)
Velog sinclebear ํฌ์ŠคํŒ… [Javascript] Call by Value vs. Call by Reference์™€ ์–•์€ ๋ณต์‚ฌ vs. ๊นŠ์€ ๋ณต์‚ฌ
Chat GPT ๊ฒ€์ƒ‰๊ฒฐ๊ณผ [์ฝœ ๋ฐ”์ด ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ์›๋ณธ์˜ ๋ถˆ๋ณ€์„ฑ์„ ์ง€ํ‚ค๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•]

profile
22๋…„ 12์›” ๊ฐœ๋ฐœ์„ ์‹œ์ž‘ํ•œ ์‹ ์ž… ๊ฐœ๋ฐœ์ž โ€˜๊ถŒํƒœํ˜•โ€™์ž…๋‹ˆ๋‹ค. ํฌ์ŠคํŒ… ํ•˜๋‚˜ํ•˜๋‚˜ ๋‚ด๊ฐ€ ๋‹ค์‹œ๋ณด๊ธฐ ์œ„ํ•ด ์“ฐ๋Š” ๊ฒƒ์ด์ง€๋งŒ, ๋‹ค๋ฅธ ๋ถ„๋“ค์—๊ฒŒ๋„ ๋„์›€์ด ๋˜์—ˆ์œผ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค. ๐Ÿ’ฏ์ปฌ๋ŸฌํฐํŠธ๊ฐ€ ์ž˜ ์•ˆ๋ณด์ด์‹ค ๊ฒฝ์šฐ ๐ŸŒ™๋‹คํฌ๋ชจ๋“œ๋ฅผ ์ด์šฉํ•ด์ฃผ์„ธ์š”.๐Ÿ˜€ ์ง€์ ๊ณผ ์ฐธ๊ฒฌ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. ๋งŽ์€ ๋Œ“๊ธ€ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€