For starters it would be more correctly to declare the parameter as having type unsigned int
. Otherwise decreasing the parameter with a negative value can result in a very long output sequence.
In each recursive call of the function the parameter at first decreased and after decreasing it prints the output.
func(--x);
^^^^
printf("%d\t",x);
So if the function initially was called with the argument equal to 2
then it outputs value 1
because the argument was decreased.
Thus
func( 2 ) --> outputs 1
func( 1 ) --> outputs 0
func( 0 ) --> outputs nothing
Only the innermost function call outputs its value firstly.
If you want that the output would look like
0 1 2
then the function should be defined as it is shown in this demonstrative program
#include <stdio.h>
void func( unsigned int x )
{
if ( x ) func( x - 1 );
printf( "%u\t", x );
}
int main(void)
{
func( 2 );
putchar( '\n' );
}
The function output is
0 1 2
The function differs from the original function is that the argument itself is not decreased in the function call. It keeps its value unchanged.
if ( x ) func( x - 1 );
^^^^^^ the variable x itself is not changed
If the recursive call occurs at the beginning of a method, it is called a head recursion. The method saves the state before jumping into the next recursive call.