From 0c34fa9c5b5d972cf942fe26b8a36e58bc1de367 Mon Sep 17 00:00:00 2001 From: fdai7514 Date: Mon, 6 Feb 2023 20:53:58 +0100 Subject: [PATCH] refactoring: change variable names, format code and attach some descriptive comments in calculatorFactorial.c --- src/calculatorFactorial.c | 13 ++++++++++--- src/calculatorFactorial.h | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/calculatorFactorial.c b/src/calculatorFactorial.c index a86dcea..fb1a228 100644 --- a/src/calculatorFactorial.c +++ b/src/calculatorFactorial.c @@ -1,6 +1,13 @@ #include "calculatorFactorial.h" -int calculatorFactorial(int x) +int calculatorFactorial(int num) //implement recursion. The function calls itself so many times, till the breaking condition is fulfilled. { - if (x==0)return 1; - else return x*calculatorFactorial(x-1); + if (num == 0) //breaking condition + { + return 1; + } + else + { + return num * calculatorFactorial(num - 1); //If its not breaking condition, then multiply the number with the same function implemented on the previous number. Eventually it will reach breaking condition. + } } + diff --git a/src/calculatorFactorial.h b/src/calculatorFactorial.h index d6f0383..5dbd323 100644 --- a/src/calculatorFactorial.h +++ b/src/calculatorFactorial.h @@ -3,6 +3,6 @@ #include #include -int calculatorFactorial(int x); +int calculatorFactorial(int num); #endif // CALCULATORFACTORIAL_H