Arguments, Parameters

This page was translated by a robot.

The C and C++ languages ​​allow the programmer to specify input values ​​for a function. When a function is called, arguments are passed to a function, which are then available as parameters within the function.

Details

An argument designates a transfer value that is specified when a function is called. A parameter , on the other hand, designates a variable that stores the passed argument as an input value within the function. When a function is called, we speak of arguments, but within the called function we speak of parameters. Since both terms mean almost the same thing, they are more or less mixed up in common usage. ManderC tries to use these terms consistently.

In general, dealing with arguments and parameters is unproblematic: A function uses the parameter list to declare how many and what kind of parameters it can store. Accordingly, when the function is called, a list of arguments matching the function signature is specified, which the programmer would like to pass to the function. In C, a linguistic distinction is made between two methods of how arguments can be passed. A third method was added in C++.

Transfer by value

In C, the basic rule is that a function forms a self-contained unit that is independent of the rest of the code, which means that the parameters defined within the function do not change the arguments passed. When passing by value, the value of the argument is copied into the parameter of the function. The value of the original argument is unaffected, the parameter is discarded at the end of the function.



5
4
3
2
1




Liftoff!
#include <stdio.h>

void countdown(int count){
  while(count){
    printf("%d\n", count);
    count--;
  }
}

int main(){
  countdown(5);
  printf("Liftoff!\n");
  return 0;
}

Passing by value is particularly appropriate for the built-in base types. This method of passing is the only method that also works with rvalues . Caution is required in C++ when entire objects are passed by value: the compiler automatically calls a copy constructor at the beginning of the function and the destructor at the end of the function, which can be very expensive for frequent function calls, depending on the class.

For example, recursive programs can be programmed without problems using the transfer by value. The computer allocates new space on the stack for each parameter with each function call. At the end of the function, the stack is cleared again and the original arguments remain untouched when passed by value.

Transfer by pointer

It is not uncommon for a programmer to want a function to change certain arguments anyway. There are many reasons for this and often have to do with the programming style of each individual programmer. An important reason, however, is that functions are often required that return more like a value, but C and C++ allow many input values ​​for a function, but only one return value (return-value). So that passed arguments can be changed, they are passed using pointers.

When transferring by pointer, it is not the actual value that is transferred, but the pointer of the value. This provides the function with an address where changes take effect both within and outside the function. A pointer to the desired type is thus declared in the parameter list of the function. To pass a pointer to a value when calling the function, the desired argument is converted to a pointer using the address operator& . The transfer by pointer only works with lvalues ​​that are in memory.











12
#include <stdio.h>
#include <string.h>

void countchars(const char* string, int* count){
  *count = strlen(string);
}

int main(){
  int length;
  countchars("Hello World!", &length);
  printf("%d\n", length);
  return 0;
}

To access the desired value within the function, the pointer must be addressed using the dereference operator* , the pointer access operator-> , or the array element operator[] . A change in the dereferenced value within the called function also causes a change in the value in the area of ​​the function call. In addition to global variables under C, this is the only way to access a variable outside of a function.

It should be noted that on closer inspection, passing by pointer is nothing more than passing by value: the passed value is a pointer, which is copied into the function's parameter and which is discarded again at the end of the function . The actual value is only accessed by dereferencing the pointer. This dereferencing always causes a change outside of the function. However, it is not always desired that a function can change values ​​using a pointer argument. To prevent an argument that has a pointer type from being modified by a function, the parameter can be declared with the constqualifier . Further information can be obtained from theconst-safe programming can be read.

Arrays are usually passed as pointers. The passed pointer corresponds to a pointer to the first entry of the array. Using the array element operator [] , the array can then be addressed and changed within the function, which is always effective outside of the function. In the function declaration, arrays can also be written using an array declaration [] instead of using a pointer declaration * , but the compiler does not check whether the number of array entries is correct, since the array is always converted into a pointer. However, it should be noted that passing by reference (see below) behaves differently in this regard.

Under C++, passing by pointer is particularly suitable for objects that have many member variables, which, when passed by value, would always have to be copied and later deallocated, which can be very expensive. With the transfer via pointer, nothing else is copied apart from the address of the object, which has a positive effect on many function calls.