0

Can anyone please tell me that why the functions parameters can not be static? Is this the reason that function parameters are declared on Stack and gets de-allocated when function return? There is no way to retain parameter values? Just confused. Please clarify.

Thanks.

Mustansar Saeed
  • 2,730
  • 2
  • 22
  • 46

1 Answers1

0

The keyword static can probably be seen as somewhat "overloaded".

The following usage-options are all viable:

  • Static local variables
  • Static global variables
  • Static member variables
  • Static global functions
  • Static member functions

Variables:

In terms of runtime, all types of static variables are essentially the same. They all reside in the data-section of the program, and their addresses remain constant throughout the execution of the program. So the only difference between them is during compilation, in the scope of declaration:

  • Static local variable: recognized by the compiler only in the scope of the function
  • Static global variable: recognized by the compiler only in the scope of the file
  • Static member variable: recognized by the compiler only in the scope of the class

Functions:

In terms of runtime, all types of functions (static and non-static) are essentially the same. They all reside in the code-section of the program, and their addresses remain constant throughout the execution of the program. So the only difference between them is during compilation, in the scope of declaration:

  • Static global function: recognized by the compiler only in the scope of the file
  • Static member function: recognized by the compiler only in the scope of the class

As to your question, arguments are passed to a function in the stack. There is no sense in having them static, because that would effectively place them in the data-section. And if they are located in the data-section, then the function can simply read them from there instead of having them passed to it in the stack.

barak manos
  • 29,648
  • 10
  • 62
  • 114
  • Running following code, void print(static int i) {cout << "Child:: print :: i = " << i << endl;} and passing different values from main, different value is being used in print. Its not picking value from data segment, and there is not any compilation error. Can we say that static in parameter list is not having any impact? – Mustansar Saeed Mar 20 '14 at 17:59
  • Are you sure that you're not getting any compiler-warning? I'm getting `warning C4042: 'i' : has bad storage class` on my environment. Perhaps it's a new feature in C++11, but certainly not something that I'm aware of... – barak manos Mar 20 '14 at 18:10
  • Yes I am getting warning, not the error. Conceptually, what should I say that what is static making point here? I can say that its not making sense putting static. Can I say that static keyword does not matter in function parameter list? – Mustansar Saeed Mar 20 '14 at 18:22
  • Sorry, but I don't understand what you mean by `static`. – barak manos Mar 20 '14 at 18:27
  • means adding `static` keyword in the function parameter as done in `void print(static int i) {cout << "Child:: print :: i = " << i << endl;}` – Mustansar Saeed Mar 20 '14 at 18:30
  • Well, I guess that since `static` is a storage class, the compiler does not consider this is an error. Still, the `bad storage class` warning does suggest that you should refrain from using `static` in this scope. – barak manos Mar 20 '14 at 18:31
  • Thanks barak for the explanation. – Mustansar Saeed Mar 20 '14 at 18:34