Wednesday, November 28, 2018

Function and Recursion


 Function and Recursion

There is 2 Diffrent Function:
-Library  Function
            Such as strcpy() in string.h, sqrt() in math.h, printf() in stdio.h

-User Defined Function

In order to create your own function you need to use this construction of function

           return-value-type  function-name( parameter-list )
           {
   statements;
           }

return-value-type:  data type of the value returned
      If not filled, then default data type will be used (default integer)
      If return-value-type is void then the function will not return value

Parameter-list: list of value sent from the function initiator (user)


Recursion Definition:
a function call inside a certain function calling itself

Recursive Function has two components:
     Base case:
            return value(constant) without calling next recursive call.
     Reduction step:
            sequence of input value converging to the base case.


Example:

Fibonacci Number
sequence: 0, 1, 1, 2, 3, 5, 8, 13 ...
Relation between the number define recursively as follows:
Fib(N) = N                                          if N = 0 or 1
Fib(N) = Fib(N-2) + Fib(N-1)            if N >= 2


int Fib(int n) {
   int f;
   if(n==0) f = 0;
      else if(n==1) f = 1;
         else f = Fib(n-2) + Fib(n-1);
   return f;
}
 
 
 Hope you understand with this thing if you have any question gladly ask at the comment section below

Albert TG/2201736385
LB04
Binusian 2022


No comments:

Post a Comment