return type confirmation

This is a discussion on return type confirmation within the C++ Programming forums, part of the General Programming Boards category; just a quickie, I have no reference books with me at the mo. 1. Is it possible to return an ...

  1. #1
    Unregistered
    Guest

    return type confirmation

    just a quickie,

    I have no reference books with me at the mo.

    1. Is it possible to return an array from a function or can you only return a single value?

    thanks.

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    if the array is declared locally, no.
    if it was declared elsewhere, yes.
    if it is dynamically allocated locally and not freed, yes.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    if the array is declared locally and static, yes.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    84
    i think i know how to solve this problem using pass by reference but i was wondering if you can solve the problem this kinda way.
    I just want to pass an array into a function add 1 to each of the elements then return the contents of the array into another array.

    Code:
    // function that adds 1 to every element of an array.
    
    #include<iostream>
    using namespace std;
    
    int[] function(int array[],count);
    
    int main(void)
    {
    	int array[5]={1,2,3,4,5};
    
    	int array2[]=function(array,(sizeof array[]/sizeof array[0]));
    
    	return(0);
    }
    
    int[] function(int array[],count);
    {
    	for(int x=0; x<count; x++)
    	{
    		array[x]+1;
    	}
    
    	return array[];
    }
    Any help more than appreciated!

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    273

    Wink

    true, true Salem.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    106
    yes you can return it you can return nearly everthing
    C++ Makes you Feel Better

    "Gravity connot be held reponsible for people falling in love"--Albert Einstein

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    (sizeof array[]/sizeof array[0]) is a strange way to get 5 but so be it.

    Seems to be fine the way you did it but I always use pointers instead of []. doesn't really matter I suppose.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    84
    i cant get the program to work . the compiler seems unhappy with my function prototype . can anyone tell me what i have done wrong please.

    C:\Program Files\Microsoft Visual Studio\MyProjects\day5\main.cpp(6) : warning C4091: '' : ignored on left of 'int' when no variable is declared
    C:\Program Files\Microsoft Visual Studio\MyProjects\day5\main.cpp(6) : error C2143: syntax error : missing ';' before '['
    C:\Program Files\Microsoft Visual Studio\MyProjects\day5\main.cpp(6) : error C2143: syntax error : missing ';' before '['
    C:\Program Files\Microsoft Visual Studio\MyProjects\day5\main.cpp(12) : error C2065: 'function' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\MyProjects\day5\main.cpp(12) : error C2059: syntax error : ']'
    C:\Program Files\Microsoft Visual Studio\MyProjects\day5\main.cpp(17) : warning C4091: '' : ignored on left of 'int' when no variable is declared
    C:\Program Files\Microsoft Visual Studio\MyProjects\day5\main.cpp(17) : error C2143: syntax error : missing ';' before '['
    C:\Program Files\Microsoft Visual Studio\MyProjects\day5\main.cpp(17) : error C2143: syntax error : missing ';' before '['
    C:\Program Files\Microsoft Visual Studio\MyProjects\day5\main.cpp(18) : error C2143: syntax error : missing ';' before '{'
    C:\Program Files\Microsoft Visual Studio\MyProjects\day5\main.cpp(18) : error C2447: missing function header (old-style formal list?)
    Error executing cl.exe.

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    do this instead
    Code:
    // function that adds 1 to every element of an array.
    
    #include<iostream>
    using namespace std;
    
    int *function(int *array,count);
    
    int main(void)
    {
    	int array[5]={1,2,3,4,5};
    
    	int *array2 = function(array, 5);
    
    	return(0);
    }
    
    int *function(int *array, count);
    {
    	for(int x=0; x<count; x++)
    	{
    		array[x]+1;
    	}
    
    	return array;
    }

  10. #10
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    array are passed by reference, you can simply modify the original if need be. This should also work:

    Code:
    void myfunc( int* array, int size )
    {
         for( int i = 0; i < size; i++ )
         {
              array[ i ] += 1;
         }
    }
    
    void main( )
    {
         int original[] = { 1, 2, 3, 4, 5 };
    
         myfunc( original, 5 );
    }

  11. #11
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    of course he already is modifying the original but he seemed to want the thing referenced by a second pointer. either way the whole thing's silly!

  12. #12
    Registered User
    Join Date
    Aug 2001
    Posts
    84
    Thanks guys,

    The penny has just dropped as to the importance of pointers
    again . So having a pointer as a return type you could also return
    an object . Is that correct?

  13. #13
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    The penny has just dropped as to the importance of pointers
    again . So having a pointer as a return type you could also return
    an object . Is that correct?
    No, because you are returning the pointer and not any object. But if the pointer points to the object and the object exists, then you can access it.

  14. #14
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    It is possible to return a local array. Wrap it in a struct

    Code:
    struct arr
    {
    	int a[5];
    };
    
    arr foo()
    {
    	arr a;
    	return a;
    }
    
    int main()
    {
    	arr b = foo();
    }
    Not much point, though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 12:56 AM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  5. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM

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