-1
struct rec {
    int length;
    int breath;
};

int area(rec objrec1) {
    objrec1.length ++;
    //objrec1.breath;
    //int result= objrec1.length++ * objrec1.breath;
    return(objrec1.length++ * objrec1.breath);
    //return(result);
}

int main() {
    rec objrec = { 10,5 }; // create object to access struct
    int arearesult;
    arearesult = area(objrec); // we pass the whole struct into the parameter
    cout << arearesult << endl;
}

my question is, if actual parameter is not making changes on the formal parameter, how is it that the increment of length (++) when returned to the main "arearesult" affected the outcome making the answer 55 instead of 50. because I'm thinking if when I incremented length by 1 making it 11,but when it gets to the main() it will become invalid and turn back to 10 since length in the actual and formal parameter are not the same. Thanks

underscore_d
  • 6,309
  • 3
  • 38
  • 64
Lionel
  • 11
  • 2
  • 4
    you pass `objrec` by value and you asign the return value to `areresult` and print its value, those are two different variables/objects – 463035818_is_not_an_ai Oct 15 '20 at 09:06
  • But `objrec` doesn't change value. – Galik Oct 15 '20 at 09:14
  • 1
    It's very unclear what "it" is that you believe will become "invalid" and "turn back". The function *is* modifying the argument, but the argument is a copy of `objrec`. It works exactly the same as if you passed an `int` directly. – molbdnilo Oct 15 '20 at 09:16
  • 2
    You might want to invest into a good [C++ Book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). It covers this and many, many other aspects of the language. – rustyx Oct 15 '20 at 09:18

2 Answers2

2

Your question can be boiled down to:

#include <iostream>

int foo ( int x ) { 
    x = 42;
    return x; 
}

int main {
    int a = 1;
    int b = 2;
    a = foo( b );
    std::cout << a << ' ' << b;
}

b is passed by value. It is not modified by calling foo, because foo modifies a copy of b. a is not passed to the function, but gets the returned value assigned. After that it has a different value than before.

The value of a in main is 42, because thats what was returned from foo. After foo returned there is no magic hidden dependency between a and b.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 1
    `x = 42; return x` might be clearer, since the OP seemed to think that saying `return x` meant the result would 'track' `x` such that it would revert to `2` outside `foo()`... or something. – underscore_d Oct 15 '20 at 09:13
1

I'm thinking if when I incremented length by 1 making it 11,but when it gets to the main() it will become invalid and turn back to 10

There is no single variable length. You have two independent objects, each with its own independent length.

We could rewrite your call as

int main() {
    rec objrec = { 10,5 }; // create object to access struct
    int arearesult;

    // manually inline this call:
    // arearesult = area(objrec); // we pass the whole struct into the parameter
    // int area(rec objrec1)
    {
        rec objrec1 = objrec;

        objrec1.length ++;

        arearesult = objrec1.length++ * objrec1.breath;

    } // objrec1 ceases existing here, objrec is unaffected

    cout << arearesult << endl;
}

and you can see that objrec is never touched inside the scope of the call at all, except to initialize objrec1. Changing objrec1.length does not affect objrec.length.

Useless
  • 64,155
  • 6
  • 88
  • 132