TRENDING NEWS

POPULAR NEWS

Can A Fanction Return A Table In C

Can you return more than 1 value from a function in C++?

I'm using visual studios to write a program in C++ that does stuff with matrices

The main has to call a function that will read a matrix from a data file, along with how many rows and cols (the rows and cols are value are from the file itself, just the first 2 numbers of the file)

so the program sets the first value of the file to rows, the second to cols, then the rest to a matrix A[50][50], but i need to send all three values back to the main.

So am i going to have to make it into a matrix again to send it back or can i send back multiple values somehow


-----Heres my code so far if it helps----

#include
#include

void read(void); //function i want to return 3 values for
void header(void); //just displays a header, nothing else

int main(void)
{
char fil[50];
int rows,cols;
header();
read();
}

void read(void) //will read the data file
{
double A[50][50];
int rows,cols,i,j;
char fil[50];
printf("Please enter the name of the file\n");
scanf("%s",&fil);
printf("\n\n");
FILE *readf;
readf=fopen(fil,"r");
fscanf(readf,"%d %d",&rows,&cols);
printf("Rows = %d; cols = %d\n",rows,cols);
for (i=0;i {
for (j=0;j {
fscanf(readf,"%lf",&A[i][j]);
}
}
for (i=0;i {
for (j=0;j {
printf("A[%d][%d] = %.2lf\t",i,j,A[i][j]);
}
printf("\n");
}

}

--------------------------------------...

Can you return an array from a function(C++)?

You can only return a pointer to an array. Which in many cases would also require that you pass a pointer to the array when you call the function. If you create the array inside the function, then you need to be very careful that you don't create a memory leak by forgetting you used memory from the heap. There is also a bunch of other little problems with creating arrays inside of functions which may be using the stack and vanish when the function returns.

Sometimes an easier way is to simply declare the array as a global.

Also, it could be neither of your examples because it is somewhat unclear when it is completely out of context. When programming, use variable and function names that have some sort of meaning and help to document what you are trying to do.

This would be one way of passing a pointer to the function and then returning the pointer. Note that there is no need to return the pointer and you can use the return for an error trap or other things.

int *DoSomething(array[]){
return array;
}

In this second version the pointer to the array is still there and you can work with the array. If you detect an error or no error at all, you can return that rather than redundantly returning a pointer to the calling function that already has the pointer.

int DoSomething(array[]){
return ERROR;
}

You could also pass the array by reference. Look in your book or get your own copy of Stroustrup, The C++ Programming Language. The book has everything you never wanted to know about C++ from the guy who designed the programming language.

Shadow Wolf

What can I return for a char* function in C?

If you mean a C function declared aschar* func(...) {
...
return myval;
};
then you can return anything (myval) which is of the type char* or it is implicitly convertible by the given C compiler to char*. Or you can apply C style explicit cast to char* on the returned value, do it on your own risk, you have been warned!For more details see:Implicit conversions

In C++, can a function return another function?

Functions are not first class objects in C++. A function cannot return a function.[Edit: Except by reference, as Sergey Zubkov points out.]But, you can come close enough that it doesn’t matter. You can return a Callable type. That includes:Function pointers. These aren’t functions themselves, but rather pointers to them.Function objects. While function pointers qualify as function objects, C++ this most often refers to a class that implements operator()()—the function call operator—so you can make a function call on an instance of the class.Lambdas. These are actually just function objects with more convenient syntax and unutterable type names.Bind expressions. These are another type of function object that provide C++’s take on partial application. (Sometimes called currying.)Function pointers in particular enjoy a peculiar status in the language: Both C (as of ANSI C) and C++ allow you to apply the function call operator to a pointer-to-function in the same way you’d apply it to a function. Likewise, an expression of function type—such as the name of a function—decays to a pointer-to-function in an expression. This differs from the usual behavior of pointers, and gives function pointers special status.You can easily write code that gives the impression you’re returning a function and then calling the function you just returned. In reality, you’re returning something else: a function pointer or an object that implements the function-call operator.Helpers such as std::function provide some syntactic lubrication in certain cases.

Return a String from a Function C++?

You gotten into the complex area of pass by value and pass by reference.

Pointers, arrays and references are pass by reference. This means you act on the original data. Your first function doesn't get a copy of the character array, it gets a pointer to the array. When the first first function calls getline, it stores the data in the original array which can be used later.

The second function is pass by value. Here you don't get a reference to the original string, you get a copy. The function get-string has its own private string to use. When it exits, the private copy is destroyed and the original string is left blank.

There are two ways to fix this. One is to use a reference, the other is to use return values.

1)
void get_string(string &usr_string)
//etc

In this declaration, usr_string is a reference to the callers string. Any modifications to usr_string are made directly to the caller's value.

2) return a string from the function
string get_string( void )
{
cout << "whatever";
string usr_string;
getline(cin, usr_string);
return usr_string;
}

In this case a caller you do this:
string input = get_string();

Technically there is a third option of using pointers, but I will leave that out to keep the complexity down.

Return c++ Linked List from Function?

Hi Everyone,

I guess I am a little confused with pointers and I need your help. So, I have a function that should return a pointer. However, should I create the linked list head node outside the function or inside the function -- because I know that memory allocated locally are destructed when the function ends? Also, when I create a linked list inside the function, how do I return the head node?


Thanks!

Is it possible for a function to return an array in C++?

You can wrap the array around a struct and pass the struct from the function.#include
using namespace std;

typedef struct{
int array[10];
}wrap;

wrap func()
{
wrap w;
for(int i=0;i<10;i++)
w.array[i]=i;
return w;
}

int main()
{
wrap b=func();
for(int i=0;i<10;i++)
cout<}
However passing array by value is not a good idea as copying the array contents to another memory location is an expensive operation. Define the array in the caller function instead and pass the pointer to that array. Passing array by value is generally unnecessary and should be avoided.

How many return statements can a function have in C?

The Primary Purpose of Return is to go ahead and stop the current execution (current function) and return value and the control to the calling function.So if we declare multiple return statements in given function and call the same function the execution or control will end for that statement will end ,the moment it reaches the return statement and hence it would not consider the rest of the return statements which are declared.First Return Statement is considered.Example ( C_Program)—————————————————————————————————————int func1(){return 21; // Value 21 is returned to the function */return 22; return 23; /* this will be ignored */}void main() {printf(“%d”,func1());}OUTPUT: 21As Mentioned return 21; will be the exit point for the func1() and hence the rest of the return statements would not be considered for that function (func1)

How can we return multiple values in c function?

Using pointers you can return more than one value from the function.Consider following program (See live demo Ideone.com )#include
void fun(int a,int b,int *pa,int *pb,int *pp)
{
*pa=a+b;
*pb=a-b;
*pp=a*b;
}
int main(void)
{
int a,b,add,subtract,product;
puts("Enter 2 numbers: ");
scanf("%i %i",&a,&b);
fun(a,b,&add,&subtract,&product);
printf("sum of %d & %d is %d\n",a,b,add);
printf("subtraction of %d & %d is %d\n",a,b,subtract);
printf("product of %d & %d is %d\n",a,b,product);
return 0;
}
as said by Arpit Jain you can use reference variable in C++ to achieve the same.Here is C++ version of above program (see live demo Ideone.com )#include
void fun(int a,int b,int& add,int& sub,int& mul)
{
add=a+b;
sub=a-b;
mul=a*b;
}

int main()
{
int a,b,add,diff,prod;
std::cout<<"Enter value of a & b: \n";
std::cin>>a>>b;
fun(a,b,add,diff,prod);
std::cout<<"addition of "< std::cout<<"difference between "< std::cout<<"product of "<}
Edit 1:As said by Bhaskar Tiwari it is better & looks more elegant to use struct to achieve this. Edit 2:As said by Saurabh Singh in the comments you should use std::tuple since C++11 If you want to return >1 value from function in C++.Here is program that demonstrates it (See live demo C++ Shell )#include
#include
std::tuple compute(int s,int m,int diff=0) {
return std::make_tuple(s+m,s*m,s-m);
}
int main() {
int s(30),m(15),diff;
std::tie(s,m,diff)=compute(s,m);
std::cout<<"Sum is: "< std::cout<<"Product is: "< std::cout<<"Difference is: "<}

How can I return a structure from a function in c?

Just like any other variable. Here is a nice example:#include

struct a {
int i;
};

struct a f(struct a x)
{
struct a r = x;
return r;
}

int main(void)
{
struct a x = { 12 };
struct a y = f(x);
printf("%d\n", y.i);
return 0;
}
source: Return a `struct` from a function in C

TRENDING NEWS