hello, can someone explain me how this printf inside the function works?

#include<stdio.h>
#define SIZE 10
void function(int [],int);
int main()
{
		int a[SIZE]={32,27,64,18,95,14,90,70,60,37};
		function(a,SIZE);
		return 0;
		}
		void function(int b[],int size)
		{
			if(size>0){
				function(&b[1],size-1);
				printf("%d  ",b[0]);
			}
			
		}

Recommended Answers

All 8 Replies

Can you be more specific about what confuses you?

it seems when program comes to function(&b[1],size-1) part
it calls the function again and then the same
how and when does the printf() work when the function is calling itself ?

Each time the function calls itself, it calls itself with a slice of the array. The first element is sliced off, which means b[1] becomes b[0] for the next call. Repeating that process while decrementing size until size is 0 produces the recursive equivalent of a reverse traversal loop:

for (int i = size - 1; i >= 0; i--)
    printf("%d  ",b[0]);

the function calls itself before reaching printf what I don't understand is how printf runs

The function returns too. What happens after that?

ok i understood , thanks for help.

Hey help me ..I can't understand....
how that printf works?

@Utsav,after calling function(b[1],0),the code completes all calling functions from function (b[1],1) to function(b[1],9) by writing printf part of if clause.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.