calling functions and top down design

So I'm learning how to write code in a 'top down design' and call on functions as needed. First declare them, then define them after main function. I'm a bit confused on the syntax needed and where to declare variables...(globally or within functions and how they can move between functions) This is a short program I'm working on to practice but I'm getting a bit stuck at this point..

Thanks
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 // projecttopdownfunctions author bbqdev version 1.00x
//

#include "stdafx.h"
#include <iostream>
using namespace std;

void print_introduction();
// prints out information to tell user what the program does

double celsius_at_depth(double depth);
//computes and returns the celsius temperature at a depth measured in kilometers

double celsius_to_fahrenheit(double celsius_at_depth);
// converts a celsius temp to fahrenheit

void print_conclusion();
// display the conclusion of what temperature is in fahrenheit at that depth of earth

int main()
{
	// declare needed variables
	double depth;
	char user_response;
	

	// begin do while loop
	do {
		// print introduction by call print_introduction function
		print_introduction();

		//ask user to enter depth
		cout << "What is the depth in kilometers?\n";

		// get input
		cin >> depth;

		//print conclusion by call print_conclusion function
		print_conclusion();

		// ask user if they want to to do again
		cout << "Would you like to run the program again? Y or N?\n";

		// get user input
		cin >> user_response;

	} while (user_response == 'Y' || user_response == 'y');
}

// write the print_introduction function
void print_introduction() {

	cout << "This program will take a depth of the earth and tell you what the" << endl;
	cout << "temperature is at that depth in both celsius and fahrenheit." << endl;
	cout << endl;  cout << endl;
	
}

// function to calculate temp in celsius based on user depth input
double celsius_at_depth(double depth) {

	return ((10 * depth) + 20);
}

// function calculate fahrenheit temperature, converts celsius to fahrenheit
double celsius_to_fahrenheit(double celcius_at_depth) {

	double celsius_at_depth;
	return ((1.8 * celsius_at_depth) + 32);
}

// the conclusion printing function
void print_conclusion() {

	double depth;
	cout << "The temperature at " << depth << "is " << celsius_at_depth << " degrees Celsius," << endl;
	cout << "or " << celsius_to_fahrenheit << " degrees Fahrenheit.";
}
So many errors gah, in this case you probably want to make depth a global, also you refer to var outside of their functions when you have functions of the same name (pls name you var and param something different from each other)

I cleaned up the coding a little, and voila it worked fine. But please seriously don't name your variables and params the same name as your functions.


Functional example with changes
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 // projecttopdownfunctions author bbqdev version 1.00x
//


#include <iostream>
using namespace std;

double depth;

void print_introduction();
// prints out information to tell user what the program does

double celsius_at_depth(double depth);
//computes and returns the celsius temperature at a depth measured in kilometers

double celsius_to_fahrenheit(double celsius_at_depth);
// converts a celsius temp to fahrenheit

void print_conclusion();
// display the conclusion of what temperature is in fahrenheit at that depth of earth

int main()
{
	// declare needed variables
	char user_response;
	

	// begin do while loop
	do {
		// print introduction by call print_introduction function
		print_introduction();

		//ask user to enter depth
		cout << "What is the depth in kilometers?\n";

		// get input
		cin >> depth;

		//print conclusion by call print_conclusion function
		print_conclusion();

		// ask user if they want to to do again
		cout << "\nWould you like to run the program again? Y or N?\n";

		// get user input
		cin >> user_response;

	} while (user_response == 'Y' || user_response == 'y');
}

// write the print_introduction function
void print_introduction() {

	cout << "This program will take a depth of the earth and tell you what the" << endl;
	cout << "temperature is at that depth in both celsius and fahrenheit." << endl;
	cout << endl;  cout << endl;
	
}

// function to calculate temp in celsius based on user depth input
double celsius_at_depth(double depthh) {

	return ((10 * depthh) + 20);
}

// function calculate fahrenheit temperature, converts celsius to fahrenheit
double celsius_to_fahrenheit(double celcius) {

	return ((1.8 * celcius) + 32);
}

