Thread: Function

  1. #1
    Registered User Euclid365BCE's Avatar
    Join Date
    Jul 2015
    Location
    USA
    Posts
    33

    Function

    Code:
    /*Demonstrates local variables*/
    
    //C libraries
    #include <stdio.h>
    
    //Declare variables
    int x = 1, y = 2;
    
    //Declare user defined function
    void demo (void); //Used to print a character string
    
    //Main function
    int main (void)
    {
    printf ("\nBefore calling demo (), x = %d and y = %d.", x, y);
    demo();
    printf ("\nAfter calling demo (), x = %d and y = %d\n.", x, y);
    return 0;
    }
    
    //Demo function
    void demo (void)
    {
    /*Declares and initializes two local variables*/
    int x = 88, y = 99;
    /*Display their values*/
    printf ("\nWithin demo (), x = %d and y = %d.", x, y);
    }
    I am learning about functions for the first time, and the author states that the above void demo (void) function does not return any values; yet, in the int main (void) function the void demo (void) function prints the character string "Within demo (), x = 88 and y = 99.": is the printing of that character string not considered a return value, or does the author simply mean that it doesn't return a value that is used by another part of the program (e.g., return to another (not main) function; initialize a variable; etc...)?
    Last edited by Euclid365BCE; 09-23-2015 at 08:37 AM. Reason: Add (not main)

  2. #2
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107
    ... is the printing of that character string not considered a return value ...
    No it's not. Just because the demo function provides some form of feedback does not mean it returns a value, at least in programming terms.

    When a functions returns a value it should really return something. So when a function is declared as void demo( void ); you know it will never return a value, it will only return to the position it was called from.

    See the function below for an example of return values.
    Code:
    int sum( int x, int y )
    {
        return x + y;
    }
    
    int result = sum( 3, 6 );//variable result will get the value 9 after calling the function sum
    This parameter is reserved

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Euclid365BCE
    yet, in the int main (void) function the void demo (void) function prints the character string
    This kind of behaviour is called a side effect. More generally:
    Quote Originally Posted by C11 Clause 5.1.2.3 Paragraph 2a
    Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment.
    In this case you are effectively modifying a "file" (as represented by the standard output stream stdout) by printing a string, but you may have encountered functions that modify say, an array via a pointer to its first element.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User Euclid365BCE's Avatar
    Join Date
    Jul 2015
    Location
    USA
    Posts
    33
    Thank you both for the feedback.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function Prototype, Function Call, and Function definition
    By dmcarpenter in forum C Programming
    Replies: 9
    Last Post: 04-09-2013, 03:29 AM
  2. Replies: 11
    Last Post: 09-07-2012, 04:35 AM
  3. Replies: 13
    Last Post: 03-20-2012, 08:29 AM
  4. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM