Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7858693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:33:48+00:00 2026-06-02T21:33:48+00:00

I’m really wondering, is pass by reference (or passing a pointer) ever the best

  • 0

I’m really wondering, is pass by reference (or passing a pointer) ever the “best” solution semantically wise? Especially for primitive types it feels like:

int x = 5;
x = foo(x);

is more natural than

int x = 5;
foo(x);

I first thought about code needing to return error codes, however then I realised using exception mechanisms is always(?) preferable over this. Are there real reasons why one would wish to pass by reference? – Leaving speed out of the scope for now.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:33:51+00:00Added an answer on June 2, 2026 at 9:33 pm

    Arguably, for primitive types, it really is more natural to pass them by value. Just looking at mathematics, we all have an understanding of values and know what we mean by mapping values to other values.

    For example when we write sin(x), we really want our function sin to map the value (of) x to another value which we simply denote sin(x). Sometimes we want to give the mapped value a different name, so we could write something like y := sin(x) and use y instead of sin(x).

    Now if sin would take this parameter by-reference, sin(x) would mean, that the value of x would be mapped to sin(x) and implicitely stored in x again – so the old value of x would be lost!

    In many cases this would be really annoying because we may want to use the “old” x again – so we would have to copy its value beforehand.

    Now lets look at the other hand: OOP. There you want to have objects, like a car for example, and this object should be able to change. For example cars can move (if they have gas), therefore they can change their position; driving costs gas, so the gas tank will deplete over time; you can open the doors etc etc…

    Here we don’t think about mapping a car to another car (a copy) with its door opened or its gas tank slightly decreased, but we really think about the same car, but now with open doors.


    So again in short:

    • passing by value is good when we want the arguments to not lose its original meaning – its most useful for mapping values.

    • passing by reference is good when we want our value (which we here interpret as an object) to change.


    So semantically it is quite clear what you typically want to use pass by value/pass by reference for, but there are some problems one could have with passing by value:

    If your object is a resource, then passing that resource by value would (probably) mean, that you`d allocate a second resource! This is sometimes simply not possible – e.g. think about (TCP) network connections. When you already have one, its not possible to open another on the same port, sharing the context in the already open connection. So in such cases you really have no choice but pass your network connection (socket, data stream, whatever) by reference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.