Jump to content

Hey I am having trouble with a C++ function.

 

The function I am coding takes in the value cost of an item then calculates the tip for the customer. 

 
In the function I made sure the parameters are given and what the function does. When I ran the code, it does not give me the expected output and consistently rounds the value up to the nearest single digit. 

float CalcTip(float item1,float tip15, float tip18, float tip20)
{
	float tip1;
	tip1 = (item1* tip15);
	return tip1; 
	float tip2;
	tip2 = (item1 * tip18);
	return tip2;
	float tip3;
	tip3 = (item1 * tip20);
	return tip3;
}

 

int main(){
 // return the value of tip here 
}

 

LTT CSGO SERVER! IP 8.12.22.45!~  Connect by connecting on csgo console

Use console command "connect"   --->  connect 8.12.22.45

Link to comment
https://linustechtips.com/topic/561206-c-function/
Share on other sites

Link to post
Share on other sites

20 minutes ago, KungPaoChino said:

Hey I am having trouble with a C++ function.

 

The function I am coding takes in the value cost of an item then calculates the tip for the customer. 

 
In the function I made sure the parameters are given and what the function does. When I ran the code, it does not give me the expected output and consistently rounds the value up to the nearest single digit. 


float CalcTip(float item1,float tip15, float tip18, float tip20)
{
	float tip1;
	tip1 = (item1* tip15);
	return tip1; 
	float tip2;
	tip2 = (item1 * tip18);
	return tip2;
	float tip3;
	tip3 = (item1 * tip20);
	return tip3;
}

 


int main(){
 // return the value of tip here 
}

 

Return stops the execution of the function so everything after "return tip1;" is not executed.

If you want to return 3 values you can either use reference arguments or return an std::vector.

Another option is to create 3 functions: CalcTip15, CalcTip18 and CalcTip20.

Desktop: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

Link to comment
https://linustechtips.com/topic/561206-c-function/#findComment-7383197
Share on other sites

Link to post
Share on other sites

14 minutes ago, mathijs727 said:

Return stops the execution of the function so everything after "return tip1;" is not executed.

If you want to return 3 values you can either use reference arguments or return an std::vector.

Another option is to create 3 functions: CalcTip15, CalcTip18 and CalcTip20.

In terms of memory, will these three functions eat up more memory than the function that calculates all three?

LTT CSGO SERVER! IP 8.12.22.45!~  Connect by connecting on csgo console

Use console command "connect"   --->  connect 8.12.22.45

Link to comment
https://linustechtips.com/topic/561206-c-function/#findComment-7383278
Share on other sites

Link to post
Share on other sites

#include <stdio.h>

void CalcTip(const float item1, const float tip15, const float tip18, 
	const float tip20, float*tip1, float*tip2, float* tip3)
{
	*tip1 = (item1 * tip15);
	*tip2 = (item1 * tip18);
	*tip3 = (item1 * tip20);
}

int main(void) {
	float tip1, tip2, tip3;
	
	CalcTip(0.2, 1, 2, 3, &tip1, &tip2, &tip3);
	printf("item1 = %f\t%f\t%f\t%f\n", 0.2, tip1,tip2,tip3);
	
	return 0;
}

 

Link to comment
https://linustechtips.com/topic/561206-c-function/#findComment-7383541
Share on other sites

Link to post
Share on other sites

1 hour ago, KungPaoChino said:

In terms of memory, will these three functions eat up more memory than the function that calculates all three?

Every function would use a bit less memory, but it doesnt matter at all. We're talking about 8 bytes (2 * 4 byte floats) which is nothing (1GB = 1 bilion bytes).

Desktop: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

Link to comment
https://linustechtips.com/topic/561206-c-function/#findComment-7383709
Share on other sites

Link to post
Share on other sites


 

Hey KingPaoChino,

The first thing you need to know is that your program will stop and return to main() the second it gets to the first "return" statement. So, you have quite a few different options to do what you're trying to do. The easiest, in my opinion, would be to the function with no return values, instead passing the arguments by reference instead of value. Although this may not be best practice. You see, when you write a function declaration like float CalcTip(float item1, ...), the arguments to the function are passed by value. This is the default for C++. Also, only a single return value is specified. In this case, float. What this means is that the program will copy all of those items into a new piece of memory (technically called a stack frame), and then let the function resume. If you instead do it by reference, C++ only passes a reference of that piece of memory, making the function much smaller. So, I will rewrite the function using reference values (which are denoted in the function definition with the & symbol):

 

#include <iostream>

void CalcTip(float &tip1, float &tip2, float &tip3, float item1, float tip15, float tip18, float tip20)
{
	tip1 = (item1* tip15); //tip1, declared in main(), is now equal to item1*tip15 
	tip2 = (item1 * tip18); //tip2, declared in main(), is now equal to item1*tip18
	tip3 = (item1 * tip20); //tip3, declared in main(), is now equal to item1*tip15
}

int main()
{
    const float FIFTEEN_PERCENT_TIP = 0.15; //It's generally good practice to declare constants like this, in all caps
    const float EIGHTEEN_PERCENT_TIP = 0.18; //This avoids what we call 'magic numbers'
    const float TWENTY_PERCENT_TIP = 0.20;
    float steakDinner = 20.5; //Expensive steak!
    float tip1 = 0;
    float tip2 = 0;
    float tip3 = 0;
    CalcTip(tip1, tip2, tip3, steakDinner, FIFTEEN_PERCENT_TIP, EIGHTEEN_PERCENT_TIP, TWENTY_PERCENT_TIP);
    std::cout << "A " << FIFTEEN_PERCENT_TIP*100 << "% tip on a $" << steakDinner << " item = $" << tip1 << std::endl;
    std::cout << "An " << EIGHTEEN_PERCENT_TIP*100 << "% tip on a $" << steakDinner << " item = $" << tip2 << std::endl;
    std::cout << "A " << TWENTY_PERCENT_TIP*100 << "% tip on a $" << steakDinner << " item = $" << tip3 << std::endl;
    return 0; //technically optional
}
    

After you make your way further into C++, learn to use the std::vector class. You could return one of these objects from your function instead of a single float, and then process the values from main that way. Another option would be a std::tuple.

Link to comment
https://linustechtips.com/topic/561206-c-function/#findComment-7392676
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×