Tuesday, 30 August 2016

FUNCTION OVERLOADING


We can create as many functions as we want but what will happen if they belong to the same class category, Let's say that there are two function printing some values but of different types.In this case we will be needing two functions. But we can make the functions with the same name but the condition is with the different number of Parameters or arguments. 
It will be more clear with the help of the program.

PROGRAM: 

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

void show(int a)      //Function 1
{
cout<<"This function contains Integer value = "<<a;
}


void show(float b)    //Function 2
{
cout<<"This function contains Float value = "<<b;
}

void main()
{
clrscr();

// Now we will Call the functions
show(2.3);      
show(10);
getch();
}  


OUTPUT:

This function contains Float value = 2.3
This function contains Integer value = 10

Now the first calling was 'show(2.3)'. This was a float value, so the compiler compared both the functions and their Return types and Parameters. When it found the float value, the function 2 was called then. Then the next call 'show(10)' was having an Integer value, so comparing with the parameters it executed the First Function. 

Also :


Saturday, 27 August 2016

Functions


Functions are the small parts or we can say sub-parts of the program that perform special tasks. They can be reused again when required.

Syntax: type function-name(parameters)

              {body of the function }

type means it's return type and if the function is not returning any value then its void. For Example if the function is returning an integer value then 'type'= 'int'. If no type is specified then Compiler assumes an integer value by default. So it is good to specify the Return type. Then in the parentheses is parameter_list. It can be leaved blank but it's up to the programmer whether he wants variables or not.



Program: 

#include<iostream,.h>
#include<conio.h>
void main()
{
clrscr();
void show();
{
 cout<<"Hello World";
}
getch();
}

Output:  Hello World

This will create a function 'show' inside the main. When the program will execute the compiler will go inside the function 'show' and will display the statement "Hello World" and will come out of the function and the program will terminate.

Function Prototype is the declaration of the function that tells the return type and the number of arguments and type of arguments that are going to be used in the program. If the function's definition is written first then there is no need for function prototype because function's definition is declaration itself( i.e. prototype of the function).


A function can be invoked in two manners:

1. Call by Value
2. Call by Reference


Call by Value

Call by value means calling the function by the matching of the number of arguments. The Call by Value copies the value of actual parameters(These are the parameters that appear in the function call statement or you can say in the 'main' function) into the formal parameters(These are in the function's definition). The function creates it's own copy of values and then uses it.


Program:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
void show(int);
int num;
cout<<"Enter a Number: ";
cin>>num;
cout<<"Original Number: "<<num<<"\n";
show(num);
cout<<"Number after change: "<<num<<"\n";
getch();
}

void show(int num)
{
num= 5;
cout<<"Value of Number in function: "<<num<<"\n";
}


Output: 

Enter a Number: 10
Original Number: 10
Value of Number in function: 5
Number after change: 10

'show' function copies the value from the actual and shows it in formal. So by call be value does not changes the original values(i.e. actual parameters). So even after calling the function 'show' there will be no change in actual.

For Understanding Better: 


Array Initialization


It is a simple method to initialize an array, whether One-Dimensional or Two Dimensional.
The values given in the brackets is separated by the comma. All the elements in the list will have the same base type because as you know an array consists of elements of  same type of elements whether float, int,etc.




Syntax: type array_name[size]={values_list};


Example:

int num[6]={23,56,12,81,10,1};
Now the values would be assigned like:
num[0] =23;
num[1] =56;
num[2] =12;
num[3] =81;
num[4] =10;
num[5] =1;

Two Dimensional arrays are also initialized in the same manner.

Syntax: type array_name[size][size]={values_list};


Example:

int matrix[3][3]={1,2,3,4,5,6,7,8,9};
Now the values would be assigned like:
matrix[0][0]=1;
matrix[0][1]=2;
matrix[0][2]=3;
matrix[1][0]=4;
matrix[1[1]=5;
matrix[1][2]=6;
matrix[2][0]=7;
matrix[2][1]=8;
matrix[2][2]=9;

It can be also be written as
int matrix[3][3]= { 1,2,3,
                               4,5,6,
                               7,8,9 };

C++ also allows you to skip the size of the array in initialization statement. The main advantage of not declaring the size is that we can increase or decrease the value_list without changing dimensions.

Example:      int square[]={2,3,4,5};

                     char name[]="User";
                                   


Friday, 26 August 2016

Single Dimensional Array


It is a simple form of array. It consists of type, name and its elements(subscripts or indices).

Syntax: type array-name[size];
It's type is also called the base type. It starts with 0 and goes till 'size-1' . Let's understand this with an example.


Program:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, marks[3];
for(i=0; i<3; i++)                /* i is taken zero because our array starts with zero
                                             so it will make us easy to understand it's
                                            elements position
                                          */
{
 cout<<"Enter your Marks in paper "<<i+1<<": ";
 cin>>marks[i];
}
cout<<"\nHere are the Marks of Student\n";
for(i=0; i<3; i++)
{
 cout<<"Paper "<<i+1<< "= "<<marks[i]<<"\n";
}
getch();
}


Output:

Enter your Marks in paper 1: 20
Enter your Marks in paper 2: 17
Enter your Marks in paper 3: 18

Here are the Marks of Student
Paper 1: 20
Paper 2: 17
Paper 3: 18

The above array takes the Marks from the Student. The first values goes at position 0, second stores at position 1 and last at position 2. Then in the next Loop values stored in the Array marks is displayed.

Thursday, 25 August 2016

ARRAYS


Array is having a large number of data of same type at a single place. The proper definition will be: An  Array is a collection of  variables of the same type that are referenced by a common(same) name.
It can be of any type : char, int, float or even user defined types.

Array are of different types:

1. One- dimensional arrays: have only one Row or column.

Syntax: type array_name[size];


2. Multi-dimensional arrays: It has both rows and columns. You can also say that having many single arrays in one array. The simplest example is Two-dimensional array but it can have more than two-dimensions.

Syntax: type array_name[rows][columns];

Nested Loops


A loop containing another loop in its body is a Nested Loop. In Nested Loop the inner loop terminate before the outer loop.

Program:

//Assuming that all the header files are included
.
.
.
for(i=1; i<5; i++)    //Considering that variable i and j are already created
{
cout<<"\n";
for(j=1; j<=i; j++)
cout<<"*";
}
.
.
.

Output:

*
**
***
****

First the value of i is assigned as 1, then comes the inner loop j =1(also assigned). Now inner loop having j will run until the test expression becomes true and then value of i increases from 1 to 2. Then inner loop will run again and the will go on like this until the test expression of i becomes true.Then it will terminate.

The do-while Loop


It is an exit controlled loop. It means that it evaluated its test expression after executing the loop body because its test expression lies at bottom of the loop. It runs atleast once whether the condition is true or false. Other loops(for and while), first checks the test expression then executes accordingly.



Syntax: do
              {
                   statement;
              }
              while(test_expression);   //Put semicolon after while statement


Program: 


#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr();  // For clear screen
int num=1;
do {
           cout<<num<<"\n";
           num++;
      }
     while(num<10);
getch();
}


Output: 

1
2
3
4
5
6
7
8
9
The do while loop will run as long as 'num' is smaller than 10.
You can ask questions if you have doubts regarding C++.