NO

Author Topic: Very Basic Question About Function Definitions RESOLVED  (Read 9744 times)

Honduras 2811

  • Guest
Very Basic Question About Function Definitions RESOLVED
« on: November 11, 2013, 12:12:47 AM »
The first application program I bought for the first computer I built, in 1987, was Borland Turbo C 1.0. I think it came on 1 x 3.5" diskette then.

Now times have changed, computers have changed, and I haven't been keeping up with it for more than 10 years.

So I decided to get back into programming, as therapy, and eventually ran across Pelles C. I love it.

Naturally, on mu second tutorial program I run across something that should be very simple, but an hour searching the web doesn't give me what I need. Then I ran into these forums. I didn't even know there was a Pelles forum.

Anyway, please cut me a little slack because I'm old. Dirt hadn't been invented when I was born.

The problem is how to do a function declaration now.

Code: [Select]
int hello(void,void);

hello()
{
printf("Hello\n");
return 0;
}

The 'hello' function is called by 'main', so it isn't that. After flailing around for a while I got the number of Build error messages down to 3:

C:\Users\Documents\Pelles C Projects\T2\main.c(14): error #2040: Invalid formal parameter type.
C:\Users\Documents\Pelles C Projects\T2\main.c(27): warning #2099: Missing type specifier; assuming 'int'.
C:\Users\Documents\Pelles C Projects\T2\main.c(35): warning #2117: Old-style function definition for 'hello'.
*** Error code: 1 ***

The code above is kind of what I thought a function definition should look like, but . . .

What am I doing wrong? I looked at C11 and the example it uses is way over my head.

Also, ordinarily I would not return a value from 'hello', but I think that made a few error messages go away. Is it mandatory now that a function return a value?
« Last Edit: November 14, 2013, 12:25:40 AM by Honduras 2811 »

palle

  • Guest
Re: Very Basic Question About Function Definitions
« Reply #1 on: November 11, 2013, 02:24:27 AM »
hi,
two things:

always declare a function with return type:
int f(); // returns an int
char f(); returns a char
void f(); returns void/nothing - used when function returns nothing

in your example the function has no return type but still returns an int of value zero.

edit: function prototype is different from implementation, you forgot to put "int" in front. also your function does not need to be of type int since all it does is to print or use printf(). the type void is better suited.


« Last Edit: November 11, 2013, 02:31:28 AM by palle »

CLR

  • Guest
Re: Very Basic Question About Function Definitions
« Reply #2 on: November 11, 2013, 02:28:34 AM »
Code: [Select]
void hello(void) {
    printf("Hello\n");
}

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Very Basic Question About Function Definitions
« Reply #3 on: November 11, 2013, 04:37:32 AM »
The first application program I bought for the first computer I built, in 1987, was Borland Turbo C 1.0. I think it came on 1 x 3.5" diskette then.
<snip>
Now times have changed...
Indeed...
Quote
So I decided to get back into programming, as therapy, and eventually ran across Pelles C. I love it.
Great to hear that...
Quote
Naturally, on mu second tutorial program I run across something that should be very simple, but an hour searching the web doesn't give me what I need. Then I ran into these forums. I didn't even know there was a Pelles forum.

Anyway, please cut me a little slack because I'm old. Dirt hadn't been invented when I was born.

The problem is how to do a function declaration now.

Code: [Select]
int hello(void,void);

hello()
{
printf("Hello\n");
return 0;
}

The 'hello' function is called by 'main', so it isn't that. After flailing around for a while I got the number of Build error messages down to 3:

C:\Users\Documents\Pelles C Projects\T2\main.c(14): error #2040: Invalid formal parameter type.
C:\Users\Documents\Pelles C Projects\T2\main.c(27): warning #2099: Missing type specifier; assuming 'int'.
C:\Users\Documents\Pelles C Projects\T2\main.c(35): warning #2117: Old-style function definition for 'hello'.
*** Error code: 1 ***

The code above is kind of what I thought a function definition should look like, but . . .

