I am in the midst of reading "C Primer Plus" by Stephen Prata. The 7th programming exercise of the 5th chapter wants you to "Write a program that requests a type double number and print the value of the number cubed. Use a function of your own design to cube the value and print it. The main() program should pass the entered value to this function."
What am I doing wrong? When I run this, I only get the same number I entered.
#include <stdio.h>
void cube(double n);
int main(void)
{
double a;
printf("Enter a Number: ");
scanf("%lf", &a);
cube(a);
printf("%lf", a);
return 0;
}
void cube(double n)
{
n = n * n * n;
}
cubeonly modifies its local copy of the value you pass it.main(), so all these considerations about returning values or passing a pointer are off-point.