Sunday, 8 December 2013

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.......!