Returning multiple values from a function

preview_player
Показать описание
When you first stumbled upon functions you might have thought that this is impossible, but what do you know, with some hacking you can return as many values as you want from a function. Feel free to ask questions in the comments below!

---

Рекомендации по теме
Комментарии
Автор

thanks man, your videos are super helpful

breno
Автор

I have looked up loads of videos on coding with C and your videos are the most informative out there.
You explain the theory and always have a great example that demonstrates what you want to convey.
Thank you <3

ecthelion
Автор

I would return a struct instead (and would put the error in the struct as well)

soniablanche
Автор

Absolutely outstanding explanations and example. Thank you !!!

ElementResources-rpox
Автор

Usually, a function can only return a single value, using the return mechanism. If you try to return more than one value from a function, only one value will be returned that appears at the rightmost place of the return statement. However, you can return multiple values from the function using the pointer, array, or structure.
Using Pointers
You can use pointers to return more than one value from a function by passing pointers as function parameters and use them to set multiple values which will then have visibility in the caller function.
Using Array
If you want to return multiple values of the same data type from a single function, then using an array is best suited because when an array name is passed as an argument then its base address is passed to the function so whatever changes made to the array is changed in the original array.
Using Structure
Another way to return multiple values from a function is by using structures. Structure is a user-defined datatype in C that can hold several datatypes of the same or different types. The idea is to create a structure variable containing all required data types as its members and return that from the function. We can then retrieve the values from the structure variable inside our caller function.

SandipBhattacharya
Автор

verry goodd so gooodd bro you win this c: (: thx you help me brother

jestemulomnyulomny
Автор

Thank you so much! This was really clear.

OMGWTFBBQSHEEP
Автор

completely new to understood it....good explanation....yes, what language is this....I am trying to learn java....hope its java

samburdge
Автор

5:08 use return int for returning error code for invalid inputs

yash
Автор

nice video but you need to learn to speak english

andreasandreou
Автор

Is there any difference between function pointer and pointer to function?

Or
Both are same..?

rajeshuppala
Автор

Amazing and simple . Thanks for the great explanation!

giorgostsitomeneas
Автор

what if we return an array of two elements from the sumandproduct function? Would it work?

zltn_brkl
Автор

Great explanation, thanks. One question: If sum and prod are arrays, how do I get the function to return it?

alexandresordi
Автор

I am using c and need a function that return a variable number of outputs. also these cannot be modifications in the memory but new variables created by the function. this is possible in matlab, is it in c?

charbeleid
Автор

Another option is to return a struct, in which you packed the two integers. Can you make a video of that too?

jabuci
Автор

This is giving values to two different variables at the same time from the same function. Can we return 2 values from a single function and then assign variables to those two values?

prathyushaguduru
Автор

Beautifully explained thank you so much!

julioortega
Автор

I have one small doubt, the function is not returning multiple returns at a time but either 0 or 1. So how can we say function returning multiple values?

rafimd
Автор

you can use std::pair or std::tuple if or some other object holding multiple values

codemop
welcome to shbcf.ru