Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 07-11-2009
Member
 
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?
Reply With Quote
  #2  
Old 07-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
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).
Reply With Quote
  #3  
Old 07-11-2009
Member
 
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;
}
Reply With Quote
  #4  
Old 07-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
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)
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 08:37 PM.