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

Wednesday, 20 November 2013

If else if Statements

# include<iostream>
using namespace std;
void main()
{
cout<<"Enter any number";
cin>>a;
if(a==0)
cout<<"value of a is o"<<endl;
else if(a==2)
cout<<"Value of a is 2"<<endl;
else if(a==3)
cout<<"Value of a is 3"<<endl;
else
 cout<<"Value of a is not 0,2 and 3"<<endl;

}

In this program,
we will take value from user,the value user gives is store in a(data type int)
If the user give inout o(zero),then the program says that:-
"Value of a is o".......!!
How is this possible,Actually if statement is a check which will check either value of a is zero nor not..!
If it is zero then Program says that,Valus is zero.....!
But if value of a is 2,then the command transfers to thie statement and this statement is printed:-
"Value of a is 2"...!
and if the user gives value of  a is 3,then the command transfers to thie statement and this statement is printed:-
"Value of a is 3"...!
but if it is not zero then the command transfers to the else,and the program says,
"Value of a is not zero"..!
Note:-
If you want one statement to be executed with if and else,then no need to place braces(show in program),but if two or more statements are,then you will put braces,(Explain in this link:-http://jdrana.blogspot.com/2013/11/if-else-statement.html)
I hope you wil got this..!
Feel free to give suggestions in comment box.......!