Skip to main content
GameDev.net gamedev.net
🔒 Locked

simple C++ question

Started by gamer_aster Jan 11, 2005 at 10:10 AM 1 replies 600+ views
Original Post
gamer_aster
gamer_aster
What's the main difference between the mere function and referencing function below? int ar[5]; int funcname(int n) { //some codes here; return ar[n]; } int& funcname(int n) { //some codes again; return ar[n]; } thx for replies in advance:)
DrEvil
DrEvil
The first example returns a copy of the int, also known as "return by value"

In the 2nd example, by returning a reference, the caller can modify the int, which will change the original int in the array. You can prevent changing by returning a const int &

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.