Hello Programmers! I am making a program that demonstrates the difference between passing by value to a function and passing by reference. My code is below:

#include <iostream>
using namespace std;

int squareByValue(int);
void squareByReference(int&);


int main()
{
    int x=2;
    int z=4;

    cout << "x= "<<x<< " before squareByValue"<<endl;
    cout << "Value returned by squareByvalue: " << squareByValue(x) << endl;
    cout << "x= " << x << " after squareByValue\n"<<endl;

    cout << "z= " << z << " before squareByReference"<<endl;
    squareByReference(z);
    cout << "z= " << z << " after squareByReference" <<endl;

    return 0;
}

int squareByValue(int num)
{
    return num*=num;
}

void squarebyReference(int& numRef)
{
    numRef*=numRef;
}

However, the code won't compile. I get the following two messages from my xCode compiler:

Undefined symbols for architecture x86_64:
  "squareByReference(int&)", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

And this:

Undefined symbols for architecture x86_64:
  "squareByReference(int&)", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Could any of you offer any advice as to what is the problem here?

Recommended Answers

All 2 Replies

You've used different names for the function in the declaration and definition.

void squareByReference(int&);
void squarebyReference(int& numRef)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.