C++ gurus and learners, we program ourselves :P all work is CC licensed

Friday, November 8

Best Explanation of Functions by Usman Sherry

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)

------------------------------------------------------------------------------------------------------------
  1. #include<iostream>
  2. using namespace std;
  3. void function();                                 //with or without arguments dose not effect it's output type
  4. void main()
  5. {
  6.           function();
  7. void function()
  8. {
  9.            cout<<"To display something "<<endl;
  10. }                                                         // void type returns no value with or without argument
------------------------------------------------------------------------------------------------------------
Output will be:
To display something                                                                               Press any key to continue . . .                                                                   
------------------------------------------------------------------------------------------------------------

Void: (with arguments)
------------------------------------------------------------------------------------------------------------
  1. #include<iostream>
  2. using namespace std;
  3. void function(int);                 //with or without arguments dose not effect it's output type
  4. void main()
  5. {
  6.           int y;
  7.           cin>>y;
  8.           function(y);
  9. void function(int y)
  10. {
  11.            cout<<"Square is :"<<y*y<<endl;
  12. }                                                // 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

------------------------------------------------------------------------------------------------------------
  1. #include<iostream>
  2. using namespace std;
  3. int function();         //with or without arguments dose not matter it's output type
  4. void main()
  5. {
  6.        cout<<function()<<endl;
  7. int function()
  8. {
  9.  return 5*99;
  10. }                       // must return a value 
------------------------------------------------------------------------------------------------------------
Output will be:
495                                                                                                       Press any key to continue . . .                                                                   

------------------------------------------------------------------------------------------------------------

with argument

------------------------------------------------------------------------------------------------------------
  1. #include<iostream>
  2. using namespace std;
  3. int function(int,int);    //with or without arguments dose not matter it's output type
  4. void main()
  5. {
  6.        int x,y;
  7.        cin>>x>>y;
  8.        cout<<function(x,y)<<endl;
  9. int function(int x,int y)
  10. {
  11.   return y*x;
  12. }                         // must return a value 
------------------------------------------------------------------------------------------------------------
Output will be: (x=5 / y=3)
5                                                                                                           
3                                                                                                           
15                                                                                                         Press any key to continue . . .                                                                   

------------------------------------------------------------------------------------------------------------

3 comments :