Learn C - C Function Parameter






You can qualify a function parameter with the const keyword, which indicates that the function will treat the argument as a constant.

Typically you apply the const keyword to a parameter that is a pointer.

Here's an example of a function with a const parameter:

bool hello(const char* pmessage){
   ...
}

Functions with a Variable Number of Arguments

The following code shows how to declare a function prototype with variable length parameters.

double average(double v1, double v2, ...);

The ellipsis indicates that a variable number of arguments may follow the first two fixed arguments.

There must be at least one fixed parameter for Variable-Length Argument Lists.

To reference the arguments, use pointers.

You need to use three functions to handle a variable number of arguments.

They are called va_start(), va_arg(), and va_end().

You must call va_start() to initialize the value of the variable argument list pointer in your function. This pointer must be declared as type va_list.

va_start() has the following form:

void va_start(va_list parg, last_fixed_arg);

This function accepts two arguments: a pointer parg of type va_list, and the name of the last fixed parameter you specified for the function.

The va_list type is a type that is defined in stdarg.h.

The first argument to va_arg() is the variable parg you initialized through the call to va_start().

The second argument is a specification of the type of the argument you expect to find.

The function va_arg() returns the value of the current argument specified by parg, and this is stored in value.

It updates the pointer parg to point to the next argument in the list.

va_end() is to clean up.

It resets the parg pointer to NULL.


    #include <stdio.h> 
    #include <stdarg.h> 
      //from www. j  av a  2 s  .c  om
    double average(double v1 , double v2,...);       // Function prototype 
      
    int main(void) 
    { 
      double v1 = 1.5, v2 = 2.5; 
      int num1 = 6, num2 = 5; 
      long num3 = 2L, num4 = 12L; 
      
      printf("Average = %.2lf\n", average(v1, 3.5, v2, 4.5, 0.0)); 
      printf("Average = %.2lf\n", average(1.0, 2.0, 0.0)); 
      printf("Average = %.2lf\n", average( (double)num2, v2,(double)num1, (double)num4,(double)num3, 0.0)); 
      return 0; 
    } 
      
    // Function to calculate the average of two or more arguments 
    double average( double v1, double v2,...) { 
      va_list parg;                      // Pointer for variable argument list 
      double sum = v1 + v2;              // Accumulate sum of the arguments 
      double value = 0.0;                // Argument value 
      int count = 2;                     // Count of number of arguments 
      
      va_start(parg,v2);                 // Initialize argument pointer 
      while((value = va_arg(parg, double)) != 0.0) { 
        sum += value; 
        ++count; 
      } 
      va_end(parg);                                  // End variable argument process 
      return sum/count; 
    } 

The code above generates the following result.





The main() Function

The main() function is where execution starts.

main() can have a parameter list so that you can pass arguments to main() when you execute a program from the command line.

You can write the main() function either with no parameters or with two parameters.

When you write main() with parameters, the first parameter is of type int and stores a count arguments including the name of the program itself.

The second parameter to main() is an array of pointers to strings.


#include <stdio.h> 
int main(int argc, char *argv[]) 
{ 
  printf("Program name: %s\n", argv[0]); 
  for(int i = 1 ; i<argc ; ++i) 
    printf("Argument %d: %s\n", i, argv[i]); 
  return 0; 
} 

The code above generates the following result.