Friday, November 8
Best Explanation of Functions by Usman Sherry
00:05
By
Usman Shery
c++
,
C++ Functions Explained
,
usman sherry
,
void functions c++ use
3
comments
Some basic understanding to use functions:
First of all we have to look for output of functions as we want
- void
- int
- double
- and so for other types. The major difference is between void and non-void (integral, character and float) outputs.
Void: (with no arguments)
------------------------------------------------------------------------------------------------------------
- #include<iostream>
- using namespace std;
- void function(); //with or without arguments dose not effect it's output type
- void main()
- {
- function();
- }
- void function()
- {
- cout<<"To display something "<<endl;
- } // void type returns no value with or without argument
------------------------------------------------------------------------------------------------------------
Output will be:
To display something Press any key to continue . . .
------------------------------------------------------------------------------------------------------------
Void: (with arguments)
------------------------------------------------------------------------------------------------------------
- #include<iostream>
- using namespace std;
- void function(int); //with or without arguments dose not effect it's output type
- void main()
- {
- int y;
- cin>>y;
- function(y);
- }
- void function(int y)
- {
- cout<<"Square is :"<<y*y<<endl;
- } // void type returns no value with or without argument
------------------------------------------------------------------------------------------------------------
Output will be: (let y=5)
Square is :25 Press any key to continue . . .
_________________________________________________________________
------------------------------------------------------------------------------------------------------------
Other types of outputs:
without argument
------------------------------------------------------------------------------------------------------------
- #include<iostream>
- using namespace std;
- int function(); //with or without arguments dose not matter it's output type
- void main()
- {
- cout<<function()<<endl;
- }
- int function()
- {
- return 5*99;
- } // must return a value
------------------------------------------------------------------------------------------------------------
Output will be:
495 Press any key to continue . . .
------------------------------------------------------------------------------------------------------------
with argument
------------------------------------------------------------------------------------------------------------
- #include<iostream>
- using namespace std;
- int function(int,int); //with or without arguments dose not matter it's output type
- void main()
- {
- int x,y;
- cin>>x>>y;
- cout<<function(x,y)<<endl;
- }
- int function(int x,int y)
- {
- return y*x;
- } // must return a value
------------------------------------------------------------------------------------------------------------
Output will be: (x=5 / y=3)
5
3
15 Press any key to continue . . .
------------------------------------------------------------------------------------------------------------
are you a teacher of c++
ReplyDeleteno he is just a first semester student who has done pre-engineering
ReplyDeletegood job.........Usman..
ReplyDelete