C++ functions

A program contains the following function.
int cube(int num) {
return num * num * num; }
Write a statement that passes the value 4 to this function and assigns its return value to the variable result.

Could someone please guide me to the right direction? Please find my code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

// Function Prototype
int cube(int);

int main() {

    int result;
    result = cube(4);

    return 0;
}

int cube(int num) {
    return num * num * num;
}
Last edited on
It looks fine to me. The assignment doesn't say to output anything so you can drop the inclusion on Line 1 and the namespace on Line 2.
Thanks!
I was looking online and someone had a completely different answer so I thought I did it incorrectly.
To nit-pick, though there is nothing wrong with the code above, the OP asks:
Write a statement that passes the value 4 to this function and assigns its return value to the variable result.

To me that means you don't need an entire program. Just a single statement.
Last edited on
Topic archived. No new replies allowed.