MY mENU


Wednesday, 15 February 2012

If Statement in C


if statements:
If statement is a conditional branching statement. In conditional branching statement a condition is evaluated, if it is evaluate true a group of statement is executed. The simple format of an if statement is as follows:
if (expression) 
        statement;
  If the expression is evaluated and found to be true, the single statement following the "if" is executed. If false, the following statement is skipped. Here a compound statement composed of several statements bounded by braces can replace the single statement.

if else statement:   This feature permits the programmer to write a single comparison, and then execute one of the two statements depending upon whether the test expression is true or false. The general form of the if-else statement is


if(expression)
          statement1
else
          statement2

                    Here also expression in parentheses must evaluate to (a boolean) true or false.
You can set up an if-else statement to test for multiple conditions. if-else construct, the  if, else if, else. This construct is useful where two or more alternatives are available for selection.


The syntax is
if(condition)
          statement 1;
else if (condition)
          statement 2;
          .....................
          .....................
else if(condition)
         statement n-1;
else
          statemens n ;


PassByValue and PassBy Reference in C functions


 Arguments can be passed to a function by two methods, They are

1.pass by value
2.pass by reference


Pass by Value:
                 Function in C passes all arguments by value. When a single value is passed to a function via an actual argument, the value of the actual argument is copied into the function.
 Therefore, the value of the corresponding formal argument can be altered within the function, but the value of the actual argument within the calling routine will not change. This procedure for passing the value of an argument to a function is known as passing by value.

Example:

Pass by Value



Pass by Reference:

                When passing by reference technique is used, the address of the data item is passed to the called function. Using & operator we can determine the address of the data item. Note that function once receives a data item by reference, it acts on data item and the changes made to the data item also reflects on the calling function. Here you don't need to return anything to calling function.

Example:

Pass by Reference


Recursion:

 Recursive functions are those functions, which call itself within that function. A recursive function must have the following type of statements.

1.A statement to test and determine whether the function is calling itself again.

2.A statement that calls the function itself and must be argument.

3.A conditional statement (if-else)

4.A return statement.

Example: Factorial of a number

             This is the most famous program on recursion. Many versions of this program are available.
All programs differ only in checking conditions. I prefer to write like the following one.

Recursion




Functions in C


Functions:

A number of statements grouped into a single logical unit are called a function. The use of function makes programming easier since repeated statements can be grouped into functions. Splitting the program into separate function make the program more readable and maintainable.

It is necessary to have a single function ‘main’ in every C program, along with other functions used/defined by the programmer.

A function definition has two principal components: the function header and body of the function. The function header is the data type of return value followed by function name and (optionally) a set of arguments separated by commas and enclosed in parenthesis. Associated type to which function accepts precedes each argument. In general terms function header statement can be written as

return_type function_name (type1 arg1,type2 arg2,..,typen argn)

where return_type represents the data type of the item that is returned by the function, function_name represents the name of the function, and type1,type2,…,typen represents the data type of the arguments arg1,arg2,..,argn.Here is a very simple function, which accepts one argument, multiplies it by 2, and hands that value back:

int add(int p,int q)
{
return p+q; //Body of the function
}

Here p and q are arguments. The arguments are called formal arguments or formal parameters, because they
 represent the name of the data item that is transferred into the function from the calling portion of the program. The corresponding arguments in the function call are called actual arguments or actual parameters, since they define the data items that are actually transferred.

A function can be invoked whenever it is needed. It can be accessed by specifying its name followed by a list of arguments enclosed in parenthesis and separated by commas.
e.g., add(5,10);
The following condition must be satisfied for function call.

1.The number of arguments in the function calls and function declaration must be same.

2.The prototype of each of the argument in the function call should be same as the corresponding parameter in the function declaration statement.


 A function may or may not return a value. A ‘return’ statement returns some value to the calling function and it may be assigned to the variable in the left side of the calling function. The return value can be a constant, variable, a user defined data structure, a general expression, a pointer to function, or a function call. The return statement also causes the program logic to return to the point from which the function was accessed. In general term the return statement is written as


return expression;


                  If a function does not return a value, the return type in the function definition and declaration is specified as void. The declaration for a prototype for a function that receive any arguments from the calling function and does not return any value will have the format
                                                void function_name (void);