LESSON 35: Passing Parameters and Variables by Reference in Arduino Functions
HTML-код
- Опубликовано: 28 авг 2018
- WE have learned in earlier lessons that is it poor programming practice to use global variables. In today's lesson we learn how to pass variables and parameters to and from arduino functions by using the pass by reference method.
Наука
I wish I had a teacher like you when I was attending school. With your explenation everything looks so simple!
I came across this while researching for training for a young friend in Nigeria. I like your very gently staged style. And I love that background music ;-)
Last few lessons have made so much sense and made my programming 300% better
Wow, this series was good. I finished it in 3 days, and I don't wanna stop. Thank you for another amazing series!
Enjoy!
Hello sir, Will you be making more videos?
You are missed by many
After a hiatus of more than 30 years from programming I started "playing" with Arduino projects. I remembered some of the C lessons, lectures and exercises from back then but things like state machines and pointers I needed refreshing. Your short and to the point lessons are very good for learning and refreshing. Thank you.
This was very useful to me. I’ve watched the second Arduino series and the robotics but this clarifies the workings of variables in functions. Always good to plug in with you Paul.
"BOOM!!"
me: *spills coffee*
All the 36 RUclipss are built with lot of care and really useful for learning Arduino. Thanks Paul.
I've used the Arduino for several years now but one thing I am not good at is teaching it to others. So whenever someone asks me how to program the Arduino I first direct them to watch your videos. After they've watched most of this series, I then also encourage them to watch other channels such as Ralph S Bacon.
Your videos are fantastic and your teaching style works well with beginners. It's the best channel I've found to start learning with.
Thanks for the lessons on arduino, watched all. You explained it so well. Good luck with further projects.
I have learned so much from your lessons, both if using the Arduino controller and in the programming language itself. In this lesson, it was all programming. I enhanced the program to use a separate function for ADD, SUBTRACT, MULTIPLY, DIVIDE and finding the SQUARE of the first number. I used REFERENCE to pass parameters back as well as the return command.
Thank you so much, I've struggled to understand the nature and implementation of communicating to and from functions. This lesson cleared my lack of understanding. ❤
what a great explanation (passing by reference), first time explained, first time understood. Thank you
Hi Paul,
How are you?
Leared a lot today by watching your lessons how to pass variables and parameters to and from arduino functions by using the pass by reference method. From now on I will use this easy methode! BOEM!!
PS Nice to see we share the same passion : God (at the first place) and ARDUNIO ;-)
Paul, your teaching style is great! I have been trying to learn arduino programming for a while. It is finally starting to click. Thanks to your tutorials.
for people like us who wants to learn but because of lack of money our dreams never come true, you are giving us hope......thank you so much sir
Hello Paul. I have now watches several of your videos and really getting a lot from them. You explain things clearly and thoroughly. Definitely up with the best. You have helped me get my head around functions and getting cleaner code.
Thank you and keep up the great work.
Thanks for the encouraging words!
Thanks Paul, I use your lesson's as a reference guide. Please keep the video's coming!
Really good example at the end on the pass by reference and that is what I was interested in. I just did a fast forward got what I needed. For some reason I could not find this in all my google searches. You really do a good job on the videos and they will last forever. Thanks for all your efforts. Thank BOOM was exactly what I wanted!
I am behind on providing the comments you've asked for. I am have gone through your entire series on Arduino, Python, BeagleBones, and Raspberry Pi. This is a tremendous teaching medium, and you really have it dialed in, many thanks.
Because of you, I bought the RUclips Red so I can download the series. Like many Texans, I have a long commute to work, and I listen to the audio a couple of times before I sit down to watch them. They are so well done that I can visualize the operations in my mind without needing the visual component. I mentor a HS robotics team (FRC) and they use LabView for the robot. These series have given me a great knowledge base to work off even for other programming languages, but I would love to see you do a series on LabView, especially since I think it would mesh perfectly with your High Altitude Ballooning operations.
My only "complaint" would be that I have noticed that you use fewer comments in the code as we go along. I usually FF to the end code, read it, then go back to the beginning to watch the whole thing. That is how I have been gauging my progress, on how well I read it. By the way, I'm an old guy too, just a couple years from retirement.
Keep up the good work! I hope your school district recognizes what they have in you. You are leaving an impressive legacy. Best regards.
I finally settled on the BeableBone Blue for most of my robotics, and BeagleBone Black for projects that use LabView. The series you did on Linux was instrumental in moving those projects along. Thankfully, I started computers prior to DOS coming out, so the command line stuff was rusty and dusty, but eventually I remembered most of how that worked. If you are interested in LabView, let me know since I have many contacts at National Instruments. They are a big sponsor of FIRST robotics and wonderful people.
Thank you so much for your videos! I started learning arduino in my spare time and going through your first few lessons got the ball rolling. As I wanted to advance my programs I kept coming back to learn how to do so.
Thank you Paul for sharing with us your valuable knowledge in such a lucid fashion :) . Thanks a lot.
My pleasure!
Excellent lesson. I typed some notes on this lesson because you covered a LOT of details. I took special note on function construct, order of argument list in tally(). Noted no RETURN in this construct, concur like this better. I looked up the reference operator (&) and I found out that it actually returns the address and retrieves the value of that variable Address....awesome. I assume we don’t have a RETURN because we are using the argument list list instead.....and I also assume this is a void function because we don’t have the RETURN as well. Finally I learned that in the tally() function definition the variables were type cast in the definition argument.....I did try to type cast them in the function body and I got some compiler errors. I LEARNED SOMETHING VALUABLE.......THANKS.
Hey Paul! your videos are crystal clear, one of the best across RUclips. Please make new tutorial series for our sake. PLIZ
Great teacher and lessons. Thanks to this course you don't have to leave the house
Thank you very much for these lessons. I will now watch your new series. I made a grain bushel estimator to load our trucks on the farm. It uses an amp meter on the grain leg and I calibrate it.
Hello, Mr.Paul McWhorter. I am a computer science student from Sri Lanka. I love your teaching method. I have watched all the tutorials in this playlist. i want to make a finger print door system for my campus. We are going to fix this finger print door system to class room doors. I need to implement the GSM module with it. for example if one student comes and places his finger, if his finger matches in the database. we need to open the door and also we need to send an SMS to his parent mobile phone. if the finger does not match in the database, the door does not have to open and we need to take a photo of the person who placed the finger in it. We need camera module also with the flash light. It uses the solenoid lock.
FEATURES
=========
01. It should a have solenoid lock.
02. if the finger matches in the database, we need to open the door and send an SMS to their guardian.
03. If it doesn't we need to take a photo.
Please please, cloud you please make a tutorial how to do it step by step. please.......
I believe in you.
Thank you so much Sir. I also thank you for the tutorials in this channel.
kuna rakulan how u want to kept the data in database. What system u use?
i do not want to keep the data in database.
check the following video. finger print sensor has backup to store the finger prints.
ruclips.net/video/aYUFmRckc_k/видео.html
just search " arduino and finger print " you will get videos with the specification you requested.
@@123kkambiz thank you so much
It was a great tutorial. I'm very thankful for the amount of effort and time you put in to teach us Arduino programming.
I had tried to learn Arduino before but you are the only tutor I could continue the course to the end. Best wishes Mr.McWhorter.
Glad it helped!
" Hello guy, hold your breath and BOOM :) !!!" you are great teacher Paul.
I would be great if you have a lesson with the wifi module, thanks!
Very good tutorial. Studied comp sci at uni back in 2003. I'm playing around with Arduino now. Wish this was around back when I was learning to code- would have made the learning more fun and easy.
this makes more sense than the return alternative and is easier to understand and debug. thanks!
Thank you again for another great lesson Mr. Mcwhorter. I'm excited to be done with these and start your new arduino lessons. I appreciate the walkthrough. I'm going to join your patreon as soon as I can.
Sir, You are the BEST !! Your explanations are so well structured that understanding arduino coding has really become very easy, for hobbyists like myself. Thanks and God Bless.
Lessons 32-35 were super useful!! Thank you
Hi Paul, you have a fantastic teaching style, perfect pace and an obvious understanding of how a student can be guided efficiently to understand the topic. I subscribed to your channel without hesitation. I would love an explanation of how I can input a string of numbers one by one from a touch screen and turn the string into a real number that the rest of the program can make calculations from. My stumbling point is how to handle the decimal part of the string. I also want to validate the number to be between 2 and 25. I have seen many examples of code to read numbers from a keypad or a touch screen keypad, but none have ever handled a number such as 19.7
Im so glad that i've learned something in this quarantine useful thank you Paul 😄
This is another great lesson to take my programming to the next level. Thanks so much!
Thanks for the awesome videos, ou baas! You did a fantastic job explain these concepts to a complete noob. I will look forward to seeing more in the future!
Keep them coming, you saved me. Great job, thanks a lot!
Thank you, sir! Your video has made my problem solved.
Very well explained and thanks for sharing your knowledge with us, thought i don't get the same results like you do, the code looks identical to mine. I hope you can explain static and volatile variables in another video !
I like this, using the Ampersand seems so easy to pass the parrameters back.
Because of their position in the Void tally() function name (3rd and 4th) they match up with the position of the parameters in tally(a,b,c,d); so sum will reference to c and difference will reference to d
Nice.
Great information Paul, I would have found it time consuming to find that answer else where I think.
Also because you're not using 'return;' the function type is again Void, please correct me if that last bit is incorrect.
Thank you as always for another thorough, and informative video!
Hi Paul, Ignore the picture, that was me in my younger years!
First of all, "thank you SOO much". I love your Arduino IDE videos. They are so much easier to understand than any others i have come across.
The question: I have been looking at a number of projects for making clocks recently, and almost all of the code use the terms ++x or x++.
For a long time i could not work out what it meant, until i found it in the arduino.cc webpage.
x++; // increment x by one and returns the old value of x
++x; // increment x by one and returns the new value of x
Your code is more "old school", using x=x+1, which in my opinion is the "proper" way to do it. But as i am 59 years old now, and was brought up with the original BASIC, i guess i would prefer your method. However, could you possibly create another video in the Arduino IDE series that covers this topic as clearly all the "young ones" out there are determined to use this newer syntax.
There is also a - - x and x - - too (where will it all end).
The return is used to send back ONE value in the name of the function e.g. numSquared=sqr(num);
//num unchanged
If more than one variable needs to be sent back changed then use pass by reference.
Thanks a lot! I've been looking for a while how to do that and couldn't find. I tried to clean the code to have the function in a different tab (file) and it works.
Hi Mr. Paul! I have watched all of your videos and I love the way you teach. I can't wait for the next video!
Would you please consider doing one on building a simple LCD menu system that would allow the user to scroll through the menu options and read data from variables. It seems that all examples I can find are menus that allow changes in parameters, or are so complicated that I can't wrap my head around it. When i look at the code examples I am so overwhelmed by it's complexity that I don't even want to try to understand it. There has to be a simpler solution. I'm probably just being lazy, but a simple example might help me understand the basic structure of these examples.
Thank you! And don't stop teaching! :-)
Thank you so much for your help.
I’ll just implant this technique into my project.
Didn’t hear this in school up to now.
...and it works great! Thank you so much!!! 🙂👍
great explanation,keep it up
Great video Paul, thanks for the great work, very helpful.
love it is there is a good text book I can get for reference (OLD - retired basic , Fortran, PLC programming here) .. Need to learn something new
I hope there will be a future video explaining how to use created functions from one program into another. For example: creating a function that maps out the IR remote buttons properly in one function in program "a" and sending that data into a different program "b".
You, sir, are very good at explaining the innards of this device to an old man (70).
Can you speed up the date of your next series? I am now at lesson 9 and I'm worrying at reaching the end of lesson 35.
Greetings from The Netherlands.
Thanks sir for this tutorial.
I really wanted to know how to return multiple values from a function.
& My waiting is over.
Once again thanks a lot.
&
I learnt lots of Arduino coding techniques from your tutorial and used in my projects. Your techniques of explaining things are really good.
& Waiting for next tutorial.
Hello SirThank you Sir for your kind help in understanding the working of Arduino ,written demonstration is nor readable in some cases due to poor focus.
It's amazing... Thank you Paul...
thanks for the complete explanation!!!
thank you for the video sir! by far the best lesson/explanation in youtube
Paul, Great job, but can you take this lesson a little further and tie in when to pass by value, when to pass by reference and when to use pointers for the arrays we just learned about?
Thanky for wonderfull programming tips
all video waching.... best tutorial in arduino... thanks sir...!
Hey sir can u make
E ink (coustome writable with stylus or anything ) device with arduino or raspberry pi?
i was building an obstacle avoidance car and it was realy hard to write the code for each left ,right ,front turns again and again and functions helped ,me alot
Is local veriable saves memory or it faster than global one???
Hello, i like your videos. they are very helpful for me. Only one suggestion: I practice and learn at night time and please don't do boom. because i setup the volume just right and suddenly you do boom. :) I can't decrease the volume and everybody wakes up and angry. Or volume is too low for my hearing. Thank you for your helpful videos.
H Paul Having an issue with the code as it does not pause to allow entering the second number. It looks correctly coded, but to be sure I went over to your website but can't find it. In fact, I cannot locate "Lesson 35" under "Arduino Lessons" as it stops at 33.
" BOOM !!!" great teacher
In C++, & is called reference. It acts like a pointer, so & will change the value of a specific memory address, rather than only using the passed content for calculation. The 0/1 states of the transistors inside the CPU is changed by &.
What happened to the final code, written out so we can see where we made our mistakes? So we can fix them.
Great videos, very helpful.
I'd really like to see examples that do not use the serial monitor as most projects will not use it. For example, how about a reading a POT value, setting it to a variable, and passing that along?
it makes a lot of sense and will practice
Hi!
I know this is an older video but hoping you or someone out there can answer.
Can you do this with arrays as well?
I wouldn’t know what to google search. Your video made me wonder if that was possible, especially like you said about multiple variables being manipulated in a function. Tried to look, but I don’t think I’m searching the correct terminology….
Thanks. Great video btw.
Thank you Paul for your lessons! very useful!
yup, subbed as soon as I saw the cup of coffee :,)
Coffee . . . the elixir that fuels the high tech world
Hello Sir,
Your website doesn't open yet. Do you have any lessons on How to interface NRF24L01 with Arduino?
Thank you, I love your videos.
This lesson *very* helpful... good stuff!
Glad you think so!
Hello Paul, Can you please make a video on how to control Brushless Motors with Arduino?
Your Vids are really easy to follow :)
Sir I'm from j&k india. I love this series of lectures. Please upload some videos on esp8266 wifi module.
How come the function Tally has void in front of it even though it returns a value? Isn't void used when no value is to be returned? I'm confused.
The function did not return anything, as a matter of fact it didn't even finish by 'return' so indeed it is a void function. The way values were "returned" in this case is actually by passing the memory addresses of c and d through sum and difference using that & symbol. Since for instance c and sum now share the same memory address, any modification made to sum within the function will also affect c in the main loop. Nothing is returned.
Hi Paul, I am a very-soon-to-be mechanical engineer and have been doing a lot of project based learning with Arduinos. Thanks for all the helpful videos. One aspect of arduinos that I am having difficulty understanding is how the baud rate can affect or limit your sampling rate when using the analogRead() command for analog sensors and communicating the data through the serial monitor. For example, my understanding is that the default sampling rate for the analogRead() command with an arduino (16MHz clock speed and 10 bit ADC) is 9615 Hz, but if I set the baud rate for serial communication at anything below 9615 bits/sec * 10 bit ADC = 96150, won't the serial communication baud rate truly dictate the sampling rate instead of the arudinos built in ADC converter properties? Not sure if this is an area you have studied, but if you have any thoughts, I would be interested to hear what you have to say. Thanks
do you have some advice about use inkjet print head and Arduino? Thanks
Great, Sir.
I want to move some functions to my own library I can use later and I need to know how to pass and get back variables that are not in the same file. The other problem that arises is how to keep variables from those functions alive after I call the functions. ( I receive a string on the serial port, any time I receive something on the port I call the function which is building the string, it adds to the string, check if the string is completed and if not returns to the main loop. If the string is completed, it breaks it to code and variables and returns them. Question is how to keep the string until it's completed(in the library function) and how to return the two variables(from the library function) afterward to the main program
Absolutely excellent!
Hello sir, I learnt a lot from your videos. It would be very helpful if u can make few videos about wireless communication on arduino. Thanks.
I thought you could not use 'void' if you are returning data from a function? In this case, I thought 'void' should be replaced with 'float'?
@paul mcwhorter Hello, is there a way to get a live camera feed and communicate with the instrument package near the edge of space?
Thank you!!
I was confused about parameters
Hello sir will you please upload a lesson for gsm with arduino
Hello, Mr.Paul McWhorter. Thank you very much for all the Arduino lessons. You can make things very clear. I like to ask you a question: What is the best method to do the following: I connected 6 sensors to the ports A1 to A6. I have to read the values (value1, value2,value3 etc) of those sensors. Can I do that in one function? a function like: for(i=1;i
Love all your lessons ( boom boom boom)
Hi Paul great work on all your videos. I have really learned a lot. Can you please do a tutorial on Q learning.I have a robot that i am building and have some sample code, but i am trying to make sense of how the values are passed back and forth.Thank you so much.
Thank you Paul for all these nice videos.
I followed you New Arduino Tutorials and also many of the old Arduino Lessons.
They are all great ones...
I have a question, please, is there a way to create function with optional arguments? I surfed the net and i didn't find a clear relevant answer.
💡💡.. wow, great video, thx!!
Hello Doctor Aduino I love your lessons , but i need your help
I have 1 principal websocket function and 2 independant secondary fonctions ( firebase+ time) I want to return int (hour+ minutes receive by firebase) + (hour+ minutes of time ) to the websocket function
how to do that i have try your example but did not work
Sir, please make a tutorial on SPI and I2C.
Paul I love your lessons. I was wondering if you are planning to do PLC tutorials in the future? Not only ladder logic but also include structured text and fuction block?
Hi..paul, I really love your video.Can u do tutorial on PID control ??
Please do more tutorials on Fusion 360!!