Thread: functions

  1. #1
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534

    functions

    [edit]

    Been doing some research and my page needs some work. Thanks for the input though. I appreciate it.

    [/edit]
    Last edited by kermit; 08-31-2003 at 07:02 PM.

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Hmmm... I think it's great when people write tutorials, so just consider the following to be me proof reading for you

    This is not to say that there are not many programmers who need no clarification on functions.
    ackward/confusing wording

    Explain WHAT a function is before you explain how to use it.
    return-type function-name ( parameter declarations, if any)
    {
    declarations
    statements
    }1
    You might want to consider throwing in a return after the statements.

    If a function has no parameters when it is defined, then the keyword void should be used. For example,

    int main()

    Here we have the main function with no parameters given. This will work, but to be correct, it should be written

    int main(void)
    Not entirely accurate. Only declare it with a void in C (well, I guess this is on the C board, but people will probably read it for C++ too), or when you want that function to ONLY take 0 parameters.

    Every C program has at least one function, that being the main() function. Here is an example of a C program with a main function:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    
         printf( "Hello, cruel world..." );
    
         return 0;
    } 
    //lol, the board made me put this in code tags
    Windows programs don't have main, they have winmain. They can also be written in C.

    as main does not always return an int.
    It doesn't?

    We have already discussed that main() can return different types, the default being int, but it can also return a double or a float etc.
    I could be wrong, but I thought that the operating system expected an int? You might want to just say that other functions can return floats, doubles, pointers, etc. I've never seen main() return a float. (Someone correct me if I'm wrong here)

    Anyway, it looks pretty good. Revise it and fix up the formatting and stick it on a site You might write some other tutorials as well, if it helps you learn or something.
    Away.

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    you're not wrong the OS or whatever calls the program looks for an integer as a return type. There is a table of integers defined somewhere in the calling program/OS that defines different return codes for example
    I could write a program to call a hello world program and if hello world could not be output I could return a -1 instead of 0 and I could write my program to interpret the -1 as a segmentation fault error or something. (However, segmentation faults are usually signals sent to the program that causes it to abort())

    however the standard (I believe) desires the return value on a successful run to be 0

    main could return a double, float etc. but it would be truncated and the behavior is UNDEFINED. The standard says that main should return an int, this has been discussed so many times its giving even me a headache when people still post void main questions. At least he didn't write void main anywhere and say it was the right way! ^_^

    If salem saw it he would probably delete the post and replace it with a big SIGH.

    I am not sure but I believe the return value is popped into the stack and read off the stack when the program exits but I could be completely wrong but I think I heard that somewhere before, If I learn of differnet or if someone corrects me I'll edit this post.

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534


    The reason I wanted you guys to look at my page was for the critique. I learn a lot of stuff that way. You see now I can go back and fine tune some things. It is very helpful for me, and in the end I am hoping it will help others. I have spent a lot of time looking around here and there on the web for the kind of things that you guys are pointing out to me now. I have not found all the answers, obviously. Anyway, this is good. I will go back and rework some stuff. I am aware that besides tweaking some of the information, that some of it needs to be rewritten so that it is easier to understand. I am not a writer, I'm a carpenter - heh.

    Thanks for your input

    kermit

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Originally posted by Lynux-Penguin

    main could return a double, float etc. but it would be truncated and the behavior is UNDEFINED. The standard says that main should return an int, this has been discussed so many times its giving even me a headache when people still post void main questions. At least he didn't write void main anywhere and say it was the right way! ^_^

    If salem saw it he would probably delete the post and replace it with a big SIGH.

    -LC
    Ok, will have to check back on some things about the value that int can return. I am almost certain that I read somewhere that you could return a double, but I gambled when I wrote you could return a float. lol... I knew that somebody would ferret the truth out. I guess that is bad, but its done now.

    ergh...out of room here...

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Originally posted by Lynux-Penguin
    At least he didn't write void main anywhere and say it was the right way! ^_^

    If salem saw it he would probably delete the post and replace it with a big SIGH.
    -LC
    There is actually a dude with an avatar that is against int main - he promotes void main - heh. I don't use void main.

    What is a SIGH?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM