Sunday 8 December 2013

Structures

# include<iostream>
using namespace std;
struct student
{
char name[10];
int r;
float m;
};
void main()
{
student s;
cout<<"Enter name"<<endl;
cin>>s.name;
cout<<"Enter Roll number"<<endl;
cin>>s.r;
cout<<"Enter Marks"<<endl;
cin>>s.m;
cout<<"Name is"<<s.name<<endl;
cout<<"Roll number is"<<s.r<<endl;
cout<<"Marks is"<<s.m<<endl;
}

In this program,we will discuss Structures........!!

First,What is the Structure is??Why we need it??

Structures:-
Structures are a way of storing many different values in variables of potentially different types under the same name. This makes it a more modular program, which is easier to modify because its design makes things more compact. Structs are generally useful whenever a lot of data needs to be grouped together--for instance, they can be used to hold records from a database or to store information about contacts in an address book. In the contacts example, a struct could be used that would hold all of the information about a single contact--name, address, phone number, and so forth.

I think now,you got is what the structure is......!!

Syntax:-
struct structurename
{
only data types not functions
};

Note:-
before using structure,you will first make it and then use it........!!It will be not make in any function...!!



In this program,we will make a student structure and make dattypes of  name,roll number and marks.....!!
In main,we will make an object of student(structurename) and enter name and other things in it......!!
s.name mean using s we will assign name,and so on......!!

Then,after we will printing name and other datatypes using s.......!!


Here,you will make your own Datatype like int,float but in your datatype student there is one char,one int and one float........!!

I think you got it.........!!!

Feel free to give suggestions in comment box.......! 


Function by Reference

#include <iostream>
using namespace std;
void swap(int &a, int &b);
int main ()
{
  int a,b,z;
  cout<<"Enter First Number"<<endl;
  cin>>a;
  cout<<"Enter Second Number"<<endl;
  cin>>b;
  cout<<"Numbers before Swapping"<<endl;
  cout<<a<<endl<<b<<endl;
  swap(a,b);
  cout<<"Numbers after Swapping"<<endl;
  cout<<a<<endl<<b<<endl;
}
void swap(int &a, int &b)
{
int c;
c=a;
a=b;
b=c;
}

In this,we will discuss sending data by reference......!!
But,First you will knowm what the difference b/w Calling function by Value and
Calling function by Reference......!!
Call by Value:-
1)Call by value passes the value of actual parameter to formal parameter
2)The actual and formal parameters refer to different memory locations.
3)Any change made by function in formal parameter does not affect the value of Actual parameter.
4)It Requires more Memory.
5)It is less Efficient.
Call By Reference:-
1)Call by Reference passes the address of actual parameter to formal parameter.
2)The actual and formal parameters refer to same memory locations.
3)Any change made by function in formal parameter actually changes the value of Actual parameter.
4)It Requires Less Memory.
5)It is more Efficient.

Now,I think you known Whats the difference b/w..............!!!
In this program,we will simply geting two numbers from users and then displaying it,and afterthat we call the function swap() and it will swap these two numbers(mean a is put to b and b is put to a).......!!!
Note:-
If you want to type your function after main,then you will always tell the compiler that a function name this will be using in this program like in this case....!!
What the swap() function is doing.....??
We will send int a,b to it,but on other side(function side) there are pointers which will save there addresses respectively........!!
And its done...!
If we give,
a=2;
b=3;
After swapping it will be,
a=3;
b=2;


I think you got it.........!!!

Feel free to give suggestions in comment box.......! 

Pointers


#include <iostream>
using namespace std;

int main ()
{
  int a = 5,b = 15;
  int *p1, *p2;

  p1 = &a;             // p1 = address of firstvalue
  p2 = &b;             // p2 = address of secondvalue          
  cout<<*p1<<endl<<a<<endl;
  cout<<"*p2<<b<<endl;
  cout<<&a<<p1<<endl;
  cout<<&b<<p2<<endl;
 
  
}


In this,we will discuss pointers......!
So,first we known what are pointers.....!!
Pointers:-
Pointers are an extremely powerful programming tool. They can make some things much easier, help improve your program's efficiency, and even allow you to handle unlimited amounts of data. For example, using pointers is one way to have a function modify a variable passed to it. It is also possible to use pointers to dynamically allocate memory, which means that you can write programs that can handle nearly unlimited amounts of data on the fly--you don't need to know, when you write the program, how much memory you need. Wow, that's kind of cool.


In this program,we will take two int,a and b and two pointers,p1 and p2 of int type.....!!
When we assign p1=&a,we will giving p1 address of a and same case with p2.....!!
and then we will printing out *p1,which will print the value which we give to a .....!!
How's this possible.......Actually,p1 is pointing to a(not to its value) and *p1 means what the value of a is........!!

And if we print &a it will print the address of a,and if we print p1 it will also printing the address(These two addresses are same) and same case with p2 and b......!!

I think you got it.........!!!
Feel free to give suggestions in comment box.......! 

Wednesday 4 December 2013

Return Function

#include <iostream>
using namespace std;
int addition (int a, int b)
{
  int r;
  r=a+b;
  return r;

}

int main ()
{
  int a,b,z;
  cout<<"Enter First Number"<<endl;
  cin>>a;
  cout<<"Enter Second Number"<<endl;
  cin>>b;
  z=addition(a,b);
  cout <<"The result is"<<z;

 
}
In this program,We will discuss Return  Function....!!
 first of all remember something said at the beginning of this tutorial: a C++ program always begins its execution by the main function. So we will begin there.

We can see how the main function begins by declaring the variable z of type int. Right after that, we see a call to a function called addition. Paying attention we will be able to see the similarity between the structure of the call to the function and the declaration of the function itself some code lines above: 

 

The parameters and arguments have a clear correspondence. Within the main function we called to additionpassing two values: 5 and 3, that correspond to the int a and int b parameters declared for function addition.

At the point at which the function is called from within main, the control is lost by main and passed to functionaddition. The value of both arguments passed in the call (5 and 3) are copied to the local variables int a and int b within the function.

Function addition declares another local variable (int r), and by means of the expression r=a+b, it assigns to rthe result of a plus b. Because the actual parameters passed for a and b are 5 and 3 respectively, the result is 8.

The following line of code:

return r;
finalizes function addition, and returns the control back to the function that called it in the first place (in this case,main). At this moment the program follows its regular course from the same point at which it was interrupted by the call to addition. But additionally, because the return statement in function addition specified a value: the content of variable r, which at that moment had a value of 8. This value becomes the value of evaluating the function call.   So being the value returned by a function the value given to the function call itself when it is evaluated, the variable z will be set to the value returned by addition (5, 3), that is 8. To explain it another way, you can imagine that the call to a function (addition (5,3)) is literally replaced by the value it returns (8).
I think you got it.........!!!
Feel free to give suggestions in comment box.......!

Parameterized Function

#include <iostream>
using namespace std;
void addition (int a, int b)
{
  int r;
  r=a+b;
  cout <<"The result is" <<r;

}

int main ()
{
  int a,b;
  cout<<"Enter First Number"<<endl;
  cin>>a;
  cout<<"Enter Second Number"<<endl;
  cin>>b;
  addition(a,b);
 
}
In this program,We will discuss Parameterized Function
A Parameterized Function is the Function,which will recieve parameters from any  other function(Main is also a function).
we will enter two numbers and these two numbers will save in a and b(Type int),
then we will send these two numbers to addition function,which will add these two numbers and display there result.......!!
Note:-


As shown in figure,a will be send to x,b will be send to y and c will be send to z  respectively and in our case a will be send to a and b will be send to b...!!
The a and b,which we will sending from the main is the data type of main,and,a and b which is recieving these two are data types of addition function......!!

Function

# include<iostream>
using namespace std;
void dis()
{
cout<<"I am the Display function"<<endl;
}
void main()
{
cout<<"This program is calling the function"<<endl;
dis();
}

In this program,
We will discuss function:-
What is a Function??
A function is a group of statements that is executed when it is called from some point of the program. 

Using functions we can structure our programs in a more modular way, accessing all the potential that structured programming can offer to us in C++.

The following is its format:

type name ( parameter1, parameter2, ...)
statements 
}

In the type name,we will enter type such as int float,char etc....!
It is used when the function can return some thing like some value,some character etc.
If we want,a function cannot return some thing then will will put "void",in type.This type is also known as return type
Next,is the function name,It will be any name....In this case,function name is dis!!
Next,Is the parameters,The parameters is the value which we are sending to the function like It may be int,float,char etc.....And there will be as many parameteres as you want in your function.
In this program,we will simply call dis() function and others things I think you got earlier.....!!
Feel free to give suggestions in comment box.......!