1. ## Functions Explained

I dunno if someone wants to take the time but I would be glad, I got this code from Sams Teach Yourself C Programming in 21 Days:

Code:
```#include <stdio.h>

long cube(long x);

int main()
{
printf("Enter an interger value: ");
scanf("%ld", &input);

printf("\nThe cube of %ld is %ld.\n\n", input,answer);

return 0;
}

long cube(long x)
{
long x_cubed;

x_cubed=x*x*x;

return x_cubed;
}```
Now I get most of this but what I don't understand is when the program sends whatever the user entered to the function how does all of that work. Because it says 'long cube(long x)' and in the main part it has 'cube(input)'. I get confused when I look at the fuction because it is returning x_cubed, it uses only the variable x, so I don't see how or where input comes into play here or what they point of x is...I know this is a beginner question but so am I, thanks.

2. ## Re: Functions Explained

Originally posted by CAP
I dunno if someone wants to take the time but I would be glad, I got this code from Sams Teach Yourself C Programming in 21 Days:

Code:
```#include <stdio.h>

long cube(long x);

int main()
{
printf("Enter an interger value: ");
scanf("%ld", &input);

printf("\nThe cube of %ld is %ld.\n\n", input,answer);

return 0;
}

long cube(long x)
{
long x_cubed;

x_cubed=x*x*x;

return x_cubed;
}```
Now I get most of this but what I don't understand is when the program sends whatever the user entered to the function how does all of that work. Because it says 'long cube(long x)' and in the main part it has 'cube(input)'. I get confused when I look at the fuction because it is returning x_cubed, it uses only the variable x, so I don't see how or where input comes into play here or what they point of x is...I know this is a beginner question but so am I, thanks.
"long x" is only in the scope of the cube() function. the variable x doesn't exist outside of it. in order to call cube() you just have to pass a long to it, it doesnt matter what the variable name is.

it is the same thing with x_cubed; it is only in the the scope of cube(). cube() returns a long, you assign that long to whatever variable you want.

3. ## hmm

to keep it short and simple :

long cube(blah blah blah)

"long" is the type of the return value of that function

long cube(long x)
this "long" is the type of the argument passed to the function.

long x_cubed;
you declared a variable of type long and did something to it
later on (i.e. x_cubed = x * x * x)

return x_cubed

you're returning the value of x_cubed from the function

and finally...

that value is being taken over by the variable "answer"
which is also of type "long"

it's as simple as that.

>I get confused when I look at the fuction because it is returning x_cubed, it uses only the variable x, so I don't see how or where input comes into play here or what they point of x is

input came into play when you were using it to assign a
value to your x_cubed variable.... you said x_cubed = x * x * x
... the value of "x" is the value of the variable sent to the
function (i.e. input)
just think of a function argument as a box that can accomodate
stuff given to it by the function call
so, by saying
cube(input) ... you're sending "input" variable into that "box - x"
of that function ... which will later process it
(multiply/divide/whatever) and then return a value ...
now, this is a very raw way of explaining it, but i hope you
got the gist of what i said.

GOOD LUCK

4. Ok then, so the whole long x, x_cubed and so on and so on is pretty much like a frame that defines how the function works, but when you put input it, input takes over and just runs it course...ok seems simple enough, this is chapter 5 and it said that scopes and local variable are on chapter 12 or something so thanks for the heads up