Monday, December 10, 2018

Function and Recursion

Important things from the Function and Recursion material :

Function and Recursion are related to modular programming.
Modules, also called sub-program, is a block of statement that is used to do a certain task.

There are two types of function in C:
1. Library function
    - provided by the compiler. Example : strlen(), strcpy()
2. User defined function
    - does not provided by the compiler. It is made by the programmer during the coding.

Function

return_type function_name(parameter)
{
     statement;
}

Recursive is a function that returns or call the function itself.
Recursive function must have a base case and a reduction step so infinite loop is not made.

Example of recursive function :
==Count power of  a number==

int power (int base, int exponent)
{
     if(exponent==0 || exponent ==1) return b;
     return a*power(base, exponent-1);
}


No comments:

Post a Comment