Monday 18 June 2018

What is a function? How to declare a function and what is function definition ?


Function
A Function is a set of instructions which is used to perform a specific task. Every C++ program has at least one function which is main(). You can divide your program info different function depending on you.


            Syntax:
                        Return_type function_name(Parameters)
{
                        Statements of the function;
}

Example:
A simple example of the function is the “clrscr()” function which is used to clear the output window.
Function Declaration:
Function declaration means how to declare a function. A basic syntax to declare a
function is as follow:
Return_type function_name (data_type parameter1, …data_type parameterN);

You have to mention the return type, the name of the function, parameters if any and put a terminator “ ; ” at the end.
    If there is no parameter then function will:
            Return_type function_name();
            Example:
            int odd (int);
            Function is declared at the start of the main().
            Function declaration tells the compiler about the structure of the function.

            Function Definition:
            The instructions in the body of a function are collectively called as function definition. It              defines that what a function will do?
            A function definition can be written at
·         Before main()
·         After main()

What is operator precedence in C/C++ ?


It defines the way to evaluate an expression with multiple operators:

1. Unary [“Postfix”] à  A++, A--
2. Unary [“Prefix”] à ++A, --A
3. Arithmetic: Scaling à *, /, %
4. Arithmetic: Addition à +, -
5. Comparison operator à <, <=, >, >=
            6. Equality Operator à ==, !=
            7. AND à &&
            8. OR à ||
            9. Compound assignment operator. à =,+ =,- = etc.