Can a function return multiple values?

Updated: Mar 9


We know that in C/C++ programming, a function cannot return more than one value or in other words a function cannot return multiple values directly.

But if we use the call by reference/call by address method using pointers/structures/arrays, we can easily make a function return multiple values.

In C programming the above mentioned method can be referred to as returning multiple values from a single function.


Example of a function returning multiple values, written in C (using pointers)-



#include<stdio.h>

// Returning multiple values from a function in C
void assign(int *num1, int *num2, char *c)
{
	*num1=20;
	*num2=30;
	*c='S';
}

int main()
{
	int num1, num2;
	char c;

	assign(&num1, &num2, &c);
	printf("num1 = %d, num2 = %d, c = %c", num1, num2);
	return 0;
	
}

A function in C Programming returns only one value. If you use return statements to return multiple values, you'll fail because the statements will be invalid.

For example, the following return statements are invalid:

return(a,b);

return(x,12);


But using pointers, we can easily achieve our goal of returning multiple values using a function in C programming. Using call-by-reference or call-by-address, we can make a function return more than one value or multiple values at a time.


Indirectly, we'll be able to return two values from a called function and hence have overcome the limitation of the return statement, which can return only one value from a function at a time.


Recently, one of my classmates from school days uploaded a status on WhatsApp claiming that a function can return only one value and if I or anyone can prove him wrong, he'll give up teaching (he teaches coding to kids and aspires to be a computer teacher), in response to his claim, some people replied to his status explaining how a function in C programming can return multiple values, so did I, but he remained to stick to his claim. I asked him to let me know what the professor of his college has to say on this but heard from him.

The question was, "A function can return multiple values. Justify this statement with a suitable program segment.".

Before uploading the video on "Can a function return multiple videos?", I discussed about this with a computer teacher and a professor, to verify the fact that a function can return multiple values. I was right! IT CAN, and in the exact way I explained.

Back in my 12th grade, some of my classmates argued with me over the definition of hacker & cracker and later I was proved right.


There's no "official" way for a function to return multiple values in C programming, but-

The world of computers is all about tricks and hacks, sometimes you need to think out of the box to get your work done because there might be no official way to execute the task.

For complete explanation and better understanding, please refer to the video embedded below.


Thanks for reading. Have a nice day!

Recent Posts

See All

© 2020 BY ANUKIRAN GHOSH

  • Facebook Basic Black
  • Twitter Basic Black
  • Black Instagram Icon