What am I doing wrong? I looked at C11 and the example it uses is way over my head.
Well, it's not C11 that is causing your problems, but that you didn't learn proper ANSI C even back in the days (yes, that existed already in 1987, official C standard since '89).

And that is that you need to do away with being, a "lazy" programmer. It's one of the first things I teach people when showing them how to program, You simply need to properly define the function, and subsequently the return value of a function.

By default (and that applies also to K&R C), everything is a function in C and function that are not intended to return a value need to be explicitly declared as "void" (the equivalent of a "procedure" in Pascal).
The main() function is defined to always return a value and needs to be declared as int, with a proper return value ('0') to indicate normal program termination.

So your little test should look like this
Code: [Select]
#include <stdio.h>

void hello(void) // void as you just want to declare a procedure rather than a function returning a value, (void) as this function doesn't take any parameters
{
printf("Hello\n");
}

int main (void)
{
    hello ();
   
    return (0); // 0 (zero) indicates normal/intended program termination in most OS
}

Honduras 2811

  • Guest
Re: Very Basic Question About Function Definitions
« Reply #4 on: November 11, 2013, 04:38:18 PM »
It's funny that When I started out the examples in the tutorials seem to have always started with just 'main()' After I studied 'main' for a while I would automatically type in 'void main(void)' (if appropriate) and I could get away with it then. I had heard that the requirement for 'main' had changed so that it always returned something, but wasn't sure until I read your responses.

I guess I didn't make myself clear when I described the problem. I'm getting the error messages from the function definition and not the function. Maybe I should call it the 'parameter declaration.'

My reference book, which is "Using Turbo C Second Edition" covering Turbo C 2.0, is really confusing me. It explains the difference between the 'Classic' parameter declaration statement and the ANSI version but it's a little vague about which code block is which type.

I'm going to assume that, when it says

Code: [Select]
f(type varname1, type varname 2, type varnameN)
That appears to be a fairly accurate example of the ANSI standard, but later on it says:

Quote
For example, this modern declaration

Code: [Select]
float f(int a, int b, char ch)
{   
   .
   .
   .
}

This is where it gets confusing. Is the second example ANSI standard? I assume that something is supposed to be on the three lines that just have a '.' in them. Does that mean, if he expanded it, the ANSI standard says it should look like this?

Code: [Select]
float f(int a, int b, char ch)
{
   int a;
   int b;
   char ch;
}

I really appreciate your help, and you can look forward to more questions on this level from me. :)

Offline DMac

  • Member
  • *
  • Posts: 272
Re: Very Basic Question About Function Definitions
« Reply #5 on: November 11, 2013, 06:17:33 PM »
They are both the same.

int is a type, as well as char and float.

What's going on is that when you declare the function f(varType varName)

The compiler knows that whatever value is used in the function call should be of the declared type and will interpret it as such.

As for the contents of the function.  The variables declared in the argument list need not be declared again and in fact should not.  They can be used, copied, or whatever and the result of computation returned by the return statement.

Other local variables should be declared within the function as needed.
« Last Edit: November 11, 2013, 06:25:36 PM by DMac »
No one cares how much you know,
until they know how much you care.

palle

  • Guest
Re: Very Basic Question About Function Definitions
« Reply #6 on: November 11, 2013, 06:30:56 PM »
Code: [Select]
float f()
{
    float a;
    a=10/3;
    printf("%f\n",a);
}

or declare a and b in main() and call f(int a, int b):

int a=10, int b=3;
.
.
printf("%f\n",f(a,b));

the function you are calling should have correct prototype and implementation:
float f(int a, int b);

