A function is a sequence of statements designed to do a particular job. You already know that every program must have a function named main(). However, most programs have many functions, and they all work analogously to main.
Often, your program needs to interrupt what it is doing to temporarily do something else. You do this in real life all the time. For example, you might be reading a book when you remember you need to make a phone call. You put a bookmark in your book, make the phone call, and when you are done with the phone call, you return to your book where you left off.
C++ programs work the same way. A program will be executing statements sequentially inside one function when it encounters a function call. A function call is an expression that tells the CPU to interrupt the current function and execute another function. The CPU “puts a bookmark” at the current point of execution, and then calls (executes) the function named in the function call. When the called function terminates, the CPU goes back to the point it bookmarked, and resumes execution.
Here is a sample program that shows how new functions are declared and called:
//#include <stdafx.h> // Visual Studio users need to uncomment this line
#include <iostream>
// Declaration of function DoPrint()
void DoPrint()
{
using namespace std; // we need this in each function that uses cout and endl
cout << "In DoPrint()" << endl;
}
// Declaration of main()
int main()
{
using namespace std; // we need this in each function that uses cout and endl
cout << "Starting main()" << endl;
DoPrint(); // This is a function call to DoPrint()
cout << "Ending main()" << endl;
return 0;
}
This program produces the following output:
Starting main() In DoPrint() Ending main()
This program begins execution at the top of main(), and the first line to be executed prints Starting main(). The second line in main is a function call to DoPrint. At this point, execution of statements in main() is suspended, and the CPU jumps to DoPrint(). The first (and only) line in DoPrint prints In DoPrint(). When DoPrint() terminates, the caller (main()) resumes execution where it left off. Consequently, the next statment executed in main prints Ending main().
Functions can be called multiple times:
//#include <stdafx.h> // Visual Studio users need to uncomment this line
#include <iostream>
// Declaration of function DoPrint()
void DoPrint()
{
using namespace std;
cout << "In DoPrint()" << endl;
}
// Declaration of main()
int main()
{
using namespace std;
cout << "Starting main()" << endl;
DoPrint(); // This is a function call to DoPrint()
DoPrint(); // This is a function call to DoPrint()
DoPrint(); // This is a function call to DoPrint()
cout << "Ending main()" << endl;
return 0;
}
This program produces the following output:
Starting main() In DoPrint() In DoPrint() In DoPrint() Ending main()
In this case, main() is interrupted 3 times, once for each call to DoPrint().
Main isn’t the only function that can call other functions. In the following example, DoPrint() calls a second function, DoPrint2().
//#include <stdafx.h> // Visual Studio users need to uncomment this line
#include <iostream>
void DoPrint2()
{
using namespace std;
cout << "In DoPrint2()" << endl;
}
// Declaration of function DoPrint()
void DoPrint()
{
using namespace std;
cout << "Starting DoPrint()" << endl;
DoPrint2(); // This is a function call to DoPrint2()
DoPrint2(); // This is a function call to DoPrint2()
cout << "Ending DoPrint()" << endl;
}
// Declaration of main()
int main()
{
using namespace std;
cout << "Starting main()" << endl;
DoPrint(); // This is a function call to DoPrint()
cout << "Ending main()" << endl;
return 0;
}
This program produces the following output:
Starting main() Starting DoPrint() In DoPrint2() In DoPrint2() Ending DoPrint() Ending main()
Return values
If you remember, when main finishes executing, it returns a value back to the operating system (the caller) by using a return statement. Functions you write can return a single value to their caller as well. We do this by changing the return type of the function in the function’s declaration. A return type of void means the function does not return a value. A return type of int means the function returns an integer value to the caller.
// void means the function does not return a value to the caller
void ReturnNothing()
{
// This function does not return a value
}
// int means the function returns an integer value to the caller
int Return5()
{
return 5;
}
Let’s use these functions in a program:
cout << Return5(); // prints 5 cout << Return5() + 2; // prints 7 cout << ReturnNothing(); // This will not compile
In the first statement, Return5() is executed. The function returns the value of 5 back to the caller, which passes that value to cout.
In the second statement, Return5() is executed and returns the value of 5 back to the caller. The expression 5 + 2 is then evaluated to 7. The value of 7 is passed to cout.
In the third statement, ReturnNothing() returns void. It is not valid to pass void to cout, and the compiler will give you an error when you try to compile this line.
One commonly asked question is, “Can my function return multiple values using a return statement?”. The answer is no. Functions can only return a single value using a return statement. However, there are ways to work around the issue, which we will discuss when we get into the in-depth section on functions.
Returning to main
You now have the conceptual tools to understand how the main() function actually works. When the program is executed, the operating system makes a function call to main(). Execution then jumps to the top of main. The statements in main are executed sequentially. Finally, main returns a integer value (usually 0) back to the operating system. This is why main is declared as int main().
Some compilers will let you get away with declaring main as void main(). Technically this is illegal. When these compilers see void main(), they interpret it as:
int main()
{
// your code here
return 0;
}
You should always declare main as returning an int and your main function should return 0 (or another integer if there was an error).
Parameters
In the return values subsection, you learned that a function can return a value back to the caller. Parameters are used to allow the caller to pass information to a function! This allows functions to be written to perform generic tasks without having to worry about the specific values used, and leaves the exact values of the variables up to the caller.
This is a case that is best learned by example. Here is an example of a very simple function that adds two numbers together and returns the result to the caller.
// add takes two integers as parameters, and returns the result of their sum
// add does not care what the exact values of x and y are
int add(int x, int y)
{
return x + y;
}
int main()
{
// It is the caller of add() that decides the exact values of x and y
cout << add(4, 5) << endl; // x=4 and y=5 are the parameters
return 0;
}
When function add() is called, x is assigned the value 4, and y is assigned the value 5. The function evaluates x + y, which is the value 9, and then returns this value to the caller. This value of 9 is then sent to cout to be printed on the screen.
Output:
9
Let’s take a look at a couple of other calls to functions():
int add(int x, int y)
{
return x + y;
}
int multiply(int z, int w)
{
return z * w;
}
int main()
{
cout << add(4, 5) << endl; // evalutes 4 + 5
cout << add(3, 6) << endl; // evalues 3 + 6
cout << add(1, 8) << endl; // evalues 1 + 8
int a = 3;
int b = 5;
cout << add(a, b) << endl; // evaluates 3 + 5
cout << add(1, multiply(2, 3)) << endl; // evalues 1 + (2 * 3)
cout << add(1, add(2, 3)) << endl; // evalues 1 + (2 + 3)
return 0;
}
This program produces the output:
9 9 9 8 7 6
The first three statements are straightforward.
The fourth is relatively easy as well:
int a = 3;
int b = 5;
cout << add(a, b) << endl; // evaluates 3 + 5
In this case, add() is called where x = a and y = b. Since a = 3 and b = 5, add(a, b) = add(3, 5), which resolves to 8.
Let’s take a look at the first tricky statement in the bunch:
cout << add(1, multiply(2, 3)) << endl; // evalues 1 + (2 * 3)
When the CPU tries to call function add(), it assigns x = 1, and y = multiply(2, 3). y is not an integer, it is a function call that needs to be resolved. So before the CPU calls add(), it calls multiply() where z = 2 and w = 3. multiply(2, 3) produces the value of 6, which is assigned to add()’s parameter y. Since x = 1 and y = 6, add(1, 6) is called, which evaluates to 7. The value of 7 is passed to cout.
Or, less verbosely (where the => symbol is used to represent evaluation):
add(1, multiply(2, 3)) => add(1, 6) => 7
The following statement looks tricky because one of the parameters given to add() is another call to add().
cout << add(1, add(2, 3)) << endl; // evalues 1 + (2 + 3)
But this case works exactly the same as the above case where one of the parameters is a call to multiply().
Before the CPU can evaluate the outer call to add(), it must evaluate the inner call to add(2, 3). add(2, 3) evaluates to 5. Now it can evaluate add(1, 5), which evaluates to the value 6. cout is passed the value 6.
Less verbosely:
add(1, add(2, 3)) => add(1, 5) => 6
Effectively using functions
One of the biggest challenges new programmers encounter (besides learning the language) is learning when and how to use functions effectively. Functions offer a great way to break your program up into manageable and reusable parts, which can then be easily connected together to perform a larger and more complex task. By breaking your program into smaller parts, the overall complexity of the program is reduced, which makes the program both easier to write and to modify.
Typically, when learning C++, you will write a lot of programs that involve 3 subtasks:
- Reading inputs from the user
- Calculating a value from the inputs
- Printing the calculated value
For simple programs, reading inputs from the user can generally be done in main(). However, step #2 is a great candidate for a function. This function should take the user inputs as a parameter, and return the calculated value. The calculated value can then be printed (either directly in main(), or by another function if the calculated value is complex or has special printing requirements).
A good rule of thumb is that each function should perform one (and only one) task. New programmers often write functions that combine steps 2 and 3 together. However, because calculating a value and printing it are two different tasks, this violates the one and only one task guideline. Ideally, a function that calculates a value should return the value to the caller and let the caller decide what to do with the calculated value.
Quiz
1) What’s wrong with this program fragment?
void multiply(int x, int y)
{
return x * y;
}
int main()
{
cout << multiply(4, 5) << endl;
return 0;
}
2) What’s wrong with this program fragment?
int multiply(int x, int y)
{
int product = x * y;
}
int main()
{
cout << multiply(4, 5) << endl;
return 0;
}
3) What value does the following program fragment print?
int add(int x, int y, int z)
{
return x + y + z;
}
int multiply(int x, int y)
{
return x * y;
}
int main()
{
cout << multiply(add(1, 2, 3), 4) << endl;
return 0;
}
4) Write a function called doubleNumber() that takes one integer parameter and returns double it’s value.
5) Write a complete program that reads an integer from the user (using cin, discussed in section 1.3), doubles it using the doubleNumber() function you wrote for question 4, and then prints the doubled value out to the console.
Quiz Answers
To see these answers, select the area below with your mouse.
1.5 — A first look at operators
|
Index
|
1.3 — A first look at variables (and cin)
|
1.5 — A first look at operators
Index
1.3 — A first look at variables (and cin)
I noticed you include this comment we need this in each function that uses cout and endl when you include namespace std;.
Why do you include the library locally as opposed to globally? Are there functions in the library that conflict with other libraries that would cause a problem? Is it just a matter of preference? I hope I am not getting ahead of myself here.
That’s a very good question, actually.
Just as with local/global variables, it’s better to declare things at a local scope if you can (and it’s not too onerous). It reduces the chance of inadvertently changing something you didn’t mean to, or having a name clash.
While this is rarely a problem in small programs, in gigantic programs that run ten or hundreds of thousands of lines of code, your odds of having a strange name clash go up significantly.
Probably the safest solution is to not use a “using” statement at all, and explicitly call the function with it’s namespace qualifier:
std::cout << “Hello world!” << std::endl;
But that makes for some ugly, harder to read code (and I haven’t covered the scope qualifier yet!). Using a using statement at the function level is a nice compromise between safety and readability.
One problem I noticed with your source code for question number 5 is that you cannot use double as a function name, at least in Visual Studio 2005, as double is a reserved word. Changing it to Double allows it to work.
I may be wrong for other compilers, but I think that double is a reserved word in most CPP builds.
Thanks, Jim. You are absolutely right. I changed the function name.
That’s one advantage of using function names that begin with capital letters instead of lowercase — it avoids conflicts with language keywords.
[...] A first look at functions [...]
shouldn’t there be the cin.ignore(); statement after cin >> xxx; ? if I use the source code like in solution to question 5, the program terminates after I give in the number and press enter.
Sandor, that’s a compiler dependent issue. Some compilers close the output window immediately upon termination, while others (such as Visual Studio) hold it open until the user presses a key.
If your compiler immediately closes the output window, then for any console program you will need to add a line of code that waits for a keystroke before exiting. There are many different ways to do this.
I typically use
cin.get();i use visual basic and when i type:
#include
#include
int double(int x)
{
return 2 * x;
}
int main()
{
using namespace std;
int x;
cin >> x;
cout —— Build started: Project: helloworld, Configuration: Debug Win32 ——
1>Compiling…
1>helloworld.cpp
1>c:\documents and settings\samuel ac\my documents\c++\helloworld\helloworld\helloworld\helloworld.cpp(4) : error C2632: ‘int’ followed by ‘double’ is illegal
1>c:\documents and settings\samuel ac\my documents\c++\helloworld\helloworld\helloworld\helloworld.cpp(4) : error C2062: type ‘int’ unexpected
1>c:\documents and settings\samuel ac\my documents\c++\helloworld\helloworld\helloworld\helloworld.cpp(5) : error C2143: syntax error : missing ‘;’ before ‘{’
1>c:\documents and settings\samuel ac\my documents\c++\helloworld\helloworld\helloworld\helloworld.cpp(5) : error C2447: ‘{’ : missing function header (old-style formal list?)
1>Build log was saved at “file://c:\Documents and Settings\Samuel AC\My Documents\C++\helloworld\helloworld\helloworld\Debug\BuildLog.htm”
1>helloworld - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Hi Sam. Double is a keyword in C++, and it shouldn’t be used as a function name. I’d previously updated question 4 to address this issue, but I missed the same issue for question 5. It’s fixed now.
These tutorials are fantastic :]
Fox is right :)
Why is this not working?
Ignore the include blank thing, I dont know why its doing that, I have iostream after that, this thing keeps erasing it..
The problem is that you have a semicolon after
Remove the semicolon and it should work as expected.
I am trying to do the last question. Using code::blocks i have come up with this:
#include
#include
int main()
{
int doPrint()
{
int doublenumber(int x=5)
return 2*x;
}
I’m confused. please tell me what i’m doing wrong.
Josh, please put your code in PRE html tags, otherwise, it gets treated as HTML rather than C++ code.
There are two problems here: First, it looks like you’ve declared int doublenumber(int x=5) correctly, but you’ve done so inside doPrint(). Replace doPrint() with your doublenumber declaration. Second, you’ve declared your functions inside other functions, which C++ does not allow. Place your functions above main().
what’s the tag?
<PRE>code here</PRE>
Great tutorials thus far. Question on Quiz #5 for you. Your solution obviously works fine:
int doubleNumber(int x) { return 2 * x; } int main() { using namespace std; int x; cin >> x; cout < < doubleNumber(x) << endl; return 0; }But — when I do my solution, which is basically the same as yours but listed in a different order:
int main(){ using namespace std; int x; cin >> x; cout < < doubleNumber(x) << endl; return 0; } int doubleNumber(int x){ return 2 * x; }I get a compiler error in MS Visual Cpp 2008 Express [error C3861: 'doubleNumber': identifier not found].
Now, when I move my “doubleNumber(int x)” function so it appears ABOVE “main()” like so:
int doubleNumber(int x){ return 2 * x; } int main(){ using namespace std; int x; cin >> x; cout < < doubleNumber(x) << endl; return 0; }it works fine. So my question is: I thought the placement of “main()” within the code isn’t supposed to matter? I thought I read that somewhere in your tutorials… But regardless where I think I saw that, it doesn’t make sense that “main()” would have to appear at the bottom of every piece of Cpp code. Am I overlooking something?
Thx,
-Dan
Dan, when the compiler compiles your program, it reads through the files sequentially. This means if it hasn’t encountered doubleNumber() by the time you try to use doubleNumber(), then it will give you an error.
There are two ways to fix this:
1) Put your functions above main()
2) Use a function prototype. This means, put the function declaration
int doubleNumber(int x);above main(), and leave the actual function definition below main(). That way, when the compiler encounters doubleNumber(), it will know what to expect, even if you haven’t actually defined how doubleNumber() is implemented yet.Thanks for the feedback, Alex. I understand what you’re saying.
So generally speaking, when Cpp developers write their code, (the assumption I’m making here is that) they’re always putting their functions above “main()”? I would think that, by doing your fix #2 — i.e. using a function prototype for every function — that would create a lot of extra lines of code, especially when you get into software that has thousands of lines of code.
So the practice among Cpp developers is to have “main()” defined at the bottom of all Cpp code?
When developing single-file programs, yes. But when you get into multi-file programs, things become a bit more complex. In the case of multi-file programs, the typical way of doing this is to put the function prototypes in a header file, and then #include the header file wherever you need access to those functions. I cover this in more detail in section 1.9.
i’m dong question 5 how you said. At the moment it looks like this:
#include
int doublenumber(int x)
{
return 2*x;
}
int main ()
{
using namespace std;
int x;
cin >> x;
cout
But all I get when I run and build it is the hello world program.
I keep writing these things but it never does what i want it to.
Would you happen to know why it is doing this and how the problem could be solved?
It sounds to me like you’re compiling and executing the wrong program. Many IDEs let you have multiple projects open simultaneously — however, only one project will be active/selected, and this is the one that will compile/execute. Make sure you have your doublenumber project active. Usually active projects have their names in bold.
sorry, I cant get the PRE tags to work.
Hmmm, that’s bizarre. They have been working for other people in this thread (eg. Dan). They seem to work for me okay too:
What browser/OS are you using?
windowa 2000. Dont know what IE i’m using.
I’ve managed it in Dev-c and it runs ok but when I type in a number it doesn’t double.
My code is same as the solution.
Without seeing your code I couldn’t say what the problem is. Since you’re having issues with the pre tags, try posting your code in my forum and I’ll address your issue there.
OK then I’ve signed up to the forums. Which thread is it or do I make a new one?
Make a new thread in the “For Beginners” section.
I think these are absolutely great tutorials, certainly the best I’ve ever found. Please keep up the great work Alex!
I feel silly saying this, since I’m just starting out, but shouldn’t quiz questions one through three have
using namespace std;
statements in their main functions since they use cout and endl?
Thanks for the complements, and your question isn’t silly at all. If you wanted to compile these fragments, you’d need to add
using namespace std;as well as#include, but since they’re just code fragments rather than entire programs meant to be compiled I omitted the minor details.Hi Alex,
I was just doing question 5 of the quiz like everyone else, but the problem is that when I input a number the window closes once immediately after it displays the answer. I know you have to add the statements like cin.get(); cin.clear(); and cin.ignore(255, ‘/n’);, however, it does doesn’t prevent it from happening. Heres my coding.
I tried adding cin.get, as I have commented. THANKS
Try:
int main() { int x; cin >> x; x = doubleNumber(x); cout < < x << endl; cin.clear(); cin.ignore(255, '\n'); cin.get(); return 0; }what is the difference between the following declarations
void show()and void show(void)
There is no difference. They are functionally equivalent.
[ I answered your question in my forum. -Alex ]
[...] andrew: [ I answered your question in my forum. -Alex ] [...]
[...] andrew: [ I answered your question in my forum. -Alex ] [...]