FUNCTIONS


The functions are collection of statements can be grouped under a single name, to perform a specified task. In shortly some time the function are calling in sub-program. functions can take some arguments(values) from calling function. main function, program can call the functions any number of times. Function can return a values to main program(main()). The main() is the first function, called by system automatically.when we execute a program so, all 'c' programs, should contain atleast one function that is main(). when we want to write a functions, we have to write three statements.
1. Function declaration:
To mension the function return type and no.of arguments will passed from main function into sub-function.
2. Function definition:
Write the body of the functions.
3. Function call:
                        Call the function, in the main() or any other functions.

Syntax for function declaration:-
<return-type> function-name(argument list);
Syntax for function definition;-
<return-type> <function-name>(arguments)
                        {
                                    body of the statements
                                    return(<value>)
                        }
syntax for function call:-
                        var=function-name(arguments);

                        the main advantage of function is code reusablity. In big
            applications there may be some application in which a set of codings
            for a spesific task to be repeated again and again.

                        instead of writting the same coding in different places we
            can create a function containing the codings to active some task and
            the function can be called many times.

                        when ever we call the functions, the execution control will
            be transfered from calling program(main function) into called(sub
            function) program and after executing the function, control will
            come back to calling program. The place where it called.

            local variables:-
            ----------------
                        the local variables means nothing but, which are defining
            within the function. The scope of that variable is available in that
            function or program only.

                        whatever we are declaring in-side of the function or program
            that all variables are called local variables.that variables we can
            not able to access in another function or program, because of that
            variables are alive untill that function alive.

            global variables:-
            ------------------
                        the global variables means nothing but, the scope of that
            variable is available in that entire program.

                        we can able to access that variable in our total entire
            program.

                        the global variables declaration is begining of the program.
            this type of variables can accessed in any functions.

            function calling procedure:-
            ---------------------------
                        the functction calling procedure is classified into two type.
                        there are
                                    1.callby value or name.
                                    2.callby reference.
            callby value:-
            --------------

                        by using this method we can able to send the variable or value
                        only as an argument of any function. After that what ever we
                        did in our function on that variable we won't be get the
                        afected value.
                        syn:
                        ----
                                    function-name(argument...);

                        receiving end:-
                                    function-name(argument)
                                    {
                                                ...
                                    }
                        ex:-
                                    function-name(argument or value);

                                    add(a,12)


            Callby Reference:-
            ------------------
                        By Using this method, we can able to pass the variable's
                        address. The Receiving end should access that argument as a
                        pointer variable.
                        Syn:-
                                    function-name(&argument...);
                        Reciving End:-
                                    function-name(*argument...)
                                    {
                                                .....
                                    }
                        Ex:-
                                    add(&a);          -->       add(*a);

            return statement:
            -----------------

                        If you need to return a value from sub-function into main()
            By using the return statement we can able to return the value.
                        By using that statement at a time only one value can able to
            pass from sub into main.More than one value we won't be pass.
                        We can able to pass the argument of return statement as a value
            or variable or any Expression or any Boolean value.
            Ex:
                        return;              -->       Control will return
                        return(a);         -->     Value of Variable a Will return
                        return(10);       -->     Value 10 will return
                        return(1+b);     -->     Result of Expression 1+b will return.
                        return(a>b);     -->     Result of Expression a>b will return.
                                                            (Bollean value).

            Passing a Arrays an Argument:-
            ------------------------------

            1.      we can pass all elements of arrays by passing the array name
                        as argument, when we call the function.
            2.      We have to mension the function declaration with some array
                        name followed by a square brackets([]). (with out mensioning
                        the size of arrays.)
            3.      we can write function, after main function but, before main,
                        at the beggining of program, we have to declare the prototype
                        of function.

            Recursive Function:-
            ---------------------
                        Which function is called itself, that function is called as
                        a Recursive function.

            Storage Class:-
            ---------------
                        The Storage class decides the lifetime, initial value and
                        usage of variables.

                        The 'c' Language can hold four type.
                        1.         auto                 2.         Extern
                        3.         register            4.      static

            Variable Declaration
                                                <storage-class> <data-type> var-name;
            Ex:-
                                                auto int a;
                        By Default the all variables are auto variables only.

            1.auto:-
            --------
                        Automatic variables are declared inside a function in which
                        they are utilised.They are created when a function is
                        executed and destroyed when the function is terminated.

                        A variable inside a function without storage class
                        specification by default would be automatic.

                        auto int a;

            2.Register:-
            -----------
                        This storage class specifies or tells the complier that a
                        variable should be kept in one of the machines register
                        instead of keeping them in memory.

                        A value stored in CPU register can always be accessed faster
                        when compared to ordinary variables.(static,auto,extern)

                        register int a;
            3.External:-
            ------------
                        This storage class scope is global. This variable is equall
                        to global variable.
                        extern int a;
            4.static:-
            ----------
                        This storage class specifies that the value of static variable
                        will be retained untill the end of the program.(ie)
                        It will return the recent value for variable.The static
                        variables will store the value in same memory space
                         only.

                        static int a;

No comments: