Could Someone tell me whats the diffrence between call-by-value with call-by-refference?

In general, passing by value means that a copy of variable is made with the same value and passing by reference means the variable object itself is passed. You can change the value all you want with pass by value, but the original object is not changed. With pass by reference any changes to the passed variable also change the original. C does not support passing by reference, but you can fake it with pointers:

#include <stdio.h>

void CallByValue(int x)
{
    x = 123;
}

void CallByFakeReference(int* x)
{
    *x = 123;
}

int main()
{
    int x = 0;

    printf("Before CallByValue: %d\n", x);
    CallByValue(x);
    printf("After CallByValue: %d\n\n", x);

    printf("Before CallByFakeReference: %d\n", x);
    CallByFakeReference(&x);
    printf("After CallByFakeReference: %d\n", x);

    return 0;
}

Faking call by reference is done by passing the address of an object and then accessing the value through indirection. That way you can get to the original object and make changes, but the address is still passed by value.

commented: I appreciate you for taking the time to answer this question in a more correct way than I did :P +20

Hard time using a search engine?

come on, man. that's a legitimate question to ask.

A side note : one of the main reason call by reference was created is
because passing a class object by value is expensive.

You can think of Pass by reference like a pointer in disguise.

What is the purpose of using call by value and call by reference?

What is the purpose of using call by value and call by reference?

Your question revealed that perhaps you are not understanding what it has been explained so far.
There's not such a thing as call by reference in The C language. Everything done is done by coping values around.
It is like a World where credit hasn't been invented. Anything you buy you need to pay with cash. The bigger the purchase, the more cash you have to carry around, the more cash you carry the more effort you have to make.
Pointers are like checks. Now you don't carry money around, but rather the small weight of a check book. And with a simple check you can provide the value of a much heavy and cumbersome pile of cash.

On the other hand. Call by reference in some other languages is like paying with Credit Cards. ;)

commented: Nice explanation :) +20

What is the purpose of using call by value and call by reference?

Again :

A side note : one of the main reason call by reference was created is
because passing a class object by value is expensive.

You can think of Pass by reference like a pointer in disguise.

Pass by reference is a pointer under the hood.

Pass by reference is a pointer under the hood.

There is no hood to look under. C does not have pass by reference, only pass by value. Pass by reference is faked in C by passing pointers by value and using indirection to get to the object.

A side note : one of the main reason call by reference was created is
because passing a class object by value is expensive.

You can think of Pass by reference like a pointer in disguise.

I think you are talking about C++, but even in C++, the main reason references are part of the language is to support operator overloading. They are also a convenient way to make output parameters, but that is a side effect of the original goal.

thanx

The main reason to use pass by reference is to be able to access the original object so that it will still be modified when you leave the function. Right? (Not the best wording, but hopefully what I mean is clear). I see other statements in here that don't make sense to me.

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.