user defined functions, what am I missing?

Hello. I began learning C++ several month ago and am currently enrolled in a class for beginners at my university. I code almost every day and have the basic concepts and syntax down but I seem to have hit a wall here.

I am required to make use of user-defined functions in one of my projects but I simply can not grasp this concept. I have tried to use a simple physics equation to figure out the correct syntax but with no luck.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

double speed;
double position;
double time;

int main()
{

cout << "input position (meters):  >  ";
cin >> position;


cout << "input time (seconds):  >  ";
cin >> time;

double speed (position, time);

cout << "speed is   ";
cout << speed;
cout << " m / s" << endl;

    return 0;
}

speed (position, time)
{
    using namespace std;
    double speed;
    speed = position / time;

    return speed;
}



When I attempt to compile this it simply says that 'speed' can not be used as a function. Any and all help is appreciated!

The definintion of the function is wrong. See:

http://www.cplusplus.com/doc/tutorial/functions/

for how to define functions.

Further more: You shouldn't name a function exactly like a [global] variable.
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

using namespace std; // better to not use, but ...

double speed (double, double);

int main()
{
    double velocity = 0;
    double position = 0;
    double time = 0;
    
    cout << "input position (meters):  >  ";
    cin >> position;
    
    cout << "input time (seconds):  >  ";
    cin >> time;
    
    velocity = speed (position, time);
    
    cout << "speed is   ";
    cout << velocity;
    cout << " m / s" << endl;
    
    return 0;
}

double speed (double aPosition, double aTime)
{
    //Should check aTime != 0
    double some_velocity = aPosition/aTime;
    
    return some_velocity;
}


input position (meters):  >  1
input time (seconds):  >  2
speed is   0.5 m / s
Program ended with exit code: 0
Last edited on
closed account (48T7M4Gy)
PS lines 21, 22 and 23 are OK as a single line
cout << "speed is " << velocity << " m / s" << endl;
Okay, I see that I was not defining my variables with in the main function, and my syntax was off. My professor prefers that we use namespace std; so I have not learned any other way. To elaborate my project requires me to use a switch statement to choose from a menu of non-zero integers.

This was very helpful, thank you kindly.
closed account (48T7M4Gy)
All good. Not using std comes a bit later. For your program you would use std::cout and std::endl instead. It makes for less chance of ambiguity.

If you need help with switches note that there are tutorials here on this site.
Topic archived. No new replies allowed.