We all know that a function in C can return only one value. So how do we achieve the purpose of returning multiple values.
Well, first take a look at the declaration of a function.
So we can notice here that our interface to the function is through arguments and return value only. (Unless we talk about modifying the globals inside the function)
Let us take a deeper look…Even though a function can return only one value but that value can be of pointer type. That’s correct, now you’re speculating right!
We can declare the function such that, it returns a structure type user defined variable or a pointer to it . And by the property of a structure, we know that a structure in C can hold multiple values of asymmetrical types (i.e. one int variable, four char variables, two float variables and so on…)
If we want the function to return multiple values of same data types, we could return the pointer to array of that data types.
We can also make the function return multiple values by using the arguments of the function. How? By providing the pointers as arguments.
Usually, when a function needs to return several values, we use one pointer in return instead of several pointers as arguments.
Please see How to return multiple values from a function in C or C++? for more details.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- How to return multiple values from a function in C or C++?
- C function argument and return values
- Return values of printf() and scanf() in C/C++
- std::tuple, std::pair | Returning multiple values from a function using Tuple and Pair in C++
- How to return a Pointer from a Function in C
- What is return type of getchar(), fgetc() and getc() ?
- return statement vs exit() in main()
- Implicit return type int in C
- Return from void functions in C++
- What does main() return in C and C++?
- return statement in C/C++ with Examples
- How Linkers Resolve Global Symbols Defined at Multiple Places?
- Multiple Inheritance in C++
- Socket Programming in C/C++: Handling multiple clients on server without multi threading
- Assigning multiple characters in an int in C language
- Lex program to take input from file and remove multiple spaces, lines and tabs
- Structure Sorting (By Multiple Rules) in C++
- Can we call an undeclared function in C++?
- Can we use function on left side of an expression in C and C++?
- Can we access private data members of a class without using a member or a friend function?