-4

What's the difference between passing an argument inside a function as a parameter and declaring the argument as a variable inside the function declaration block in function definition?

Example: Make use of my comments to get the gist of the question.

#include<iostream>
         int max=0;
         int t=0;
    class stack
    {
         int s[10];
    public:
         void push(int);
         void pop();
    };
    void stack::push(int y) //argument passed inside function parameter
    {
          if(t<=max);
          {
               s[t]=y;
               t=t+1;
          }
          else
          cout<<"Stack overflow";
    }
    void stack::pop()
    {
         int item; //variable declared inside function definition
         if(t>=0)
         {
               t=t-1;
               item=s[t+1];
         }
    }
Amu
  • 1
  • 1
  • It's not clear to me what you are asking about. Could you give examples in code of the two cases you are referring to? – Bill Lynch Oct 11 '14 at 02:26
  • What´s the difference of "builing a coin slot of a vending machine which accepts any US dollar coin" and "throwing a coin in"... – deviantfan Oct 11 '14 at 02:29
  • Are you talking about K&R parameter declaration style? Then why is the question tagged C++? C++ has never accepted that style. – Ben Voigt Oct 11 '14 at 02:57
  • Based on the edit, no you aren't... but why are you talking about "declaring the argument as a variable inside the function"? If it is declared local to the function, it is not an argument at all. – Ben Voigt Oct 11 '14 at 02:59
  • (Besides that, this code snippet has more bugs than the movie "Starship Troopers") – Ben Voigt Oct 11 '14 at 03:00
  • Using `#include ` is very ancient C++; it has been `#include ` since 1998. – Jonathan Leffler Oct 11 '14 at 03:03
  • Thanks Ben for the keenness but i still want to know why i should pass arguments(as variables) when i can declare the variables locally in the function...Is there any difference? If not, which is the best practice to use. Your assist is highly valued – Amu Oct 11 '14 at 03:23

3 Answers3

0

One difference is in how arrays are interpreted:

// returns nonzero iff array1 equals {1, 2, 3}
int func(int array1[], size_t size1)
{
    int array2[] = {1, 2, 3};
    return size1 == sizeof(array2) && memcmp(array1, array2, size1) == 0;
}

While array2 is an array of 3 integers, array1 is an array of unknown size, which is why we usually pass a second parameter for the size. This is because of arrays "decay to pointers" when passed into functions like this. You can read about it here: What is array decaying?

There are fancier techniques for dealing with this in C++ using value templates for the array size, but the above is true of C and most C++ code in the wild too.

Other than arrays, C types used in function parameters behave pretty much the same as C types used in local variables.

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

One difference is that parameters are initialized by the caller but local variables have to be initialized by the function.

int somefunc(int arg)
{
    int local = 0;
    …
    return local + arg;
}

When the function is called:

int x = somefunc(23);

the variable arg in the function is initialized with the value 23 by the calling code. However, the local variable local has to be explicitly initialized (in this case with the = 0; if it was a class type, by an appropriate constructor). Local variables of built-in types such as int that are not explicitly initialized get a quasi-random value.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Thank you. Now that is the way to shoulder a newbie. And am correcting the **iostream header too – Amu Oct 11 '14 at 03:20
0

The arguments is passed by calling that function. so caller decides what should be passed. functions decides what should it accept. for example:

    main()
    {
        int i=0;
        int k=i;
        for (int j=i; j `enter code here`< n; j++)
        {
        }
    }

is same as
    main()
    {
       int i=0,k;
       
        for (int j=k=i; j < n; j++)
        {
        }
   }
 but this
   main()
    {
       int i=0,k;
       
        for (int j=i; j < n; j++)
        {
           k=i;
        }
   }
is totally different.