Function errors: Copying one function to create another.

In summary, the conversation discusses creating a new function called KelvinToCelsius by modifying the existing function CelsiusToKelvin. The issue being discussed is understanding how this conversion works and how to change the function accordingly. The final code given is a possible solution for the KelvinToCelsius function.
  • #1
Teh
47
0
I am having difficulties understanding this program may someone help me please.

Using the CelsiusToKelvin function as a guide, create a new function, changing the name to KelvinToCelsius, and modifying the function accordingly.
Code:
#include <iostream>
using namespace std;

double CelsiusToKelvin(double valueCelsius) {
   double valueKelvin = 0.0;

   valueKelvin = valueCelsius + 273.15;

   return valueKelvin;
}

/* Your solution goes here  */

/* ^ Your solution goes here ^ */

int main() {
   double valueC = 0.0;
   double valueK = 0.0;

   valueC = 10.0;
   cout << valueC << " C is " << CelsiusToKelvin(valueC) << " K" << endl;

   valueK = 283.15;
   cout << valueK << "  is " << KelvinToCelsius(valueK) << " C" << endl;

   return 0;
}
output: main.cpp: In function ‘int main()’:
main.cpp:24:55: error: ‘KelvinToCelsius’ was not declared in this scope
 
Technology news on Phys.org
  • #2
Where are you having difficulty? Do you know you need to declare a function

[m]double KelvinToCelsius(double valueKelvin) {. . .}[/m]​

?
 
  • #3
greg1313 said:
Where are you having difficulty? Do you know you need to declare a function

[m]double KelvinToCelsius(double valueKelvin) {. . .}[/m]​

?
The issue I have was to understand how this works
if i was to change the function to KelvinToCelsius
Code:
double CelsiusToKelvin(double valueCelsius) {
   double valueKelvin = 0.0;

   valueKelvin = valueCelsius + 273.15;

   return valueKelvin;
}

- - - Updated - - -

Teh said:
The issue I have was to understand how this works
if i was to change the function to KelvinToCelsius
Code:
double CelsiusToKelvin(double valueCelsius) {
   double valueKelvin = 0.0;

   valueKelvin = valueCelsius + 273.15;

   return valueKelvin;
}

This what I got so far and thanks for the help

Code:
double KelvinToCelsius (double valueKelvin) {
   double valueCelsius = 0.0;
   
   valueCelsius = valueCelsius + 273.15;
   
   return valueCelsius;

}
output:

Testing with valueKelvin = 283.15
Expected value: 10
Your value: 273.15
 
  • #4
If you solve [m]valueKelvin = valueCelsius + 273.15[/m] for [m]valueCelsius[/m], what do you get?
 

Related to Function errors: Copying one function to create another.

1. What is the purpose of copying one function to create another function?

Copying one function to create another allows for the reuse of code and saves time and effort in writing new code from scratch. It also allows for modifications to be made to the copied function without affecting the original function.

2. How do you copy a function to create another one?

To copy a function, you can use the function declaration or function expression syntax and assign it to a new variable. For example, const myNewFunction = myOriginalFunction; This creates a new function using the same code as the original one.

3. Can you modify the copied function without affecting the original one?

Yes, since the copied function is now a separate entity, any modifications made to it will not affect the original function. However, if the copied function references any external variables or objects, those may still be affected.

4. Are there any potential errors that can occur when copying a function?

Yes, there are a few potential errors that can occur when copying a function. One is if the function relies on any external variables or objects that are not available in the new context. Another is if the copied function is nested within another function, it may not have access to the same variables or objects as the original function.

5. How can you avoid errors when copying a function?

To avoid errors when copying a function, it is important to understand the scope and context of the original function and make sure that any necessary variables or objects are also accessible in the new context. It is also helpful to test the copied function thoroughly to ensure it is functioning as intended.

Similar threads

  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
966
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
Back
Top