Navigation



1.4 — A first look at functions

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.

//#include <stdafx.h> // Visual Studio users need to uncomment this line
#include <iostream>

// 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()
{
    using namespace std;
    // 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():

//#include <stdafx.h> // Visual Studio users need to uncomment this line
#include <iostream>

int add(int x, int y)
{
    return x + y;
}

int multiply(int z, int w)
{
    return z * w;
}

int main()
{
    using namespace std;
    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:

  1. Reading inputs from the user
  2. Calculating a value from the inputs
  3. 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) Show Solution

2) Show Solution

3) Show Solution

4) Show Solution

5) Show Solution

1.5 — A first look at operators
Index
1.3 — A first look at variables (and cin)

232 comments to 1.4 — A first look at functions

  • victory501

    does this code work for #5?

    #include "stdafx.h"

    #include

    int doublenumber(int x)
    {
    return 2 * x;
    }
    int main()
    {
    int x;
    x = 5;
    using namespace std;
    cout <> x;
    cout << "You entered " << 2 * x << endl;

    return 0;
    }

    • Scottt117

      My ‘double number’ code:

      #include
      #include

      int main ()
      {
      using namespace std;
      int x;
      cin >> x;
      cout << " " << endl;
      cout << "multiplied by 2 is: ";
      cout << x*2 << endl;
      system ("PAUSE");
      return 0;

      }

      Simple.

      • Frostworld

        You didn’t use a function.

        int doubleNumber(int x)
        {
        return 2*x;
        }

        int main()
        {
        int num;
        scanf(“%d”,&num);
        printf(“%d”,doubleNumber(num));
        return 0;
        }

  • Aditya

    why is declaring void main()
    illegal??

    my compiler has no problem in compiling it.

    also when we enter return 0;
    are we able to see it in the output screen??

    • lynx1241

      As the text above already stated, some compilers will let you get away with declaring main as void main(). But they will still interpret it as:

      int main()
      {
      // your code here
      return 0;
      }

      Others will just generate an error.

    • brandon broadwater

      the reason that you cant use the statement “void” for the main function, is because in the main function, you have to return an integer. you know how you have to return 0; at the end of the main function? that just means you should have no errors. but anyway, 0 is an integer, and thats why you always have to use “int”.

  • enak101

    I just wanted to say the answer for number 5 results in errors compiling. Visual C++ 2010. I used someone’s from comments and it worked fine.

    // 1.4 (4).cpp : Defines the entry point for the console application.
    //

    #include “stdafx.h”
    #include
    using namespace std;

    int doubleNumber(int x)
    {
    return 2 * x;
    }
    int main()
    {
    cout << "Input a number to be doubled:" <> x;
    cout << "The number: " << x << " has been entered" << endl;
    cout << "Your number has been doubled to: " << doubleNumber(x) << endl;
    return 0;
    }

    This works fine.

    #include
    02

    03
    int doubleNumber(int x)
    04
    {
    05
    return 2 * x;
    06
    }
    07

    08
    int main()
    09
    {
    10
    using namespace std;
    11
    int x;
    12
    cin >> x;
    13
    cout << doubleNumber(x) <> x;
    23
    x = doubleNumber(x);
    24
    cout << x << endl;
    25
    return 0;
    26
    }

    This doesn't work. I tried using each method of int main twice and that didn't work.

    If you could change it that would be good for people in the future.

    • lynx1241

      Maybe you should consider removing the ..eNumber(x) x;.. part and instead use the cin function as suggested. Also alter the empty preprocessor directive to

      <code>#include <iostream>;<code>

      , and declare using namespace std; inside the functions wich use cout or cin. That should fix it.

  • enak101

    Ignore the number lines there haha i didnt paste the number lines into IDE.

  • cooper507

    ok guys quick question
    when you run this program why does the words “Starting DoPrint()” pop up before “In DoPrint2()” even the the DoPrint2()function is listed first?

    #include “stdafx.h”
    #include

    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;
    }

    • A Random Noob

      Because the DoPrint() function was called first by main().

      • WordPress

        The becomes virtually unnecessary when it comes to multiple functions being called. The main() function is ALWAYS the first function to start executing even if it may not be the first thing in the source code. Whatever function is called by main() will take action no matter what the order is in the source code. Order only matters inside functions.

  • cooper507

    what exactly does the command “endl” do? i have an idea but i would like to know precisely what it does.

    thx guys :)

  • cooper507

    do you have to have a main() function in all programs?

  • cooper507

    in the piece of code, for example:

    int add(int x, int y)
    {
    return x + y;
    }

    does it matter that when you create a function you have the (int x, int y) in the parenthesis? what does it do for the function?

    heres another example:

    int multiply(int z, int w)
    {
    return z * w;
    }

  • cooper507

    how do i activate numbered lines?

  • Homesweetrichard

    can we write it like this “void DoPrint() {” please sense its easier to read both my mac and me. Since this is universally understood. programming grammar.

    Not like this
    “void DoPrint()
    {”
    Which is not universally understood grammar.

  • Homesweetrichard

    Also, this reads as if you could nest this (refering to the doprints section)… Or maybe im a idiot…

    also i noticed all basic versions of math were used expect division, is there a reason why?

    also why does this code work, Expecailly main2, i understand doprint and doprint2, but why does it understand main2. if it understands it as another main (which it doesnt appeer so, i have tryed removing main2 which has caused it to be ignored. I have also tryed moving it after main [main2 was ingored with our without the "main2();" in the main.].) Or is it for just having a name or does it have more privilage?

    #include iostream
    
    void DoPrint() {
    	using namespace std;  // we need this in each function that uses cout and endl
    	cout << "In DoPrint()" << endl;
    	cout << "nice to meet you!" << endl;
    
    }
    
    int main2 () {
    	using namespace std;
    	cout << "print and main both are crazy if this works" << endl;
    	cout <> x; // same
    
    	}
    
    int main () {
    	using namespace std;
    	cout << "Starting main()" << endl;
    	cout << "Hello world!" << endl; //standard hellow world intro, 1.1
    	DoPrint(); // This is a function call to DoPrint()
    	main2();
    	cout << "Ending main()" << endl;
    
    	return 0;
    
    }
    
    • A Random Noob

      I am not a pro here, but, I think that there is only one main, main2 is the same as doprint2, but, main isn’t the same as doprint.

      • Da-Rage44

        You have a backwards “<" for one, that and you have no tags on also even if you fix the “<" "cout << x;" still leaves x as an unidentified integer. Your function also has no return value. Here is your code fixed.
        #include "stdafx.h"
        #include

        void DoPrint() {
        using namespace std; // we need this in each function that uses cout and endl
        cout << "In DoPrint()" << endl;
        cout << "nice to meet you!" << endl;

        }

        int main2 ()
        {
        using namespace std;
        int x = 4;
        cout << "print and main both are crazy if this works" << endl;
        cout << x; // same

        return 0;
        }

        int main () {
        using namespace std;
        cout << "Starting main()" << endl;
        cout << "Hello world!" << endl; //standard hellow world intro, 1.1
        DoPrint(); // This is a function call to DoPrint()
        main2();
        cout << "Ending main()" << endl;

        return 0;

        }
        X has been defined and your int main2() function has a return value.

    • A Random Noob

      Also division is also used in C++. read on…

  • Homesweetrichard

    int main2 () {
    using namespace std;
    cout << "print and main both are crazy if this works" << endl;
    cout <> x; // same

    return -2;

    }

  • Homesweetrichard

    okay i cant get all my code and comments on..

  • Homesweetrichard


    #include <iostream>

    void DoPrint() {
    using namespace std; // we need this in each function that uses cout and endl
    cout << "In DoPrint()" << endl;
    cout << "nice to meet you!" << endl;

    }

    int main2 () {
    using namespace std;
    cout << "print and main both are crazy if this works" << endl;
    cout << "Enter a number: "; // no real reason for this being here other than to know the event happend

    int x; // Same
    cin >> x; // same

    }

    int main () {
    using namespace std;
    cout << "Starting main()" << endl;
    cout << "Hello world!" << endl; //standard hellow world intro, 1.1
    DoPrint(); // This is a function call to DoPrint()
    cout << "Ending main()" << endl;
    main2();
    return 0;

    }

  • Homesweetrichard

    int crap () {
    using namespace std;
    cout << "crap, here" << endl;
    }

    i also add that one, into the code most i got was a warning (warning: control reaches end of non-void function)(script still worked),

    main now looks like this

    int main () {
    using namespace std;
    cout << "Starting main()" << endl;
    cout << "roll call, world!" << endl; //standard hello world intro, 1.1
    DoPrint(); // This is a function call to DoPrint()
    cout << "Ending main()" << endl;
    main2();
    crap();
    return 0;

    }

  • Homesweetrichard

    is there a easyer way to do math with cpp, as if its not already hard enough to write already.

  • ingthinks

    Here is my solution to #5:

    #include “stdafx.h”
    #include

    int doubleNumber(int x) // doubles value of x
    {
    return x * 2;
    }

    int main()
    {
    using namespace std;
    int y; // declares variable y
    int x; // declares variable x
    cout <> x; // defines x as users input
    y = x; // defines y as equal to x to display the number that was doubled
    cout << y << " doubled is " << doubleNumber(x) << endl;
    return 0;
    }

    • You don’t actually need y, as you can simply do this
      cout << x << " doubled is " << doubleNumber(x) << When you take the x the first time, it will hold the original number, when you use doubleNumber, it will output the result of x * 2.
      That way the code is more optimized, as it requires less memory to hold the values, and it will no longer need to assign the value of x to y, which is a waste of CPU cycles (I know that in this case the difference will be a nanosecond at most, however in bigger programs this difference might be seconds).

      • ingthinks

        i see, thanks for helping me to understand this better
        here is my updated code:

        #include “stdafx.h”
        #include

        int doubleNumber(int x) // doubles value of x
        {
        return x * 2;
        }

        int main()
        {
        using namespace std;
        int x; // declares variable x
        cout <> x; // defines x as users input
        cout << x << " doubled is " << doubleNumber(x) << endl;
        return 0;
        }

        same functionality as my original program but i can see how this is a better way of doing it

  • ingthinks

    #include "stdafx.h"
    #include <iostream>

    int doubleNumber(int x) // doubles value of x
    {
    return x * 2;
    }

    int main()
    {
    using namespace std;
    int x; // declares variable x
    cout << "Input an integer to double: ";
    cin >> x; // defines x as users input
    cout << x << " doubled is " << doubleNumber(x) << endl;
    return 0;
    }

  • WordPress

    Alex, are parameters only used for mathematics?
    By the way, I found out a way to calculate the addition of two numbers with only one function =D

    #include(less than)iostream(more than)
    #include(less than)conio.h(more than)
    using namespace std;
    int main(){//returns 0
    double dA,dB,dC;
    cout<>dA;
    cout<>dB;
    dC=dA+dB;
    cout<<dA<<" + "<<dB<<" = "<<dC<>dA;
    cin>>dB;
    dC=dA+dB;
    cout<<dC;
    }

  • WordPress

    Alex, I love these tutorials.

  • Lakshya

    here is my code :

    
    #include <iostream>
    
    int doublenumber (int x)
    {
        return 2*x;
    }
    
    int main()
    {
        using namespace std;
        cout << "Enter number to be doubled:";
        int x;
        cin >> x;
        cout << " The doubled number is:" ;
        x = doublenumber(x);
        cout << x << endl;
    }
    
  • Victor Campello

    after debugging a dozen times and re-read the past chapters I finally got it! (I didn’t want to pick the answer).

    //btw: I made a few tweaks to my code based on the other users.

    #include // Visual Studio users need to uncomment this line
    #include

    int doublenumber (int x)
    {
    return 2 * x;
    }

    int main()
    {
    using namespace std;
    cout <> y;
    cout << "The number doubled is: ";
    cout << doublenumber(y) << endl;

    cin.clear();
    cin.ignore(255, '\n');
    cin.get();
    return 0;
    }

  • betefeel

    Took me some time but finally got 5 Yay!

    #include “stdafx.h”

    #include

    int doubleNumber(int x)
    {
    return x + x;
    }

    int main()
    {

    using namespace std;
    cout << "Enter your number to double: " <> x;
    cout << "Your number doubled is: " << endl;
    cout << doubleNumber(x) << endl;

    }

    • betefeel

      Copied wrong

      int doubleNumber(int x)
      {
      return x + x;
      }

      int main()
      {

      using namespace std;
      cout << "Enter your number to double: " <> x;
      cout << "Your number doubled is: " << endl;
      cout << doubleNumber(x) << endl;

      }

  • Yahsharahla

    Hi I decided to post mine since it looks like I chose a different way to make this program but with the same results.

    #include "stdafx.h"
    #include <iostream>
    
    int doubleNumber()
    {
    	using namespace std;
    	cout << "Input a number." << endl; // Asks the user to enter a number
    	int x;
    	cin >> x; //Reads user's input and stores it in place of x
    	return (x * 2);
    }
    
    int aI()
    {
    	using namespace std;
    	cout << "Hi, let's play a game!" << endl; // A little A.I. lol
    	cin.get();
    	cout << "I guarantee that I can double any number that you input." << endl;
    	cin.get();
    	return (0);
    }
    
    int main()
    {
    	using namespace std;
    	aI();
    	cout << "You're number doubled is...." << doubleNumber() << endl;
    	return (0);
    }
    
  • Grif

    Very much a beginner here, but there’s something I need a little clarification with – switching the order of “main()” and “add(x, y)” in the code seems to cause an error (I was trying to input it that way due to my own OCD wanting main to be up the top of the code).

    The result seemed to be that “main()” had no idea that “add(x, y)” even existed unless “add(x, y)” was assigned/created in code *before* “main()”.

    Is this always the case? That “function a” trying to call “function b” requires function b to be defined (and placed literally above function a’s code in the editor) before it’s run?

    • sjm71

      Yes, this is always the case. You can also use function prototypes before “main()” instead of writing the full funtion definitions and the write the funtion definitions after “main()”…

      #include

      //Function prototypes…
      int add(int, int); // NOTE: the semicolon (;) at the end!

      int main()
      {

      return 0;
      }

      int add(int x, int y)
      {
      return x + y;
      }

      Hope this helps?

  • sjm71

    Yes, this is always the case. You can also use function prototypes before “main()” instead of writing the full funtion definitions and the write the funtion definitions after “main()”…

    #include

    //Function prototypes…
    int add(int, int); // NOTE: the semicolon (;) at the end!

    int main()
    {

    return 0;
    }

    int add(int x, int y)
    {
    return x + y;
    }

    Hope this helps?

  • xPrezidential

    Here’s my code for quiz 4 and 5. Give me feedback on it, please. I just began C++ today.

    —————————————
    #include “stdafx.h”
    #include

    int doubleNumber( int x )
    {
    return x * 2;
    }

    int main( )
    {
    int integerToDouble = -1,
    theResult = -1;

    using namespace std;
    cout <> integerToDouble;
    cout << endl;

    theResult = doubleNumber( integerToDouble );

    cout << "The doubled value of " << integerToDouble << " is " << theResult << "." << endl;

    system( "PAUSE" );
    return 0;

  • Chris646

    Hey so i was trying to write this program where it says enter x for this equation and then you enter x and then it says you entered “x” and then it shows you the answer to the equation. I keep getting build errors though. Im using visual studios. Here is what i wrote for the program:

    // EquationNumberEnter.cpp : Defines the entry point for the application.
    //

    #include “stdafx.h”

    #include

    int Main()
    {
    using namespace std;
    cout <> x;
    cout << "You Entered " << x << endl;
    equation();
    return 0;
    }

    int multiply(int a, int b)
    {
    return a * b;
    }

    int add(int w, int y)
    {
    return w + y;
    }

    int equation()
    {
    using namespace std;
    cout << (5, multiply x) add 3 << endl;
    }

    Please tell me what I did wrong

  • Chris646
    Hi i tried to make to make a program where it says enter x for this equation 5x + 3 then you enter a number using cin and then it says You entered "x". Then it says the answer to the equation with the "x" you enetred. I keep getting build errors though. Im using viusal studios. Here is what I wrote for the code:
    // EquationNumberEnter.cpp : Defines the entry point for the application.
    //
    
    #include "stdafx.h"
    
    #include <iostream>
    
    int Main()
    {
    	using namespace std;
    	cout << "Enter x for 5x + 3 ";
    	int x;
    	cin >> x;
    	cout << "You Entered " << x << endl;
    	equation();
    	return 0;
    }
    
    int multiply(int a, int b)
    {
    	return a * b;
    }
    
    int add(int w, int y)
    {
    	return w + y;
    }
    
    int equation()
    {
    	using namespace std;
    	cout << add(3, multiply(5, x)) << endl;
    }
    
    Please tell me what I did wrong

You must be logged in to post a comment.