-1

I am newbie. So please bear with me,

#include<stdio.h>

    int abc(int k)
    {
    k++;

    }
    int main()
    {
    int a=1;
    printf("%d",abc(a));

    return 0;
    }

Output of above program is : 1

My question is shouldn't the output should be '2' as the actual parameter is passing the value of '1' to the formal parameter and it has to be incremented by the function abc.

And when I change the function call to

printf("%d",abc(1));

The output is some garbage value.

How does parameter passing work here? Please explain.

Vanu Aparna
  • 105
  • 8
  • 1
    Always always compile with **warnings** enabled. `-Wall -Wextra` at a minimum. That would disclose `warning: control reaches end of non-void function` telling you that you are not returning anything from `abc`. – David C. Rankin Feb 27 '15 at 16:04
  • Your program is wrong, because the function `abc` should return an `int`, but it never returns. So it's only by chance that your program doesn't crash. To avoid this in the future, enable the compiler warnings, by using the `-Wall` flag (gcc/clang) or in the compiler options (Visual Studio). It will say something like "warning: some code path don't return a value", which will make you aware of your mistake in advance. – Giulio Franco Feb 27 '15 at 16:05
  • 1
    @DavidC.Rankin i think `-Wextra` might be too annoying for a beginner. When you use `-Wextra`, you sometimes need boilerplate to get a clean compilation (think of unused argument) – Giulio Franco Feb 27 '15 at 16:06
  • 1
    @GiulioFranco, it depends. There few are occasions where `-Wextra` requires anything more than making sure your comparison are correct. See: [**-Wextra how useful is it really?**](http://stackoverflow.com/questions/2888404/wextra-how-useful-is-it-really) What it does do is force you to (1) understand what the compiler is telling you and (2) write proper code. Beginner or expert, there is no excuse for sloppy code when the compiler can identify where the problems are and give you a descriptive enough warning to point you in the right direction to fix it. – David C. Rankin Feb 27 '15 at 16:16
  • @DavidC.Rankin If you're an expert you'll probably decide which warnings to disable on a per-warning basis. If you're a newbie, you just convinced me it's better to enable everything. – Giulio Franco Feb 27 '15 at 16:43
  • Can someone please answer about why abc(1) is printing garbage value? – Vanu Aparna Feb 27 '15 at 18:01

2 Answers2

5

The unexpected results you are getting are not resulting from the "parameter passing", but from the fact that the abc function does not return any value. You should use return k; statement to get the output you are expecting. But as for parameter passing, they are passed by value, i.e. the passed value is copied to a temporary location k (visible in the function only), and not modified outside of it.

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
0

The code example you have passes a by value. You could think of it as a copy of a. You code modified with comments:

#include<stdio.h>

int abc(int k)
{
    // k is a copy of a, it is not a, since k is a copy, it has the 
    // value of a at the point of the copy.  So, k is 1

    k++;  // k is now 2
    return k; // return the computed value to the caller and destroy k
}

int main()
{
    int a=1;

    // as previously written, without the return statement in abc()
    // this function returned nothing.  So, the compiler just arranges
    // for something to be used from the stack where the return would 
    // have placed 2.  (I'm not terribly familiar
    // with assembly and so I'm not sure which register it would use).
    // That's why you get non-nonsensical data, whatever is in memory is
    // what you get and without the return statement, there's nothing 
    // meaningful there.

    // Also, as I commented above, abc() takes a **copy** of a.  Thus,
    // the contents of a are unmodified.  See how the printf() is
    // changed.  What does it print? 
    printf("%d %d",abc(a), a);

    return 0;
}
Andrew Falanga
  • 2,274
  • 4
  • 26
  • 51