// I leave the implementation as an excercise for you.
// (hint: you need to convert int values to float)
« Last Edit: November 11, 2013, 06:36:24 PM by palle »

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Very Basic Question About Function Definitions
« Reply #7 on: November 11, 2013, 10:41:23 PM »
It's funny that When I started out the examples in the tutorials seem to have always started with just 'main()' After I studied 'main' for a while I would automatically type in 'void main(void)' (if appropriate) and I could get away with it then. I had heard that the requirement for 'main' had changed so that it always returned something, but wasn't sure until I read your responses.
Well, the bad thing about most C tutorials/manuals back then was that they kept referring to the at this point already obsolete "K&R C" while ANSI C standardization started in 1983 and was first published as "C89", well in 1989...
I started with C sometime in the late 1970's on Apple DOS and CP/M, and all those compilers were not (yet) ANSI C. But as soon as the ANSI C proposal came out in the early 1980's, I immediately switched to using it, as it simply made more sense and resulted in clearer code. That was also one of the reasons why I switched back in the early DOS days from DeSmet C to Turbo C, as the later not only had a nicer IDE but also already understood what would become ANSI C notation.
Quote
I guess I didn't make myself clear when I described the problem. I'm getting the error messages from the function definition and not the function. Maybe I should call it the 'parameter declaration.'
Well, the there is that you only posted part of your source, not the whole thing. It seems you are making two different declarations, to get around the odd habit of (old) C to declare every thing that isn't already explicitly declared as int. The main point is that those declarations need to be of the same "style", not using K&R in one place and ANSI in the other...
Quote
My reference book, which is "Using Turbo C Second Edition" covering Turbo C 2.0, is really confusing me. It explains the difference between the 'Classic' parameter declaration statement and the ANSI version but it's a little vague about which code block is which type.
Not sure what so confusing about this (I don't have any manuals before Turbo C(++) 3.0 anymore).
You should simply define each parameter "in place" in ANSI C, there aren't actually different code blocks anymore.
Or you need to post some more comprehensive examples of what you mean to make things more clear...
Quote
I'm going to assume that, when it says

Code: [Select]
f(type varname1, type varname 2, type varnameN)
That appears to be a fairly accurate example of the ANSI standard, but later on it says:

Quote
For example, this modern declaration

Code: [Select]
float f(int a, int b, char ch)
{   
   .
   .
   .
}

This is where it gets confusing. Is the second example ANSI standard? I assume that something is supposed to be on the three lines that just have a '.' in them. Does that mean, if he expanded it, the ANSI standard says it should look like this?
yes, that would be ANSI C notation, and well, those lines with the "." in them, that simply would be the code that actually presents your function "f"
Quote
Code: [Select]
float f(int a, int b, char ch)
{
   int a;
   int b;
   char ch;
}
Sorry, but that code doesn't make any sense. First of all, there is no "code" and there is no need to re-declare the parameters as you did this already in the declaration of the function f() already...

Ralf

Honduras 2811

  • Guest
Re: Very Basic Question About Function Definitions
« Reply #8 on: November 12, 2013, 04:19:21 AM »
Ok. Lets start all over again.

Code: [Select]
#include <stdio.h>

void hello(void)       /* This is supposed to be the parameter declaration.*/
{
}


int main(void)
{
    /* TODO: Enter code here */
hello();
        return 0;
}

hello()
{
printf("Hello\n");
}

I added the function definition because the first time I ran it I got a message saying that the function was not declared.

This what I get now when I try to run it.

Building C:\Users\Rory Starkweather\Documents\Pelles C Projects\T2\output\main.obj.
C:\Users\Rory Starkweather\Documents\Pelles C Projects\T2\main.c(36): warning #2099: Missing type specifier; assuming 'int'.
C:\Users\Rory Starkweather\Documents\Pelles C Projects\T2\main.c(37): warning #2117: Old-style function definition for 'hello'.
C:\Users\Rory Starkweather\Documents\Pelles C Projects\T2\main.c(37): error #2123: Redefinition of 'hello', previously defined at C:\Users\Rory Starkweather\Documents\Pelles C Projects\T2\main.c(14).
C:\Users\Rory Starkweather\Documents\Pelles C Projects\T2\main.c(37): error #2120: Redeclaration of 'hello', previously declared at C:\Users\Rory Starkweather\Documents\Pelles C Projects\T2\main.c(14); expected 'void __cdecl function(void)' but found 'int __cdecl function()'.
*** Error code: 1 ***
Done.

To keep things simple, how do I fix that?



Honduras 2811

  • Guest
Re: Very Basic Question About Function Definitions
« Reply #9 on: November 12, 2013, 04:30:34 AM »
So I tried this. Notice no function definition, but I get an error anyway.

Code: [Select]
#include <stdio.h>
 
int main(void)
{
    /* TODO: Enter code here */
hello();
    return 0;
}

void hello(void)
{
printf("Hello\n");
}


This is what I get:
Building C:\Users\Rory Starkweather\Documents\Pelles C Projects\T2\output\main.obj.
C:\Users\Rory Starkweather\Documents\Pelles C Projects\T2\main.c(28): warning #2018: Undeclared function 'hello'; assuming 'extern' returning 'int'.
C:\Users\Rory Starkweather\Documents\Pelles C Projects\T2\main.c(33): error #2120: Redeclaration of 'hello', previously declared at C:\Users\Rory Starkweather\Documents\Pelles C Projects\T2\main.c(28); expected 'int __cdecl function()' but found 'void __cdecl function(void)'.
*** Error code: 1 ***
Done.


The whole program is less than 10 words long. Surely there is a way to get it to run.

palle

  • Guest
Re: Very Basic Question About Function Definitions
« Reply #10 on: November 12, 2013, 10:46:32 AM »
in your code you have no function prototype or declaration, you can make it work by putting the hello() function before the main function - for a small program like this it will work.

BUT in real programs you should always use function prototype (usually at top of your code), ie:

Code: [Select]
#include <stdio.h>

void hello(void); // function prototype

int main(void) {
  hello(); //function call
  return 0;
}

//function implementation
void hello(void) {
  printf("hello");
}

Honduras 2811

  • Guest
Re: Very Basic Question About Function Definitions
« Reply #11 on: November 12, 2013, 01:15:32 PM »
Now you're getting it. That's what I'm trying to find out. What is the ANSI Standard for function declarations?

The program I ran into trouble with is a bad example because the only variable passed is the meaningless int from the 'main' function. That float garbage was to explain why I was having trouble with the book.

Once again: What is the ANSI standard for defining functions?

palle

  • Guest
Re: Very Basic Question About Function Definitions
« Reply #12 on: November 12, 2013, 02:36:51 PM »
Oh now I get it!

You are probably more familiar with the "K&R-style " or "old-style" function definition and want  to know how it's done today, right?

K&R-/old-style
Code: [Select]
double cylinderVolume( r, h )
double r, h;                            // Parameter declarations.
{
   const double pi = 3.1415926536;      // Pi is constant.
   return  pi * r * r * h;
}

new-style (as I have learned)
Code: [Select]
double cylinderVolume( double r, double h )
{
   const double pi = 3.1415926536;      // Pi is constant.
   return  pi * r * r * h;
}

and the declaration (or protoype or definition - same thing different words) should be:

Code: [Select]
double cylinderVolume( double r, double h ); // at the top of your code as mentioned in previous post
« Last Edit: November 12, 2013, 02:44:40 PM by palle »

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Very Basic Question About Function Definitions
« Reply #13 on: November 12, 2013, 09:17:21 PM »
Ok. Lets start all over again.

Code: [Select]
#include <stdio.h>

void hello(void)       /* This is supposed to be the parameter declaration.*/
{
}


int main(void)
{
    /* TODO: Enter code here */
hello();
        return 0;
}

hello()
{
printf("Hello\n");
}

I added the function definition because the first time I ran it I got a message saying that the function was not declared.
That is correct, as you define the function twice, complete (ok, the first one doesn't do anything).

Everything in C has to be declared before being used, that is correct. So if you do not want to put the main() function at the very end of your source, you just have to do a forward declaration, not define the whole function twice...

So the first declaration should only be
Code: [Select]
void hello (void);with no code block (as denoted by the curly braces). This is the same way you enter declarations in a header file. They only declare the function return value and it parameters (and their type), but no code.

Ralf
« Last Edit: November 12, 2013, 09:20:19 PM by Bitbeisser »

Honduras 2811

  • Guest
Re: Very Basic Question About Function Definitions
« Reply #14 on: November 14, 2013, 12:23:48 AM »
Thank you all very much for being so patient. I have exactly what I need now to continue the tutorial. (I guess the author left out the function definitions, or any mention that he had.)

I don't understand why Pelles C isn't making a much bigger footprint in the market. Is it because ancient programmers don't want to leave the command like behind? Pelles C has been a very positive experience for me.