C++ POINTERS (2020) - Return multiple values from a function using pointers? PROGRAMMING TUTORIAL

preview_player
Показать описание
We can return more than one value from a function. One way to do this is with the help of pointers. This method is called "call by reference", and it passes the address of a variable to the invoked function. In this video, you have an example of how that is done.

📚 Learn how to solve problems and build projects with these Free E-Books ⬇️

Experience the power of practical learning, gain career-ready skills, and start building real applications!
This is a step-by-step course designed to take you from beginner to expert in no time!
💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.

However, please don't feel obligated to do so. I appreciate every one of you, and I will continue to share valuable content with you regardless of whether you choose to support me in this way. Thank you for being part of the Code Beauty community! ❤️😇

Follow me on other platforms:
Рекомендации по теме
Комментарии
Автор

📚 Learn how to solve problems and build projects with these Free E-Books ⬇️
Experience the power of practical learning, gain career-ready skills, and start building real applications!
This is a step-by-step course designed to take you from beginner to expert in no time!
💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.

CodeBeauty
Автор

Pointers are super powerful, they can access the memory directly by the address of a variable and change its value easily. Fantastic topic.

m.s
Автор

Thank you. This is a very nice and concise explanation and examples of pointers returning multiple values. I'm learning a lot from these tutorials.

For those interested in expanding slightly deeper into this tutorial:

The getMinAndMax function assumes the min and max has been set to the min and max of the current array that's sent to the function, but is not guaranteed. For example, in a larger program, if the function has already been used with a different array with a smaller min and a larger max then in the current array sent to the function, then the values returned will be incorrect, unless the min and max is reset to the first element of the current array in use every time the function is called.

There are two ways that this could be addressed, such as setting the initial min and max inside the function. Another way could be to use the current getMin and getMax functions as shown below:

void getMinAndMax(int numbers, int size, int* min, int* max)
{
*min = getMin(numbers, size);
*max = getMax(numbers, size);
}

kevin_mitchell
Автор

All of your videos explain the concept so well! I am able to understand a lot without rewatching it. Thanks a lot for making these kind of videos!

tashi
Автор

Love your channel, very clear and well spoken. Subbed!

OpheliaSHolmes
Автор

so basically passing 2 pointers to a function. so clear and watching it for the second time. I should remember this. I hope

consciencedimension
Автор

Using a reference in the parameters and writing the body accordingly also works.
void getMinAndMax(int[] numbers, int size, int& min, int& max){...}
No pointers required.

FarhanKhan-cxke
Автор

This exactly what i've been looking for. Thanks a lot!

Unikv
Автор

Thanks a lot. I've been struggling with the concepts of pointers for a long time now and your detailed explanations are very helpful. Take love ♥

thehighlighter
Автор

This video is amazing. It really helped me understand pointers and the purpose of & to reference memory addresses.

thataverageplayer
Автор

Finally understood the crux of pointers. Thank you very much.

rnvj
Автор

How to return multiple values from a C/C++ function?
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
Автор

Did you need to use a pointer? Would the & operator have worked in the function declaration, removing the need for pointers in the function definition?

thataverageplayer
Автор

Great video, you have a great way of presenting the data.

lupofroi
Автор

I'm falling in love with you😁😁😂🔥🔥your teaching skills are excellent💖💖💖

Ankit-mqem
Автор

Thank you this helped me understand pointers better. Is there more to them that I should know? Except for the fact that they allow you to access and edit the original address of a variable.

ryippsx
Автор

Thnaks! I try to do it by pass reference. it works too.

wcchang
Автор

thank you very much for these videos !
its just what i needed, i got confused a lot working with pointers, and you make it look really easy (:

IsaacCode
Автор

Misleading title tho. The function is not exactly "returning" multiple values it is just assigning the values of min, max referenced through the pointers and passed as an argument to the function.

kaustubhpimparkar
Автор

Thank you so much!!! It helps me a lot.

Isaac-dxvy