Monday, December 10, 2018

Structure


Structure is a data type that could store various data type.

The difference between structure and array is array can only store the same data type as the array data type while structure could store various data type.

Syntax

struct struct_name
{
    data_type1 var_name1;
    data_type2 var_name2;
    ...
};

Two ways to declare the struct variable :
1.

struct struct_name
{
    data_type1 var_name1;
    data_type2 var_name2;
    ...
} struct_variable;

2.

struct struct_name
{
    data_type1 var_name1;
    data_type2 var_name2;
    ...
};
struct struct_name struct_variable;



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