// the conclusion printing function
void print_conclusion() {

	cout << "The temperature at " << depth << "km is " << celsius_at_depth(depth) << " degrees Celsius," << endl;
	cout << "or " << celsius_to_fahrenheit(celsius_at_depth(depth)) << " degrees Fahrenheit.";
}
Ok. Thanks for the reply. I think I understand what you mean by " don't name your variables and params the same name as your functions."

I'm gonna go through each change and figure out what i screwed up, I'll be back with any more questions, again thanks!

Edit: Update, I think I've figured out what I wasn't getting, here's my updated version:
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// projecttopdownfunctions author bbqdev version 1.01x
//

#include "stdafx.h"
#include <iostream>
using namespace std;

double depth; // global variable

void print_introduction();
// prints out information to tell user what the program does

double celsius_at_depth(double depth);
//computes and returns the celsius temperature at a depth measured in kilometers

double celsius_to_fahrenheit(double celsius_at_depth);
// converts a celsius temp to fahrenheit

void print_conclusion();
// display the conclusion of what temperature is in fahrenheit at that depth of earth

int main()
{
	// declare needed variables
	
	char user_response;
	

	// begin do while loop
	do {
		// print introduction by call print_introduction function
		print_introduction();

		//ask user to enter depth
		cout << "What is the depth in kilometers?\n";

		// get input
		cin >> depth;

		//print conclusion by call print_conclusion function
		print_conclusion();

		// ask user if they want to to do again
		cout << "\nWould you like to run the program again? Y or N?\n";

		// get user input
		cin >> user_response;

	} while (user_response == 'Y' || user_response == 'y');
}

// write the print_introduction function
void print_introduction() {

	cout << "This program will take a depth of the earth and tell you what the" << endl;
	cout << "temperature is at that depth in both celsius and fahrenheit." << endl;
	cout << endl;  cout << endl;
	
}

// function to calculate temp in celsius based on user depth input
double celsius_at_depth(double celsius_temp) {

	return ((10 * depth) + 20);
}

// function calculates fahrenheit temperature, converts celsius to fahrenheit
double celsius_to_fahrenheit(double fahrenheit_temp) {

	
	return (1.8 * celsius_at_depth(depth) + 32);
}

// the conclusion printing function
void print_conclusion() {

	
	cout << "The temperature at " << depth << " is " << celsius_at_depth(depth) << " degrees Celsius," << endl;
	cout << "or " << celsius_to_fahrenheit(celsius_at_depth(depth)) << " degrees Fahrenheit.";
}


Is this more clear? Thanks
Last edited on
at 64 why are you using depth when you have the param celsius_temp?

and at 71 why use the celsius_at_depth function when you have the param for input and use it in line 79 as the input?

When you create a function with a param, you want to use the param you made for it.

When doing top down design, the point is that you connect the functions properly.

For example this is the top down design of 3 depth i get from this
1
2
3
4
5
6
7
8
9
10
11
12
13
*Say what the temperature at the depth is
    1. Get the depth form the user
        1. Tell the user what the program does
        2. Get input from the user on the top down design
    2. Calculate what the temperature at the depth is
        1. Find the temperature in celsius
        2. Find the temperature in fahrenheit
    3. Tell the user the temperature
        1. Tell the user what they originally input
        2. Tell the user the calculated temperatures
    4. Repeat if the user wants to know another temperature
        1. Find out if user wants to repeat
        2. If they wan to repeat, then repeat


This shows that I want 4 things happening in main at least, with those 4 things each doing 2 other things. Some of these 4 may be functions (calculate and tell), some may be built in commands (repeat = while). But you need to figure out how to connect them properly.

Sorry if I come off as an a**-hole, but I want to help you out by helping you think about this better (you did a good job so far with those functions, just think more about what they are doing when you code them).
Ha! You can use all caps and yell at me if you'd like, I appreciate feedback and criticism any way I can get it, especially from those patient enough to help a new learner out.

Ok, I get what you are saying, I will work on it and post an update, probably not tonight unfortunately.
Topic archived. No new replies allowed.