Calling sh*t

Why won't double scan(double); call? Thanks for the help.

#include <stdio.h>

/*Function Prototypes*/
void directions(void); //Prints directions to user
double scan(double); //Scans choice

int main()
{
double S;
directions ();
double scan(double S);
}

/*Function Definitions*/

void
directions (void)
{
printf("Why won't this next function call?");
}

double
scan (double S)
{

scanf("%lf", &S);
printf("It scanned %0.1lf", S);
printf("It finally worked.");

}
1
2
3
4
5
6
int main()
{
    double S;
    directions ();
    double scan(double S); //this is a declaration, not a function call
}
You probably meant to write scan(S);
Also, this won't work like you want it to. You would need to have scan take the variable by reference.
What if I want to scan a variable, and pass it to another function? Thanks.
function parameters are usually for "input" (ie: something the function needs)

return values are usually or "output" (ie: something that the function gives back)

In this case, passing 'S' as a parameter to scan is pointless, because scan does not need any input. It gets the input from the user. On the other hand, scan is producing output (the data it got from the user) so it should be returning a value.


The much more logical way to do this is like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
double scan();  // no parameter, returns a double

int main()
{
  double S = scan();  // assign your 'S' variable to be the output of the scan function
  //...
}

double scan()  // again note:  no parameter
{
  double S;  // local variable
  scanf("%lf", &S);  // get it from the user

  //... do whatever else with it

  return S;  // return the value.  This returned value is what gets assigned to main's S above
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <cstdio>

void PrintDirections(void)
{
	printf("This program prints a double you wrote.\n\n");
	printf("Enter a double:  ");
}

double DoubleScan(void)
{
	char _ending;
        double _double;
	scanf("%lf%c",&_double,&_ending);
	return(_double);
}

void DoublePrint(double _double)
{
	printf("\n\nYou Entered: %lf",_double);
}

int main(void)
{
	double usernum;
	
	PrintDirections();
	usernum=DoubleScan();
	DoublePrint(usernum);

getchar();
return(0);
}
Last edited on
again... passing a double as input to 'DoubleScan' is pointless. That function does not need input. You shouldn't do that.
Fixed it, I understood what you were saying. Thank you Disch.
Topic archived. No new replies allowed.