Quantcast
Channel: Recursive a+b function - Code Review Stack Exchange
Viewing all articles
Browse latest Browse all 3

Recursive a+b function

$
0
0

I am practising recursion and have written the small code for summing a+b as below:

#include <stdio.h>int b=6;static int cnt;void main(void){    int a=9,sum;    sum=succ(a);    printf("Sum returned : %d\n",sum);}int succ(x){    cnt++;    return(cnt<=b ? succ(x+1) : x++);}

I am getting the results fine using this. Can anyone suggest optimization or better code for this? I also want to understand how stacking happens in this case for variables cnt and x. Are they stacking in pairs like 0-9, 1-10 ,2-11,3-12 ,4-13, then at last when cnt is 5 it return x from the post-increment return?


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images