Faken
Faken

Reputation: 11822

Passing variables to functions

A quick question:

When i pass a variable to a function, does the program make a copy of that variable to use in the function?

If it does and I knew that the function would only read the variable and never write to it, is it possible to pass a variable to the function without creating a copy of that variable or should I just leave that up to the compiler optimizations to do that automatically for me?

Upvotes: 2

Views: 2311

Answers (5)

user357823
user357823

Reputation:

In const reference (const &) case okay inside the function you can use the value of the actual variable, no copies are generated, however you can not modify the value of the original variable, for this purpose just use & or (*) in the function definition, the function calls are the following :

myFunction (x) or myFunction (&x)

hope it helps:)

Başak Bilgi

Upvotes: 0

Thomas Matthews
Thomas Matthews

Reputation: 57708

If the function receives variables by copy, yes the program does make a copy:

  void function(int pass_by_value);

If the function receives variables by reference, no, the program does not make a copy but uses the original variable:
void function2(int & pass_by_reference);

If the function does not modify the variable, the function should receive a constant reference:
void function3(const int & pass_by_constant_reference);

In general, classes and structures should be passed by constant reference. POD (plain old data) types can be passed by value.

In C++, you need to tell the compiler and users of your functions, how you want the variables passed and if they will be modified or not.

Upvotes: 1

Pieter
Pieter

Reputation: 2912

If you use references, only an alias is passed. If you don't, a copy will be made. It depends on what you need to do with the variables. If you need to use the parameters, but not change them, a const reference is the best solution (efficiency- and semantics-wise), except maybe for really small types.

Upvotes: 0

shoosh
shoosh

Reputation: 78934

Yes it does.
If you know that you can pass the argument as const& like so:

int function(const string& str) {}

For small types like int or float or char this doesn't worth the hassle since passing a reference is the same as passing an int. doing this makes sense only if you're passing big things like vectors or strings.

The compiler is most likely not going to do this optimization for you because in most cases it can't know that it's allowed to.

Upvotes: 3

GManNickG
GManNickG

Reputation: 503913

Yes, parameters passed by value are copied. However, you can also pass variables by reference. A reference is an alias, so this makes the parameter an alias to a variable, rather than a copy. For example:

void foo(int x) {}
void bar(int& x) {}

int i;

foo(i); // copies i, foo works with a copy
bar(i); // aliases i, bar works directly on i

If you mark it as const, you have a read-only alias:

void baz(const int&);

baz(i); // aliases i, baz reads-only i

In general, always pass by const-reference. When you need to modify the alias, remove the const. And lastly, when you need a copy, just pass by value.*

* And as a good rule of thumb, fundamental types (int, char*, etc.) and types with sizeof(T) < sizeof(void*) should be passed by value instead of const-reference, because their size is small enough that copying will be faster than aliasing.

Upvotes: 6

Related Questions