Primrose Docs
  • Home
    • ⛓️Blockchain
      • Avalanche
        • What is AVAX?
      • Ethereum
        • Ethereum Cancun Upgrade Explained(draft)
        • go-ethereum: gas estimate
        • Blockchain Transaction Lifecycle
        • Mempool
        • Gas optimization in Solidity, Ethereum
      • Solidity DeepDive
        • Meta transaction
        • solidity: patterns
        • UUPS vs Transparent
        • Solidity Interface
        • Smart contract storage
        • ERC-2981 Contract
        • Solidity modifier
        • Solidity delete keyword
        • How To Make NFTs with On-Chain Metadata - Hardhat and JavaScript
        • How to Build "Buy Me a Coffee" DeFi dapp
        • How to Develop an NFT Smart Contract (ERC 721) with Alchemy
        • Upgradeable Contract
        • Smart Contract Verification
      • Common
        • Eigenlayer
        • MultiSig(draft)
        • Chain-Based Proof-of- Stake, BFT-Style Proof-of-Stake
        • Byzantine Fault Tolerance
        • Zero-knowledge
        • Hierarchical Deterministic Wallet
        • Maker DAO
        • Defi
        • Uniswap
        • IBC
        • Cosmos
        • Gossip Protocol
        • Tendermint
        • UTXO vs Account
        • Blockchain Layer
        • Consensus Algorithm
        • How does mining work?
        • Immutable Ledger
        • SHA256 Hash
        • Filecoin
        • IPFS - InterPlanetary File System
        • IPFS와 파일코인
        • Livepeer
        • Layer 0
      • Bitcoin
        • BIP for HD Wallet
        • P2WPKH
        • Segwit vs Native Segwit
    • 📖Languages
      • Javascript/Typescript
        • Hoisting
        • This value in Javascript
        • Execution Context
        • About Javscript
        • tsconfig.json
        • Nest js Provider
        • 'return await promise' vs 'return promise'
      • Python
        • Pythonic
        • Python: Iterable, Iterator
        • Uvicorn & Gunicorn
        • WSGI, ASGI
        • Python docstring
        • Decorator in Python
        • Namespace in Python
        • Python Method
      • Go
        • GORM+MySQL Connection Pool
        • Context in golang
        • How to sign Ethereum EIP-1559 transactions using AWS KMS
        • Mongo DB in golang(draft)
        • Golang HTTP Package
        • Panic
        • Golang new/make
        • golang container package
        • errgroup in golang
        • Generic Programming in Golang
        • Goroutine(draft)
    • 📝Database
      • MongoDB in golang
      • Nested loop join, Hash join
      • DB Query plan
      • Index
      • Optimistic Lock Pessimistic Lock
    • 💻Computer Science
      • N+1 query in go
      • Web server 를 구성할 때 Thread, Process 개수를 어떻게 정할 것인가?
      • CAP
      • Socket programming
      • DNS, IP
      • URL, URI
      • TLS과 SSL
      • Caching(draft)
      • Building Microservices: Micro Service 5 Deploy Principle
      • Red Black Tree
      • AOP
      • Distributed Lock
      • VPC
      • Docker
      • All about Session and JWT
      • Closure
      • Singleton Pattern
      • TCP 3 way handshake & 4 way handshake
      • Race Condition
      • Process Address Space 
      • Call by value, Call by reference, Call by assignment
      • Zookeeper, ETCD
      • URL Shortening
      • Raft consensus
      • Sharding, Partitioning
    • 📒ETC
      • K8S SIGTERM
      • SQS
      • Git Branch Strategy: Ship / Show / Ask
      • Kafka
      • Redis Data Types
      • CI/CD
      • How does Google design APIs?
      • Minishell (42 cursus)
      • Coroutine & Subroutine
      • Redis
Powered by GitBook
On this page
  • Call by value , Call by reference, Call by assignment
  • Call by value
  • Call by reference
  • Call by assignment
  1. Home
  2. Computer Science

Call by value, Call by reference, Call by assignment

Call by value , Call by reference, Call by assignment

함수를 호출할 때에, 우리는 값에 의한 호출을 하기도하고 참조에 의한 호출을 하기도 한다.

함수가 호출될 때 메모리 공간 안에서는 함수를 위한 공간이 생성된다. 그리고 함수가 종료될 때, 해당 공간은 사라진다.

Stack Frame : 함수 호출시 할당되는 메모리 블록(지역변수의 선언으로 할당되는 메모리 블록)

int    main(void)
{
    // 스택변수 a는 Stack Frame에 지역변수로서 할당이 된다.
    int    a;

    a = 4;
    return (0);
}

Call by value

Call-by-value는 말 그대로 값에 의한 호출이다. Call-by-value는 인자로 받은 값을 복사하여 처리를 하는데,

복사된 인자는 함수 안에서 지역적으로 사용되는 local value(지역변수)가 된다.

#include <stdio.h>

int    add(int a, int b)
{
    printf("a_ptr = %p\n", &a);
    printf("b_ptr = %p\n", &b);
    return (a + b);
}

int    main()
{
    int    a = 4;
    int    b = 6;
    int    c = add(a, b);

    printf("a_ptr = %p\n", &a);
    printf("b_ptr = %p\n", &b);
    return (0);
}

// 주소값이 다르다.
/***************************
a_ptr = 0x16b76322c
b_ptr = 0x16b763228
a_ptr = 0x16b763268
b_ptr = 0x16b763264
***************************/

Call by reference

Call-by-reference는 참조에 의한 호출이다. 변수의 레퍼런스(주소)를 인자로 전달해서

인자로 전달된 변수의 값도 함께 변경할 수 있다.

프로그래밍 구조상 Call-by-value에 비해 메모리적인 우위가 있다고는 하지만, 요즘에는 기기의 성능이 다 좋아서 큰 상관은 없다.

그러나 많은 연산이 들어갔을 때에는 Call-by-value는 과부하의 원인이 될 수 있다.

반대로 Call-by-value는 원래 변수를 터치하지 않고 복사하기 때문에 불변성을 유지할 수 있다는 장점이 있다.


Call by assignment

Python의 경우 함수의 호출 방식으로 call-by-assignment를 사용한다.

모든 것을 객체로 취급하는 Python에서는 객체를 크게 두 종류로 구분지을 수 있다.

  1. Immutable : int, float, str, tuple

  2. Muttable : list, dict, set

Immutable 객체가 인자로 전달되면 처음에는 call by reference로 받지만, 값을 변경시키면 call by value로 동작한다.즉 함수 내에서 formal parameter 값이 바뀌어도 actual parameter는 불변성을 유지한다.

반대로 Muttable 객체는 처음부터 call by reference로 동작한다.

PreviousProcess Address Space NextZookeeper, ETCD

Last updated 1 year ago

💻