Beginner question about the Return statement

This is a discussion on Beginner question about the Return statement within the C Programming forums, part of the General Programming Boards category; Before I continue on with my lessons in C, I want to make sure I understand the use of the ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    28

    Beginner question about the Return statement

    Before I continue on with my lessons in C, I want to make sure I understand the use of the Return statement within my own functions. From what I understand, when the program encounters the Return statement within function it immediatly returns to the function that called it? or does it return to main()? Also from the examples I've seen, something like Return( 1 + 4 ); would return the value "5" to the Function name the Return statement was part of? Therefore, you can use a Function name within a printf statement to display a value? So that means a function can be used to same as any variable I'm assuming?

    Is my logic correct on this? And would the program below work (and make sense)?

    Code:
    #include <stdio.h>
    
    int AddNumbers ( int x, int y );
    
    
    int main ( ) {
    
    printf( "My returned value is: %d\n", AddNumbers( ) );    // calls the function and a value is returned to it
    
    return 0;
    }
    
    
    int AddNumbers ( int x, int y ) {
    
    x = 1;
    y = 4;
    
    return( x + y );
    }
    Last edited by lucidrave; 08-07-2009 at 01:10 PM. Reason: made small corrections to code

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    Returns to the caller (function which called the function which returns). Everything else is correct.
    You can even store the return from a function in a variable.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    AddNumbers() takes two parameters. You are attempting to call it without passing any parameters.
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    28

    thanks!

    thanks everyone! i understand the return statement pretty well now. i even corrected my code so it compiles and runs perfectly fine.

    Code:
    #include <stdio.h>
    
    int AddNumbers( void );
    
    
    int main( int argc, const char * argv[] ) {
    	
    	printf( "My returned value is: %d\n", AddNumbers( ) );    // calls AddNumbers function and a value is returned to it
    	
    	return 0;
    }
    
    
    int AddNumbers( void ) {
    
            int x, y;
    	
    	x = 1;
    	y = 4;
    	
    	return( x + y );
    }
    Last edited by lucidrave; 08-07-2009 at 05:22 PM. Reason: misc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  5. Class Bank(i have a question)
    By kuwait in forum C++ Programming
    Replies: 3
    Last Post: 05-30-2003, 07:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21