Results 1 to 4 of 4

Thread: void f(int val) vs void f(int & val)

  1. #1
    Join Date
    Jun 2009
    Posts
    59

    void f(int val) vs void f(int & val)

    I have recently started learning C and I have reached at a place where we are supposed to pass a variable using "call by reference" or "call by value". I was just thinking which is better to use if both are almost similar. In other words, in terms of performance of placing the basic types by reference, is it slower than passing them by value or that it comes at exactly the same?

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: void f(int val) vs void f(int & val)

    If what you want in case of use, it's void f (int val) i.e. passing by value will be more economical.

    But if you looking for the performance, from the time the error returns in a register of CPU (or more effectively) is "int" or "const int" (const allowing among other optimization level to ensure that the called party does not alter the saved value used for passing).

  3. #3
    Join Date
    Nov 2008
    Posts
    1,054

    Re: void f(int val) vs void f(int & val)

    GCC ignores const& on the atomic types. That is, the code generated for f (int) and f(int const& ) would give the same result.

    Here is an example to test what I say above:

    Code:
    #include <iostream>
    #include <ostream>
    int i = 0;
    void f(int const& p)
    {
        std::cout << "p = " << p << '\n';
        i = 42;
        std::cout << "p = " << p << '\n';
    }
    int main()
    {
        f(i);
        return 0;
    }

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: void f(int val) vs void f(int & val)

    I have trouble understanding why you want to compare by value and by reference that is not constant, the choice between them must be my opinions on semantic considerations.

    Between by value and by constant reference, if I were asked to make a choice - in other words, if the considerations of possibility of aliases does not impose one or the other - by value should generally be more efficient (but as always, 1. does not premature to consider this kind of thing, 2. measure 3. what is the gain worth)

Similar Threads

  1. Problem in void ***m
    By Luis-Fernando in forum Software Development
    Replies: 4
    Last Post: 09-02-2010, 08:14 PM
  2. Don't know about void pointer
    By Sarfaraj Khan in forum Software Development
    Replies: 5
    Last Post: 04-02-2010, 01:39 PM
  3. How to return value for void method?
    By Klaty in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 11:42 AM
  4. Does overclocking Cpu void Warranty
    By christheart in forum Overclocking & Computer Modification
    Replies: 2
    Last Post: 30-10-2009, 10:30 AM
  5. Error 'javascript:void(0)'
    By Akiraa in forum Software Development
    Replies: 3
    Last Post: 14-08-2009, 07:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,668,179.14105 seconds with 17 queries