Functions are Easy in Ruby for Beginners 12
![](https://i0.wp.com/allprowebdesigns.com/wp-content/uploads/2023/11/1699253594_maxresdefault.jpg?resize=840%2C430&ssl=1)
Video Title: Functions are Easy in Ruby for Beginners 12
Hello everyone and welcome back today we’re going to be taking a look at reusing our code through the use of functions functions are going to allow us to declare a block of code AS reusable you can think of this sort of like you create a solution to a problem
Maybe your problem is you want a um like a user’s username to have the first letter capitalized every time you see it in your application instead of having to go through and write the code to capitalize that username every time you write it once put it into something called a function
And then anytime you need to use it you can just use the capitalize first letter of username function an easier example to show you out of the box is something where we let’s say need to print something multiple times we did this previously in the for Loop section or the while loop
Section so here let’s let’s just go ahead and let’s say we want to print hello world five times so our first approach might be something like let’s put hello world and then let’s just do this five times now of course this doesn’t scale very well what happens if we want to
Change this from Hello World to like happy birthday we would then have to go through and change every single one of these to happy birthday and that’s of course going to take go quite some time and we we aren’t always guaranteed to have all of these
In the same location so then we would have to go through each of these files for example find where we’re printing happy birthday or happy or hello world and changing it to happy birthday and then you know someone like me that’s not very smart might come through and just
Put in like hello birthday instead and then how do you find where that hello birthday is so of course you don’t want to do that you want to make sure that you’re only changing things in one place so what we could do is we could say say
Like um instead of just you know using the same string we could say like uh let’s do uh hello world is equal to hello world and then we could do print or sorry put hello world and now we only have to change this variable uh one time
So we can print this five times and then if we want to change this to happy birthday we just have to change this one to happy birthday right so that’s another option we have available to to us but okay we don’t want to have to do
This five times every time how else can we do it well be like before we could say 5. times do and then we could print hello world so now we’re doing this five times in one location we can of course test this by commenting these out and
Then running uh Ruby main. RB inside of this file and we can see this gets printed out five times so this is another option but sometimes we don’t just want to do something five times let’s say for example that we want to have a function that squares numbers for
Us so anytime we have a number let’s say number is equal let me actually leave this here for the sake of the notes and then we’ll say uh something like number is equal to I don’t know five and then we want to square this right so we could do something like put
Number time number something like that and now if we run Ruby main. RB you can see we get 25 because squaring a number is just multiplying it by itself you know one time but this is another one of those instances where maybe we don’t want to just use this number maybe we
Have like another number right this is equal to four and we want to also have this one get squared we don’t want to have to type another number numbered twice every time we want to square it that would be way too much work so let’s Implement a function to actually Square
Some numbers to do this we can use the def keyword and then we can say Square because we want to call it Square it then needs to take in something so that’s going to happen inside of these parentheses in these parentheses we’re going to take in some type of number
We’ll say number to B squared and then we can just I don’t know let’s put the number to be squared times itself and then end and now if we run this we won’t actually see anything because we have this function defined somewhere but we’re not actually using it yet so let’s
Actually use this function we can do that by just doing square and passing in five we can run this and then we’ll see 25 right here the reason we’re seeing 25 5 is because we have the puts inside of the square function so now anytime we
Want to run this we can do it like this we can change this to four we can change this to three we can run all of these we get 25 16 and 9 which is of course 3 * 3 4 * 4 5 * 5 this allows us to gradually increase
The complexity of our application and do so in a way where we have pieces of code that are reusable building blocks for us this won’t always be as simple as squaring we could also have something like a def for multiply let’s say multiply and this will take in two
Numbers we’ll have the first number first number and the second number so this is just a way to multiply two numbers together we can then put the first number times the second number if we then do multiply 5 comma 5 this is inherently going to give us the same thing as the
Square function but we can of course change this let’s change this to 2 * 3 which will give us six and now when we run this we can see we get back six the way that this works is the first number we pass in here before the comma goes
Into this first number the second number goes into this second number and we can call these whatever we want to we can say this one is apples instead of second number and then we can just put first number times apples which would then give us two for the first number and
Then three for the apples and then 2 * 3 is of course six I just like calling them first and second number so it’s a bit clearer and these are typically called arguments or parameters the parameters of a function allow us to have multiple different variables that we then use inside of
That function this will not always be as simple as multiplication you can think of this as maybe you have 20 lines of code here that do something super cool and you really need to have these these variables in the function to be able to do that really cool thing in that case
You just Define your variables up here they then become whatever the values are because when you when you use the function or when you invoke the function you then tell it what those variables are but instead of doing something like first number equals 2 and second number
Equals 3 you you just pass in those values and then comma separate them and then Ruby or whichever language you’re using is usually smart enough to say all right this two in this first comma slot goes to this first variable and this three goes to the second
Variable but let’s say we have a different scenario I need to print out a number I don’t need to print it out here what I actually want to do is do something like let’s multiply 2 + 3 * 5 what we could do here is we could say
Multiply 2 + 3 and then we could do this * 5 and let’s remove this 2 comma 3 if we now run this we’ll get 2 + 3 which is 5 * 5 and 5 * 5 is 25 but maybe we don’t want to do it like this
What we can instead do for this is say uh I don’t know like result is equal to 2 + 3 and then we could pass in which is what you call it when you do this result as the first argument here this would then give us the same result no pun
Intended without needing to do that ugly math inside of the function there’s other ways we can do this though instead of doing result of 2 + 3 we can just pass in three for this multip mply and we can say result equals multi 3A 5 now this will return 15 but
Not in the current state and you can see we get the squiggly line here if we run this we’ll get 15 printed out but this result here if we try to put the result will be empty it’s this empty line right here is what this put’s result is so we’re not actually getting
It back from multiply to do that we need to get rid of this puts in Ruby without that puts there we then get that that number sent into this result variable and then we can use this in other languages what’s actually happening here is you have a keyword return before this
Which is saying return whatever this is which is going to be 3 * 5 so we’re returning 3 * 5 which is 15 result is then 15 and then we can even do something like your multiplication result was and then by using our neat little uh
Pound symbol and our braces here we can put the variable into this this string and then when we print this we can see your multiplication result was 15 so when we use the return keyword we get back this value and then we can store it inside of a variable that we can then
Use later this isn’t actually required in Ruby so you’ll often just see the last line in a function just be like the variables themselves I’m not a fan of this because I like to explicitly see the word return there because my brain just Associates the word return with
Actually returning and when it sees something like this I don’t I don’t fully immediately jump to this is being returned I would say when you’re starting off it’s probably better to just keep those returns there and then over time as you get more and more used
To using this language you can start to phase these out because when you’re just making simple code like this with multiplication or addition there’s no real chance other people will see it so it just allows you to be clearer to yourself about what you’re doing you can alternatively just
Put in a comment that says this is what will uh return now that covers defining a function it covers passing in arguments or parameters to that function printing out the result inside of that function and then reusing it as well as returning the value and then using that returned value
Further on in your program there’s of course plenty more we can do with functions if we’re given the ability to but for now I’m going to leave you here because this allows us to move on to some more complex Topics in the future where we can start to address some of
The more advanced functionality so for now thank you for watching and I will see you in the next one
Video Keywords: Ruby Programming, deanin,ruby for beginners,a simple guide for beginners,ruby tutorial for absolute beginners,ruby tutorial for beginners,beginners,functions,ruby functions,learn ruby for beginners,learn ruby in one video,functions in ruby,ruby for beginners tutorial,ruby projects for beginners,ruby programming for beginners,for beginners,learn python for beginners,python for beginners,ruby language for beginners,ruby tutorials for beginners,create function in ruby
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99