answersLogoWhite

0


Best Answer

It depends on the language, but in most cases yes, you can return arrays to callers via the return statement.

In C, arrays can be returned from a function by reference (that is, by pointer) but never by value (arrays cannot be copied automatically). The array must be allocated on the heap, never on the stack (you cannot return references to local variables). However, beware that returning arrays by reference is unsafe because there's no way to determine the upper bound of the array. This is why many C library functions return multiple values through output parameters and use the return value to indicate error conditions.

One way to return both the array and its size via a return statement is by returning a structure:

struct array_info {

void* array_ptr; /* pointer to first element in array */

int size; /* number of elements in the array */

};

Obviously you must cast the array_ptr member to the appropriate type before dereferencing any of the array elements.

In C++, C-style arrays work just as they do in C. However, the preferred method is to use a vector rather than a C-style array. A vector is a class template that encapsulates a C-style array with size and reserve, along with a rich set of useful functions (as with all templates, you don't pay for what you don't use). Vectors can be returned from functions both by value and by reference. Vectors also support move semantics making it possible to return vectors allocated on the stack as well as on the heap. C++ also supports an array class template. This is specifically for fixed-size arrays but is otherwise similar to a vector.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can an array be passed from a function to the calling portion of the program via a return statement?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is return means in programming?

A return statement exits the function in which it is declared and gives control to the calling code. Returning from the main function exits the program and gives control to the execution environment.


Explain goto and return in c?

A return statement is used to transfer the program control flow to the function that has called the current function under execution. If the function is main, then the program returns the control flow to the operating system. The syntax for return statement is:return return-type;A goto statement is used to transfer the control flow to a particular labelled statement, not necessarily back to the calling program. There are somerestrictionson using a goto statement. For eg: the goto statement should not skip any variable declarations. The use of goto statement is usually considered as a bad programming practice. The syntax for goto statement is:goto label_name;....label_name: statements;


How do you write a program using the gotoxy statement and print function to display letters of the alphabet on the computer screen?

There is no gotoxy statement in C.


What is the meaning of statement missing in function main in c language?

Every C program must have a function, named main(), which is where the program starts execution. If there is no function main(), the computer does not know where to start running the program. The function main() must also do something; if it is just empty, some smarter compilers will note that there is nothing for the program to do, and will give this sort of error message to indicate that you forgot to tell the program what to do.


What is the difference between the calling function and called function?

Function calling is when you call a function yourself in a program. While function invoking is when it gets called automatically.For example, consider this programstruct s{int a,b,s;s(){a=2;b=3;}void sum(){s=a+b;}};void main(){struct s obj; //line 1obj.sum(); // line 2}Here, when line 1 is executed, the function(constructor, i.e. s) is invoked.When line 2 is executed, the function sum is called.

Related questions

What is return means in programming?

A return statement exits the function in which it is declared and gives control to the calling code. Returning from the main function exits the program and gives control to the execution environment.


Explain goto and return in c?

A return statement is used to transfer the program control flow to the function that has called the current function under execution. If the function is main, then the program returns the control flow to the operating system. The syntax for return statement is:return return-type;A goto statement is used to transfer the control flow to a particular labelled statement, not necessarily back to the calling program. There are somerestrictionson using a goto statement. For eg: the goto statement should not skip any variable declarations. The use of goto statement is usually considered as a bad programming practice. The syntax for goto statement is:goto label_name;....label_name: statements;


What are the Three characteristics of a program module?

Declaring,defining and calling function


What do you mean by calling a function?

It is asking a function to run, by typing the command that runs the function within a program or applications like databases or spreadsheets.


How do you write a program using the gotoxy statement and print function to display letters of the alphabet on the computer screen?

There is no gotoxy statement in C.


How do you use return?

In C/C++ programming and most other procedural languages, you use a return statement to return control to the calling function. In the case of the global main function, a returnstatement terminates the program, releasing all memory used by the program and returning control to the execution environment.Functions that return void do not return a value and therefore do not require a return statement, unless the function needs to return early (before falling off the end of the function). Functions that return values must use a returnstatement to return the appropriate value to the caller.In C++ (but not in C), the global main function does not require a return statement unless returning early. When omitted, the global main function implicitly returns the value 0 (to the execution environment) when execution falls off the end of the function. To return any other value, a return statement is required.


What is the meaning of x.getdata in c plus?

it mens u r calling de member function of de program


When argument are passed by value the function works with the original arguments in the calling program?

When a function is passed by value the calling function makes a copy of the passed argument and works on that copy. And that's the reason that any changes made in the argument value does gets reflected to the caller.


What is accounting's purpose of the statement of government activities?

The statement of activities reports the net expense over revenue of each individual function or program operated by the government.


What is the meaning of statement missing in function main in c language?

Every C program must have a function, named main(), which is where the program starts execution. If there is no function main(), the computer does not know where to start running the program. The function main() must also do something; if it is just empty, some smarter compilers will note that there is nothing for the program to do, and will give this sort of error message to indicate that you forgot to tell the program what to do.


What is the difference between the calling function and called function?

Function calling is when you call a function yourself in a program. While function invoking is when it gets called automatically.For example, consider this programstruct s{int a,b,s;s(){a=2;b=3;}void sum(){s=a+b;}};void main(){struct s obj; //line 1obj.sum(); // line 2}Here, when line 1 is executed, the function(constructor, i.e. s) is invoked.When line 2 is executed, the function sum is called.


What is a final catch statement?

A "final catch" or a "try catch" statement is an exception (error) handling statement that will try to run the code between the final/try and "catch". If an error has been encountered, then the computer will proceed to the catch portion of the statement to prevent the program from terminating on error.