Kamis, 06 Desember 2018

Function and Recursion

Hello Everyone welcome to my Blog !!!
Welcome back to CodingJP !!!
Today I am going to teach you about...

Function and Recursion !!!

Function
So what is Function ??

Function is a self-contained block of statements that can be executed repeatedly whenever we need it, which perform specific task. Function also called sub-task.

The benefit of using Function in C is :
   1. The function provides modularity.
   2. The function provides reusable code.
   3. large programs, debugging and editing tasks is easy with the use of functions.
   4. The program can be modularized into smaller parts.
   5. Separate function independently can be developed according to the needs.

There are 2 types of Function :
   1. Built-in (Library) Function
        These function are provided by the system and stored in library / a standard function   provided by C compiler.

       Example :
         - strcpy()
         - printf()
         - strlen()

   2. User define Function
         These are function which are defined and declared by the programmer to do some specific task. These are designed to re-use the code.

Function Construction

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)

Example :

#include <stdio.h>


int maximum (int x, int y){

      int max = x;

      if ( y > max)   max = y;

      return max

}


void main () {

     int a,b;

     printf("Input 2 even values : ");

     scanf("%d %d", &a, &b);

     printf("Largest value : %d\n",maximum(a,b));

}

Recursion
What is Recursion ??

Recursion is a function call inside a certain function calling itself / the process of calling a function by itself is called recursion and the function which calls itself is called recursive function.

There are two types of Recursion :
  1. Base case :
      return value(constant) without calling next recursive call.

  2. Reduction step
      sequence of input value converging to the base case.

Example:
  - Base case : n = 0
  - Reduction step : f(n) = n * f(n-1)

Recursion usually used in Fibonacci Number for example in this coding :

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;
}

So that's all about Function and Recursion... How was it?? did you reader get it??
For the simple is... 
- Function is formed through grouping some statements to do a particular job.
- Recursion is a function call inside a certain function calling itself.

Easy right...?? 
So.. Thank you for reading this CodingJP Blog... I hope you like it :)





Source :

- https://www.quora.com/What-is-the-difference-between-function-and-recursion-in-C

Tidak ada komentar:

Posting Komentar