C++
  Home arrow C++ arrow Page 3 - Beginner's Guide to Functions in C++
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Beginner's Guide to Functions in C++
By: Rich C.
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 139
    2002-12-05

    Table of Contents:
  • Beginner's Guide to Functions in C++
  • A Basic Program
  • Using 2 Different Functions
  • Functions Calling Other Functions
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Beginner's Guide to Functions in C++ - Using 2 Different Functions


    (Page 3 of 5 )

    So now lets design a program that uses 2 different functions in our Main program. Lets design a program that can multiply 2 numbers or 4 numbers. We will create a function that can multiply 2 numbers, and another that can multiply 4 numbers. Each of these functions will have a prototype after our header and will each have its own function definition after Main.

    // Main.cpp

    #include <fstream.h>

    int MultTwo(int,int); //prototypes
    int MultFour(int,int,int,int);

    int a,b,c,d,e;

    int main()
    {
    a=1;
    b=2;
    c=MultTwo(a,b); //function call
    d=5;
    e=MultFour(a,b,c,d); //function call
    cout << e << endl;

    return 0;
    }

    int MultTwo(int x, int y)
    {
    return (x*y);
    }

    int MultFour(int m, int n, int o, int p)
    {
    return (m*n*o*p);
    }


    First we define our 2 function prototypes. We then declare 5 global variables (a,b,c,d,e). In Main we assign values to 'a', 'b', and 'd'. Values for 'c' and 'e' are derived from the values returned by each function. There is also one other thing you've probably noticed in the function definitions. We used new variables in our 2 functions to calculate the results. This demonstrates the flexibility of functions. When you send parameters to a function (such as 2 integers), the function can create its own variables and assign values to those created variables. For instance, our first function call is this:

    c=MultTwo(a,b);

    'a' and 'b' are sent to the MultTwo function in this exact order. The MultTwo function takes these two integers and assigns the value of 'a' to 'x', and 'b' to 'y'. The function then does its calculation and returns the result back to Main. 'c' grabs the result and now equals that value (which is 2). This may seem confusing at first, and it's hard to explain in plain English. Maybe a real-life example would help. When Microsoft developers get together to write a very, very large program, there could be up to 500 different coders assigned to the project. Each coder will have his/her own specific duty in writing functions that can be used by the Main program. The last thing Microsoft wants to do is create 10Million variables, send a fax of all of them to each developer, and say have fun. This would be very stupid.

    Instead, each developer knows what parameters are needed by the function they design, work with those parameters (arguments), and return a value in the end. They can create their own variables in their functions to help get the result they need, but these variables will only live inside the function and will probably not be seen by anyone else. In our program above, we design a function we know will receive 2 variables, but their names (a, b) do not matter to us. We just need their VALUES. With these values we can calculate a number needed and send it to Main. Get the idea?

    Void Functions
    Void functions are created and used just like value-returning functions except they do not return a value after the function executes. A void function just "does something" and then returns. The best way to understand this type of function is to create one that simply prints a header to a screen. For example, lets say we want to create a function that simply prints to the screen the title of the program and its author:

    void PrintHead()
    {
    cout << "Welcome to Multiply Program!" << endl;
    cout << "Written by: Rich C." << endl;
    }


    The function definition for a Void function is the same as a value-returning function, except it does not return a value to the function or program that calls it. Lets use this new function in our already existing Main program.

    // Main.cpp

    #include <fstream.h>

    int MultTwo(int,int);
    int MultFour(int,int,int,int);
    void PrintHead();

    int a,b,c,d,e;

    int main()
    {
    PrintHead();
    a=1;
    b=2;
    c=MultTwo(a,b);
    d=5;
    e=MultFour(a,b,c,d);
    cout << e << endl;

    return 0;
    }

    void PrintHead()
    {
    cout << "Welcome to Multiply Program!" << endl;
    cout << "Written by: Rich C." << endl;
    }

    int MultTwo(int x, int y)
    {
    return (x*y);
    }

    int MultFour(int m, int n, int o, int p)
    {
    return (m*n*o*p);
    }


    First notice that the function call to "PrintHead" does not return a value to any variable. To call a Void function, you just call its name and arguments, nothing more. So now our Main program first prints our new header to the screen and then multiplies the numbers we used before. See how simple this is? Also notice the function prototype is declared the same as a value-returning function is in the very beginning of the program.

    More C++ Articles
    More By Rich C.


       · thank you for this useful info.
     

    C++ ARTICLES

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT