0

So, I'm working on an assignment and my teacher didn't quite explain functions very well. I'll save some time and show the main segment where the error occurs:

 #include <stdio.h>
 6  int main(void)
 7  {
 8   double Depth;
 9   double celsius_at_depth(Depth);
10  {
11   10*(Depth)+20;
12  }

And the error:

GHP#4.c:9:2: warning: parameter names (without types) 
in function declaration [enabled by default]
double celsius_at_depth(Depth);
^

Sorry about formatting, I wanted it to be easier to see. isn't double supposed to be the parameter type for the function celsius_at_depth?

EDIT: I've looked up the error on the site, but I didn't quite see it in the same formatting as my code, so I felt it best to post anew

Brad Boisen
  • 1
  • 1
  • 1
  • 1
  • what is this line `double celsius_at_depth(Depth);` for? – Iharob Al Asimi Mar 26 '15 at 00:51
  • 1
    Are your intentions to have a "celcius_at_depth" function? You will need to define it outside of `main`. – DigitalNinja Mar 26 '15 at 00:53
  • @BinaryJudy: No, he'll need to *define* it outside `main`. It's probably best to declare it outside `main` as well, but local function declarations are permitted. (A function definition includes the `{ ... }`; a declaration doesn't.) – Keith Thompson Mar 26 '15 at 00:55
  • @KeithThompson the declarion is not a problem, but the code suggest that the OP is defining it inside `main()` – Iharob Al Asimi Mar 26 '15 at 00:55
  • 1
    "my teacher didn't quite explain functions very well." Then you need to read about them yourself in the text book. There is absolutely, positively, no point in fixing the immediate problem with this exercise if you do not understand functions one hundred percent. Functions are one of the most important abstractions in imperative languages, so you need to get very comfortable with them. If you are not satisfied with your teacher's explanation, read a book, and do a few exercises independently. – Sergey Kalinichenko Mar 26 '15 at 00:59
  • @iharob: Or trying to. It's hard to tell from the presented code what the actual intent is. – Keith Thompson Mar 26 '15 at 00:59
  • @dasblinkenlight I've looked into functions, and that's the format they were described with. – Brad Boisen Mar 26 '15 at 01:02
  • @BradBoisen in what book `K&R`? because you can do something similar to that, but not quite that, it's like `double function(parameter) double parameter; {}` but it's [obsolete](http://stackoverflow.com/a/3092074/1983495). – Iharob Al Asimi Mar 26 '15 at 01:05
  • @iharob that line was modified prior to my receiving this error. Although, looking at the line singled out as it is, I found the problem. 'Twas the semicolon at the end of the line. The line is no longer an issue, however, I'm going to take your advice, and move the functions outside of main. – Brad Boisen Mar 26 '15 at 01:07
  • Yes because it works as it's a gcc extension AFAIK, but I am quite sure that it's not standard. Also, without the semi colon, it would be a function definition but it doesn't return a value, I suggest using compiler warnings to prevent this things, and it will help you learn if you try to understand the warning message... With **gcc** which is what I use or **clang** just `gcc -Wall -Wextra -Wshadow -pedantic -Werror` will point out a lot of this kind of problems. – Iharob Al Asimi Mar 26 '15 at 01:08

2 Answers2

2

Defining a function inside another function is a non standard extension of gcc AFAIK, so it's not a good idea.

To declare the function you first need to move it outside main() so

double celsius_at_depth(double depth);

and then you can call it in main like this

#include <stdio.h>
int main(void)
{
    double depth = 10;
    printf("celsius_at_depth(%f) = %f\n", depth, celsius_at_depth(depth))
    return 0;
}

then the function definition

double celsius_at_depth(double depth);
{
    return 10 * depth + 20;
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
-2

just remove the data type , when calling the function ... so instead of , double celsius_at_depth(Depth); write celsius_at_depth(Depth);