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

Поделиться
HTML-код
  • Опубликовано: 30 янв 2025
  • НаукаНаука

Комментарии • 160

  • @CodeBeauty
    @CodeBeauty  3 года назад +22

    📚 Learn how to solve problems and build projects with these Free E-Books ⬇️
    C++ Lambdas e-book - free download here: bit.ly/freeCppE-Book
    Entire Object-Pascal step-by-step guide - free download here: bit.ly/FreeObjectPascalEbook
    🚀📈💻🔥 My Practical Programming Course: www.codebeautyacademy.com/
    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.

  • @m.s659
    @m.s659 4 года назад +26

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

  • @tashi7316
    @tashi7316 2 года назад +7

    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!

  • @kevin_mitchell
    @kevin_mitchell Год назад

    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);
    }

  • @OpheliaSHolmes
    @OpheliaSHolmes 4 года назад +27

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

  • @1conscience0dimension
    @1conscience0dimension 3 года назад +4

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

  • @naboulsikhalid7763
    @naboulsikhalid7763 10 дней назад

    after this video, everything is clear with the pointers. Thank you Saldina

  • @thehighlighter8676
    @thehighlighter8676 Год назад +1

    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 ♥

  • @Unikv
    @Unikv 3 года назад +9

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

  • @thataverageplayer5680
    @thataverageplayer5680 4 года назад +9

    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?

    • @chilly2171
      @chilly2171 2 года назад

      I was wondering as well. I just used & pointer to send a reference in.

    • @BonesTormAxeso
      @BonesTormAxeso 2 года назад

      If you remove the & (or the addresses of the variables) operator from that function you would't be able to return those two values at once, you probably would had to return an array, for example that contains those two values :P

  • @thataverageplayer5680
    @thataverageplayer5680 4 года назад +5

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

  • @rnvj87
    @rnvj87 3 года назад +1

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

  • @FarhanKhan-cx4ke
    @FarhanKhan-cx4ke Год назад +1

    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.

  • @MoTharwat-sw2dc
    @MoTharwat-sw2dc Год назад +1

    You are so gifted. Keep shining. ❤

    • @CodeBeauty
      @CodeBeauty  Год назад

      Thank you so much, I am happy to share my knowledge and make other people's programming journey easier than mine was 🥰🥰

  • @lupofroi
    @lupofroi 3 года назад +3

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

  • @JasminFrancisco-l4p
    @JasminFrancisco-l4p 11 месяцев назад +1

    For getmax, how does the program know how to output 26 instead of 6, since theyre both greater than max which is 5

  • @IsaacCode95
    @IsaacCode95 2 года назад +1

    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 (:

  • @mohanedemad60
    @mohanedemad60 3 года назад +1

    All your videos are amazing!

  • @SandipBhattacharya
    @SandipBhattacharya 2 года назад +2

    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.

  • @Ankit-mq6em
    @Ankit-mq6em 4 года назад +9

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

  • @wcchang3321
    @wcchang3321 2 года назад

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

  • @testvb6417
    @testvb6417 4 года назад +1

    Thanks, hope you keep up the serie about c++

  • @yapayzeka
    @yapayzeka Год назад

    previous video you said arrayname acts like a first elements pointer. isn't 5:10 passing numbers array is like passing a pointer ? but function waits for a value. it becomes confusing again :/

  • @Isaac-dx2vy
    @Isaac-dx2vy 9 месяцев назад

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

  • @kaustubhpimparkar3585
    @kaustubhpimparkar3585 3 года назад +3

    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.

  • @user-ry4ip9ps9x
    @user-ry4ip9ps9x 3 года назад +1

    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.

  • @krishnawa_
    @krishnawa_ 3 года назад +1

    it's really helped me thank you so much :)

  • @FikiMehi
    @FikiMehi 11 месяцев назад

    thank you sooooooo much. you explain very well.

  • @flyindon
    @flyindon 4 года назад

    Thank you. I love your videos, They are well presented and have helped me a lot.

  • @mohamedalaa501
    @mohamedalaa501 6 месяцев назад

    When I try to calculate the size of numbers[ ] inside the getMin( ) using sizeof(numbers) / sizeof(int), it gives that the minimum of numbers[ ] is always numbers[0], i.e. the for loop in getMin( ) is skipped. However, when I send the size as a variable getMin( ), although I found size using the same way, it compiles perfectly. Why do I get this bug?

  • @andriy_bondarenko
    @andriy_bondarenko Год назад

    Thanks! Clear and beautiful!

  • @virtualrelaxation
    @virtualrelaxation 2 года назад

    Thank you! You help me very much! :)
    You are amazing! :)

  • @mahmoudyosry9409
    @mahmoudyosry9409 4 года назад +2

    Thank you for this great content

  • @romainboy9895
    @romainboy9895 3 года назад

    Very clear and interesting. Thank you very much !

  • @funwithshay5945
    @funwithshay5945 4 года назад +5

    Hey, I just started watching your channel and wanted to say thank you for the amazing help you provided. My teacher uses c instead of c++ but I notice that only things like cout and cin are different for now. In c the library is stdio.h tell me if Im right that it is about the same thx!

    • @CodeBeauty
      @CodeBeauty  4 года назад +4

      They are very similar. They have very similar syntax and code structure and both are close to the hardware - so minor differences here, it should be easy to switch between them.
      The main difference is that C is a procedural language and it doesn't support OOP, while C++ is both procedural and object-oriented, and because of that, I'd give C++ an advantage over C, but neither one is a bad choice. 🤗

    • @funwithshay5945
      @funwithshay5945 4 года назад

      @@CodeBeauty Ok, thank you!

  • @shanmugammayan6966
    @shanmugammayan6966 Год назад

    What will happen if we didnt declare int size as parameter. Without even that, the prog is running.. can pls tell

  • @Ijamhuang
    @Ijamhuang 2 года назад

    This is amazing!!

  • @skeleton_craftGaming
    @skeleton_craftGaming Год назад

    C++ is moving away from raw pointer semantics, if you want an optional value, you should be using the optional container. If you don't want an optional, you should be passing by T&...
    Also for the specific examples that you given int& would have been better because it avoids the SEG fault when I you pass null to the min and max value. (Also passing by reference rather than passing by pointer makes it so that the data does not need to be dereferenced in the function and that the address does not need to be passed)

  • @reina4969
    @reina4969 2 года назад

    What shortcut do you use to comment out a block of code?

    • @day4834
      @day4834 2 года назад

      CTRL+K, CTRL+C

    • @ajaymore2693
      @ajaymore2693 2 года назад

      @@day4834 no don't make chutiya

  • @chesterapavliashvili1533
    @chesterapavliashvili1533 4 года назад +3

    Nice tutorial, it reminds me out variables from C#
    and can someone tell me how does System("pause>0") works? it's bit confusing cuz compare is passed as string literall

    • @CodeBeauty
      @CodeBeauty  4 года назад +9

      system("pause"); is going to pause your console window until you press any key, and if you add >0 to it, it is going to remove junk text
      You can remove >0 in the program that you're writing, and check out the behavior of your program, and then remove the remaining system("pause") part and check again
      system("pause") is a platform-specific hack though, and it is not exactly recommended to use.
      I use it to pause the console window.
      However, a program that has done its job should quit and return its resources back to the computer, but in this particular situation, we're using system("pause") so that we can pause it and see the output in the console before it closes.

    • @chesterapavliashvili1533
      @chesterapavliashvili1533 4 года назад

      @@CodeBeauty much thanks

    • @paulzupan3732
      @paulzupan3732 2 года назад

      If you want something more platform independent you can use std::cin.get() instead.

  • @eeshnayani
    @eeshnayani 2 года назад

    can you tell me how to have a thousand seperator in c++?

  • @TonyAquaro
    @TonyAquaro 3 года назад

    Hi Can l use Min as Lowest , low in [10] bars shift 0 Max Highest , High in [10] bars shift 1 total = [21] bars

  • @erikstigum9127
    @erikstigum9127 3 года назад +2

    You should really assign default value to min an max in function. You can’t assume user will set it to first element of array.

  • @shreyanshjain4781
    @shreyanshjain4781 3 года назад

    nice explanation!!!

  • @jmrbautista3974
    @jmrbautista3974 4 года назад

    HELP ME PLEASE! 😢 how can i create a program in c++ that is focusing on multiple conversions. (with the use of goto loo, if else, switch) i think these 3 will be use. BIG THANKS IF YOU WILL BE ABLE TO HELP ME ❤️ . THANKS ALSO IF NOT

  • @elsafernandatorresferia886
    @elsafernandatorresferia886 4 года назад

    Great, thanks!
    Just one question: Can someone explain me how the program assigns the number of elemts of the array to variable size? I don't see it and I don't understand how the for functions because size is never "initialized" or is never given a value. Tkanhs :)

    • @thataverageplayer5680
      @thataverageplayer5680 4 года назад +1

      Size is the parameter and 5 is the argument. Meaning, the function declaration is telling us we require an int called size. However, when the function is invoked/called you put the actual int there. CodeBeauty did that. They wrote 5 when calling the function within main. When they write 5 in the argument 5 is then stored within the size variable declared in the function parentheses.

  • @alexandresordi346
    @alexandresordi346 4 года назад +1

    Great channel. You explain very well !! One question: If min and max are also arrays. How do return min[ ] and max[ ] using numbers[ ] as a parameter? Thanks!.

    • @thanaphonsanongkhun3372
      @thanaphonsanongkhun3372 2 года назад

      The min and max are not arrays. They simply store a singular data from an array at index

    • @robertogomezbarrron4968
      @robertogomezbarrron4968 Год назад

      @@thanaphonsanongkhun3372 Aren't they pointing to the same memory address ?? numbers[0].

    • @skeleton_craftGaming
      @skeleton_craftGaming Год назад

      ​@@robertogomezbarrron4968this by the way is why you should use the STLs array container rather than a pointer, all C style arrays are disappointed to an address that just happens to have the next few words after it allocated as well. (Declaring arrays with [] is a little bit different but still essentially the same thing) There is no distinguishable difference between a pointer to an array and a pointer to a single value in c...

  • @miguelangelgonzalezcontrer2434
    @miguelangelgonzalezcontrer2434 3 года назад +1

    oh por dios eres la mejor!!!

  • @sivasainagireddi7956
    @sivasainagireddi7956 3 года назад

    Damnn🔥
    Why can't I hit the like button twice🤕

  • @arshmaanali714
    @arshmaanali714 2 года назад

    can anyone tell me why copy can't be used in main fun ??

  • @mohamedahmed-jv4gz
    @mohamedahmed-jv4gz 2 года назад

    great video !

  • @vidyprasad
    @vidyprasad 3 года назад

    how to write initwindow function in visual c++ please make the video (output full screen )

  • @JD-eb5qu
    @JD-eb5qu 2 года назад

    Thank you somuch mam😍❤️

  • @alexandruteodor3585
    @alexandruteodor3585 3 года назад

    When I have a function like void getMinAndMAx() I pass the parameters by refference (int numbers[], int size, int &min, int & max), and when I call it I write getMinAndMAx(numbers, size, min, max). Is it the same thing as you did? Which method is preferable to use?

    • @billjohnes9380
      @billjohnes9380 3 года назад +1

      Yes, it is.
      In your version as in the original one the same things happen behind the scenes. Even the generated code is identical in both cases.
      The version with references is preferable.

    • @paulzupan3732
      @paulzupan3732 2 года назад +1

      In my opinion, using pointers is actually preferable to references in that scenario. The reason why I say that is twofold.
      First of all, it's very clear that you need to pass in a memory address, which will make the caller think twice about whether or not the data at that address will end up being modified inside the function.
      Second, it allows you to pass in a null pointer, which could be useful in a function like this. Maybe you don't actually care about the max value and you only want the min value.
      Anyway, that's my two cents on the matter. Both are valid approaches.

  • @onurolce
    @onurolce 4 года назад +1

    One another great tutorial about pointers. But I should warn you a possible crash in your code. Sometimes we can create an array with only 1 element. if we pass this array to your functions then your code will crash. So you should check array size first. Let me explain by code ;
    int main() {
    int myArray[] = {76};
    cout

    • @vk2ig
      @vk2ig 4 года назад +2

      If the array has only one element, won't it have a size of 1? In that case, this code
      for (int i = 1; i < size; i++)
      won't run beyond loop initialisation, because the loop variable i is initialised to 1, and i is not less than size (1 is not less than 1), so the "for" loop just won't run. In that case the code will return the only value in the array, as the variable min was initialised to the value held in numbers[0].

  • @tarekrio6438
    @tarekrio6438 3 года назад

    So helpful thanks

  • @chandrut5201
    @chandrut5201 4 года назад

    Hi i am chandru,when I run this program with the numbers (2,6,-5,67,-89)............It shows both max and min is -89.how can I fix it.?

  • @learntoearn6612
    @learntoearn6612 3 года назад +2

    Thumb up @TrueCodeBeauty 🙏☝️

  • @fsociety692
    @fsociety692 4 года назад

    @CodeBeauty can u please make a video on how to pass a function to another function ny refernce that we use one function attribute in other function

  • @ericmccarty4389
    @ericmccarty4389 3 года назад

    Your intro sound is super loud compared to your content volume. This info was super helpful though, thank you

  • @fachriyasir661
    @fachriyasir661 4 года назад

    Hi Sladina, can I do this method to return an array from a function??

    • @Uvuvwevwevweonyetenyevwe-zn1gv
      @Uvuvwevwevweonyetenyevwe-zn1gv 3 года назад

      Yes if it's dynamically allocated

    • @paulzupan3732
      @paulzupan3732 2 года назад

      Dynamic allocation is an option, but if you don't want to allocate on the heap, you can also add an array parameter and then return the same address

  • @billjohnes9380
    @billjohnes9380 3 года назад +1

    Using int instead of size_t for an array size is a severe error.

  • @nines7588
    @nines7588 3 года назад

    u saved me! thanks

  • @ArjanvanVught
    @ArjanvanVught 4 года назад

    Using pointers is C-like, in C++ you would use references in the function argument

  • @igorab1
    @igorab1 3 года назад

    Hi! it will be nice not to transfer the size of the array directly, but to calculate it: sizeof array / sizeof array[0]

    • @paulzupan3732
      @paulzupan3732 2 года назад

      That wouldn't work. When you pass an array into a function, it gets coerced into a pointer. Trying to do sizeof(array) would just yield the size of a memory address, not the total size of the array. Even if that did work, since we're no longer passing the length of the array into the function, there would be no way to guarantee that the array wasn't empty, meaning that array[0] could very well seg fault.
      TL;DR: Good idea in theory, but it wouldn't work in practice.

  • @yasarasamarathunga7532
    @yasarasamarathunga7532 3 года назад

    Hi i am really struggling with a code,
    Lets say u have a txt file which looks something like,
    Noun
    Dog
    An animal
    **
    Adj
    Pretty
    Something beautiful
    **
    Noun
    Quote
    A value
    **
    Noun
    Cinq
    Something..
    **
    How can we make the program to display only the words that has a “Q” in it but is not followed by a “U”
    Result should be something like - “cinq”

  • @canaryframes2847
    @canaryframes2847 2 года назад

    Any reason you can't you just do this?
    void getMinAndMax(int numbers[], int size, int &min, int &max)

  • @niskander1st
    @niskander1st 4 года назад

    Excellent tutorial. However, in the function call "getMinAndMax", the ampersand indicating address is redundant as both "min" and "max" are the addresses of the arrays already.

    • @vk2ig
      @vk2ig 4 года назад

      Really? Why do you say that? min and max both contain the value of numbers[0], which is 5. If you don't pass &min and &max then the getMinAndMax function will receive the address value of 5 in the pointers to min and max.

  • @TheSoreven
    @TheSoreven 3 года назад +7

    Actually that is not what is called returning multiple values from a method, just passing multiple references and changing them.
    Returning multiple values can be done with something like 'tuple' or a custom made class/struct.

    • @davidfulgencio9546
      @davidfulgencio9546 2 года назад +2

      You're right. Firstly, the function used in the example is of the void type which indeed means no return, then we cannot accurately talk about multiple return.

  • @RAMANKUMARGCEBIT
    @RAMANKUMARGCEBIT 2 года назад

    very helpful

  • @mustanggt-5006
    @mustanggt-5006 2 года назад

    i love your videos

  • @natse6
    @natse6 3 года назад

    your magic !!

  • @alfiankhaulany5326
    @alfiankhaulany5326 3 года назад

    can't understand a thing, I keep watching you for a whole video XD

  • @lamalamalex
    @lamalamalex Год назад

    I like that idea, but why not just do it in one step by calling by reference?

  • @mirthun1012
    @mirthun1012 3 года назад +2

    give me a heart sis🥰

  • @subee128
    @subee128 2 года назад

    Thanks

  • @pratyayganguli9693
    @pratyayganguli9693 4 года назад

    This is so interesting

  • @Amir-z3b5p
    @Amir-z3b5p 10 месяцев назад +1

    Greatly

  • @cambridgebreaths3581
    @cambridgebreaths3581 4 года назад

    Saldina, if one dedicates 5 hours per day of serious C++ learning, how long will it take to be an advanced C++ programmer? Subscribed and Thanks

    • @CodeBeauty
      @CodeBeauty  4 года назад +18

      5 hours every day is a lot of time, and I'm not sure if it is maintainable long term. I'd aim for 2-3 hours per day, stay consistent, do that for a year and then start looking for junior dev positions, and continue learning.
      You are not going to be a hell of a developer after one year, but you might get a chance in a company, and if you do, find yourself a good senior dev who is willing to help and guide you and don't let go!
      Don't chase the money or different benefits that companies offer if you are just starting.
      Free food, team buildings, remote work, gym memberships, playing games at work... 🚫
      The only important thing at this stage is to get experience and knowledge, and once you have that, you'll have all the benefits and money that you want. But, it'll be a marathon, not a sprint. 😉

    • @cambridgebreaths3581
      @cambridgebreaths3581 4 года назад

      @@CodeBeautyGreat. will follow your advice. And will comment after a year. Wish you all the very best.

  • @m7s293
    @m7s293 3 года назад

    Where is the task?

  • @amiganer681130
    @amiganer681130 3 года назад

    Using a "struct" with min and max, but I think the (real) return value will be a pointer to the struct variable, I think it is not realy seen as a pointer.

  • @kelvinwilliams2908
    @kelvinwilliams2908 3 года назад

    I followed it through but it sounded like a complicated way to find the max and min values in an array. Is there an easier way to do it, or were you just showing us how to do it using pointers?

    • @TheTin123456789
      @TheTin123456789 3 года назад +1

      I think she used this example just to demonstrate how to use pointers. In this case, we can find max value or min value without using pointers. Please correct me if I am wrong.
      void getMinMax(int input[]) {
      int min = input [0];
      int max = input[0];
      for (int i = 1; i < sizeof(input); i++) {
      if (input[i] < min)
      min = input[i];
      if (input[i] > max)
      max = input[i];
      }
      cout

  • @eliaspianomusic
    @eliaspianomusic 3 года назад

    This is pretty much what you understand as "call by reference", am I right? Just to make sure I connect the right dots.

    • @eliaspianomusic
      @eliaspianomusic 3 года назад +1

      Oh yeah, it is, just saw it in the video description! Thanks a lot for writing it in there as well! Great and very helpful video(s)!!

  • @mitnickcodehelper4392
    @mitnickcodehelper4392 3 года назад +1

    My answer is a short program in c++ this:
    #include
    class CodeBeautyMinMax
    {
    private:
    int min, max;
    /* Disallow object creation without an parameter */
    private: CodeBeautyMinMax(){}
    public: CodeBeautyMinMax( int *paramArr )
    {
    /* min and max should be zero */
    this->min = this->max = 0;
    this->findMinAndMax( paramArr );
    }
    private: void findMinAndMax( int *paramArr )
    {
    if ( paramArr == NULL ) return;
    /* first element of array is now stored into min and max variable*/
    this->min = this->max = *paramArr;
    /* Enter the While-Loop again if you do not find a 0 "Zero" */
    while( *(++paramArr) != 0 )
    {
    /* To reach the next element of the array - This does the trick: ++paramArr */
    /* This is the same as above into the Video - but shorter =) */
    this->min = ( *paramArr < this->min ? *paramArr : this->min );
    this->max = ( *paramArr > this->max ? *paramArr : this->max );
    }
    }
    public: int getMin(){ /* Get only the min-value */ return this->min; }
    public: int getMax(){ /* Get only the max-value */ return this->max; }
    public: void getMinAndMax( char* buf )
    {
    /*
    It would work with sstream as well - but I am a old school guy =)
    Combine string with variable like this
    */
    sprintf( buf, "min is: %d
    max is %d", this->min, this->max );
    }
    };
    int main(void)
    {
    int arrValues[7] = { 1, -5, 14, 6, -20, 99, 0 };
    int *ptr = arrValues;
    CodeBeautyMinMax *cbmm = new CodeBeautyMinMax( ptr );
    std::cout

  • @pseudoname777
    @pseudoname777 Год назад +1

    Can someone explain the purpose of pointers
    I’ve watched most of her videos on them (maybe haven’t gotten to the one with the answer)
    But pointers, so far, seem like just another option instead of nested if statements. Why use them?
    I can pass multiple if statements in a single function and return the value. Why go through its address and dereferencing them

  • @and_then_I_whispered
    @and_then_I_whispered 2 года назад

    I think, we can just pass those max and min by reference, I mean without passing addresses.

  • @nnamdiobasi3359
    @nnamdiobasi3359 2 года назад

    Hey Saldina, I'm new to c++ and your videos are really helping. I have a problem with this example as i do not fully understand the need for pointers in it. Here is my own code:
    #include
    #include
    void maxminNum(std::vector nums){
    //for max:
    int max = nums[0];
    int min = nums[0];
    for(size_t i{1}; i < nums.size(); i++){
    if(nums[i] > max){
    max = nums[i];
    }
    if(nums[i] < min){
    min = nums[i];
    }
    }
    }
    int main(){
    std::vector vect {3, 5, 1, -3, 8};
    maxminNum(vect);

    return 0;
    }
    Please explain why my code which passes by value may be affected and why we should pass by reference as the code works as it should.

    • @karelchlumecky2574
      @karelchlumecky2574 2 года назад +1

      Hey. Your code works good for min and max but if you want use max and min values in function main you can't. If you declare variable in function, this variable on the end of loop will disappear. So if you need use in main or in another function your max or min (and you don't want working with return types in function maxminNum) you need declare variables max and min in main function and send pointer like a parameter to your function.

    • @knanzeynalov7133
      @knanzeynalov7133 Год назад

      Chatgpt

  • @monkeyrobotsinc.9875
    @monkeyrobotsinc.9875 2 года назад +2

    I still don't know the point of pointers. Dang. I'll keep trying

    • @sdy.deathcall9608
      @sdy.deathcall9608 Год назад

      Bro same. The problem is i can get those functions working without it.

  • @neurothoughtmachine
    @neurothoughtmachine 2 года назад

    // using a pointer to pull data out of a function....
    // using a pointer to pass a reference to an array
    #include
    using namespace std;
    int* func_GetArray(int numbers[], int size, int min, int max, int* numbers_out)
    {
    for (int var_i = 0; var_i < size; var_i++)
    {
    if (numbers[var_i] < min)
    {
    numbers_out[0] = numbers[var_i];
    }
    if (numbers[var_i] > max)
    {
    numbers_out[1] = numbers[var_i];
    }
    }
    return numbers_out;
    }
    int main()
    {
    int numbers[5] = { 5, 6, -2, 29, 6 };
    int numbers_out[2];
    int min = numbers[0];
    int max = numbers[0];
    // placing a fixed value into a function's argument list is a no-no.
    // Always use a variable or a pointer to a variable when passing values into a function.
    int size = 5;
    func_GetArray(numbers, size, min, max, numbers_out);
    cout

  • @ahmedassal8710
    @ahmedassal8710 3 года назад +1

    In my humble opinion this video should be renamed to be "make your Function change in variables which exist in the main function using pointers" OR something like that I just not convinced with it

  • @rudybeshara
    @rudybeshara Год назад

    kess emmik shou awiye

  • @farikunaziz6504
    @farikunaziz6504 4 года назад

    can i use a function pointer*

  • @diman1ght491
    @diman1ght491 3 года назад

    OMG! Still No tuples, really?

  • @fusiontv5269
    @fusiontv5269 3 года назад

    7:15

  • @sunstrumsharam5388
    @sunstrumsharam5388 Год назад

    I try by reference and it does the same, it is different, than using with pointer? (your videos are awesome)
    void MinandMax(int array[],int size,int& pMin,int& pMax)
    {
    for(int i=0;i array[i]){pMin = array[i];}
    if(pMax < array[i]){pMax = array[i];}
    }
    }
    int main()
    {
    int arr[5] = {5,4,3,8,2};
    int min = arr[0];
    int max = arr[0];
    MinandMax(arr,5,min,max);
    std::cout

  • @kxyt4reel
    @kxyt4reel 4 года назад

    Sorry, where exactly do you return more than one value?

  • @TT-yi9nd
    @TT-yi9nd 3 года назад

    // FindMinAndMaxWithoutPointers.cpp : This file contains the 'main' function. Program execution begins and ends there.
    #include
    using namespace std;
    class FindMin_Max {
    public:
    int Max;
    int Min;
    FindMin_Max() {
    Max = 0;
    Min = 0;
    }
    void showInfo(int array [], int size) {
    Max += array[0];
    Min += array[0];
    for (int i = 1; i < size; i++) {
    if (array[i] > Max)
    Max = array[i];
    if (array[i] < Min)
    Min = array[i];
    }
    cout

  • @v_isforvictory9366
    @v_isforvictory9366 2 года назад

    if we are being honest this was very confusing all I wanted to do was return multiple values from a function, not this whole find smallest and largest biz

    • @day4834
      @day4834 2 года назад +1

      Hi, I also felt the challenge, but I believe it's very important to master the basics (which we will only appreciate later on). The videos gradually increase in complexity to allow us to evolve.

  • @Alphabet_-_
    @Alphabet_-_ 4 года назад +1

    I *Miss You

  • @chrischoir3594
    @chrischoir3594 2 года назад

    better practice to return a struct