4 Replies - 1027 Views - Last Post: 04 May 2013 - 07:47 AM Rate Topic: -----

#1 Hero26  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 03-May 13

Learning about functions and return values; run-time fail.

Posted 03 May 2013 - 06:24 PM

Hello.
This is my first post. The title says most of what's going on here.
If there is a way you could point me in the right direction without telling me the answer then that would be good.
One last thing. I havn't covered parameters yet (it's the next topic on my list). So if this can be solved without the use of those then that would also be good.
Thank you in advance for your help.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h> 
#include <math.h>

float InputRadius();
float CalculateVolume();

void main()
{
	float Volume;
	InputRadius();
	Volume = CalculateVolume();
	printf("Volume = %f\n", Volume);
	printf("Press enter to exit.");
	getch();
}
float InputRadius()
{
	float Radius = 0;
	printf("Enter Radius:");
	scanf("%f", &Radius);
	fflush(stdin);
	return (Radius);
}
float CalculateVolume()
{
	float Radius, Volume = 0, PI = 3.142;
	Volume = PI * Radius * Radius;
	return (Volume);
}



P.S. This is in c, not c++.

Is This A Good Question/Topic? 0
  • +

Replies To: Learning about functions and return values; run-time fail.

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Learning about functions and return values; run-time fail.

Posted 03 May 2013 - 06:30 PM

Quote

void main()
{
	float Volume;
	InputRadius();
	Volume = CalculateVolume();
	printf("Volume = %f\n", Volume);
	printf("Press enter to exit.");
	getch();
}



First off, it's always "int main", not "void main" in a host environment. You are calling InputRadius(), however you are not capturing the return value. You need to capture the return value inorder to use it to calculate the volume.
Was This Post Helpful? 0
  • +
  • -

#3 Zereo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 108
  • Joined: 08-November 12

Re: Learning about functions and return values; run-time fail.

Posted 03 May 2013 - 06:57 PM

What do you want to know about return values and functions? It is quite simple, and there is some great tutorials on here that can help out (They might be in C++, but that won't matter much since the concepts behind them is the same).

Also I would recommend you start learning about parameters because that is a major part of functions and without knowing how parameters work it is hard to know how to use functions effectively.

As for return values it is quite simple actually. When you declare a function you define what you want that function to return (You don't have to return anything). For example here is some function declarations.


// This will return a integer
int additionFunction();

// When you declare a void function you are saying
// that you aren't going to be returning anything.
void printFunction();

//This will return a double.
double multiplyFunction();


Now you might ask what they functions are actually returning? Well here is some examples to show what they are returning.

// Adds two numbers together
int addFunction( int a, int b )/>
{
    return a + b;
}


I know you don't know about parameters yet but all this function is doing is adding both a and b together (Both are integer numbers) and returning the result of that calculation. The parameter int a will be equal to whatever variable or value we pass it, and int b will be equal to whatever variable or value we pass it. For example here is how you could call this function.

// This will return the result of 5 + 10 since
// we pass the 5 to parameter a and 10 to parameter b
// so when the function calls return a + b; it is really saying
// return 5 + 10.
addFunction(5, 10);


You could also pass variables to the parameters like so

int main()
{
    int numberOne = 10;
    int numberTwo = 30;
    
    // Stores the result of the function in the variable sum.
    int sum = addFunction(numberOne, numberTwo);
}


Notice how we declare two integer variable (numberOne which hold a 10, and numberTwo which olds a 30). We then pass them variables to the parameters of our function. The function will then add them variables together and return the sum. We also must make sure we store or use that returned value or else we will lose it.

Hope that helps example things a bit and remember to check out the tutorials on functions in our tutorials section. If you need any other help or don't understand anything just let me know and I would be glad to help walk you through anything.

This post has been edited by Zereo: 03 May 2013 - 07:00 PM

Was This Post Helpful? 1
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon

Reputation: 5444
  • View blog
  • Posts: 16,969
  • Joined: 25-December 09

Re: Learning about functions and return values; run-time fail.

Posted 03 May 2013 - 07:46 PM

I suggest you study the function tutorials contained in my signature, they should help explain how to properly use functions.


Jim
Was This Post Helpful? 1
  • +
  • -

#5 Hero26  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 03-May 13

Re: Learning about functions and return values; run-time fail.

Posted 04 May 2013 - 07:47 AM

Thank you all for your help. I think I've got it.
I'll get started on learning about parameters now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1