Friday, 27 September 2019

What is IF statement in C/C++ ?


If Statement

If condition is a type of selection control structure. It is used to make decisions. It works on a given condition and evaluates the result if the given condition is true.

Syntax:
The general syntax of if statement as follow:

            If (condition)
            {
                        Statement 1;
                        Statement 2;
                        :
                        :
                        Statement N;
            }

            Example:
             There is an example to understand if statement.

            #include<conio.h>
            #include<iostrem.h>

            void main()
{
            Int marks=0;
            cout << “ Enter Marks: ” ;
            cin>> marks;
            if( marks > 40 )
            cout << “Congratulation, You have Passed” ;
            getch();
}

But there is a problem with if statement it evaluates only if the given condition is true otherwise no result will displayed.

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.

Wednesday, 11 April 2018

Passing Parameters Types in C/C++


Parameter passing is also divided to the following types:
1.      Pass by value.
2.      Pass by reference.
            Pass by value
In pass by value method the value of actual parameter is passed to the formal parameter in the function header at the time of function call.

The previous example uses “pass by value” method.

Pass by reference
In pass by reference method the address of the actual parameter is passed to the formal parameter in the function header at the time.
In this method the values of actual parameters can be changed by the Formal parameters.

Example:
The above example in “pass by value” is now “pass by reference”:

#include<conio.h>
#include<iostream.h>

void square_of_value(int&); à function declaration

void main()
{
clrsrc();
int value = 0;
cout<< “ Enter a value: “ ;
cin>> value;                                                                  
square_of_value( value );  à function call
getch();                                                                               
}

void square_of_value(int &num) à function header
{
cout << “square of the given number is: ”<< num ;
}

Passing Parameters of a function in C/C++


Parameters passing to function

Parameter passing to a function is a process of passing values from function call to the function header.
Example:

#include<conio.h>
#include<iostream.h>

void square_of_value(int); à function declaration

void main()
{
clrsrc();
int value = 0;
cout<< “ Enter a value: “ ;
cin>> value;                                                                   
square_of_value( value );  à function call
getch();                                                                               
}

void square_of_value(int num) à function header
{
cout << “square of the given number is: ”<< num ;
}

Actual parameter
Actual parameter is a parameter that is used in function call. It is passed to the function.

Formal parameter
A parameter used in function header is called as Formal parameter.