Home arrow C++ arrow Page 3 - Beginner's Guide to Functions in C++
C++

Beginner's Guide to Functions in C++


Rich is a self-confessed C++ guru! In this article he shows us how functions work in C++ and how to use them in everyday C++programming.

Author Info:
By: Rich C.
Rating: 4 stars4 stars4 stars4 stars4 stars / 235
December 05, 2002
TABLE OF CONTENTS:
  1. · Beginner's Guide to Functions in C++
  2. · A Basic Program
  3. · Using 2 Different Functions
  4. · Functions Calling Other Functions
  5. · Conclusion

print this article
SEARCH DEVARTICLES

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.
blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials