Thread: Void return type

  1. #1
    Registered User Spectrum48k's Avatar
    Join Date
    May 2002
    Posts
    66

    Question Void return type

    when defining this function the author of
    the program uses void instead of int, float etc...
    what exactly does this mean. is it because it doesnt
    return a value, but performs a copy task ? it actually
    copies a character array (string) to the screen.

    void copy(char to[], char from[]);

  2. #2
    Unregistered
    Guest
    sounds good.

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    You don't absolutely need to return anything from the function.
    You can handle all the errors inside the function yourself.
    This way, it's a completely working function, and all you have to do is stick it into your code, and presto.
    If the rest of the program relies on knowing what happened inside that function, then yes, you would need return values.

    I'm curious to see the code though.

    when defining this function the author of
    the program uses void instead of int, float etc...
    what exactly does this mean. is it because it doesnt
    return a value
    Correct.
    The world is waiting. I must leave you now.

  4. #4
    Registered User Spectrum48k's Avatar
    Join Date
    May 2002
    Posts
    66

    Code for -Shadow-

    it is an example from "The C Programming Language" book. It returns and prints out the longest line (sentence).

    #include <stdio.h>
    #define MAXLINE 1000
    /*maximum input line size*/

    int getline(char line[], int maxline); /* definef function */
    void copy(char to[], char from[]); /* define function */

    /* print longest input line */

    main()
    {
    int len; /* current line length*/
    int max; /*max length seen so far*/
    char line[MAXLINE]; /* CHAR array max 1000*/
    char longest[MAXLINE]; /*CHAR array max 1000 */

    max = 0;
    while ((len = getline(line, MAXLINE)) > 0 )
    if (len > max) {
    max = len;
    copy(longest, line);
    }
    if (max > 0 ) /* there was a line */
    printf("%s", longest);
    return 0;
    }

    /* getline: read a line into s, return length */

    int getline(char s[],int lim)
    {
    int c, i;

    for (i=0; i<lim-1 && (c=getchar()) !=EOF && c!='\n'; ++i)
    s[i] = c;
    if (c == '\n') {
    s[i] = c;
    ++i;
    }
    s[i] = '\0';
    return i;
    }

    /*copy: copy 'from' into 'to'; assume to is big enough*/

    void copy(char to[], char from[])
    {
    int i;

    i=0;
    while ((to[i] = from[i]) != '\0')
    ++i;
    }

  5. #5
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Hm, interesting. Next time, use code tags though.

    Are you learning this program...or, just curious?
    If you're just curious, refer to my explanation above, and whatever anybody else wants to add onto it with.

    And I just used a dangling sentance.
    The world is waiting. I must leave you now.

  6. #6
    *
    Guest
    The reason you use the keyword 'void' is twofold:

    1) Primarily to tell the compiler that when it builds the assembly code for the function that it does not need to create stackspace for a return argument.

    2) Secondarily because of language syntax, a keyword is required before the name of the function/procedure. So if there isn't a valid return value, you still have to use some keyword,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  2. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 04:14 AM
  3. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  4. msvc just ate one of my source files
    By Eber Kain in forum C++ Programming
    Replies: 6
    Last Post: 07-01-2004, 05:40 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM