Methods
In all the previous blogs we only learned about the methods of one particular class. Like in array class sort, length, and size, are the inbuilt methods. In this blog, we are going to discuss something similar that probably we can say the same as the function but from the user's point of view.
Functions
Imagine we want to use something in your project again and again. For that, the easy way is we can write that particular piece of code where ever we want. But guys!!! come on as we are programmers it's not good to go hard coding and it seems a little repetitive.
To avoid this we can place the code snippet in a box and we can call that block where ever we can instead of writing the entire code hardly.
Technically we can call them functions.
Parts
To use functions in our code we have to follow some rules while constructing a function. In every programming language,t it is mandatory to design a function like this only. There are three parts involved in this we can look at them one by one.
Header
This part contains the function name and parameters. In front of the function name we have to use the def keyword short for define means we are defining a function.
syntax: def function_name (parameters)
Parameters are nothing but named variables passed into the function. we can get more clarity while working
def a (x,y)
or
def a x,y
In the above code, a is the function name and x, and y are the variables. We can also define a function without any parameters.
note: It's not at all mandatory to use -->() these braces to place parameters
Body
This is the main thing and heart of any function. It contains the code block which we want to repeat again and again. In place, we can place anything we want even another function also.
we have to follow the indent that we use while working with if and looping statements.
def a (x,y)
#body of the function
end
As it is following the indent we have to finish this by placing the "end" keyword at last.
Return type
It's a basic thing that we are terming as calling a function. As it was called by someone it returns something. The best part of ruby is like python we don't need to define a return type. it explicitly returns something accordingly.
We have to use the return keyword that we want to return after calling and doing some operation in the body of the function.
def a (x,y)
return x+y
end
This is the full structure of defining a function on the code form.
Calling a function.
This is the term that we use while working with function. How do we even call a function? We can relate this with humans if you want to call your friend generally we call them by their names right the same thing is we just place a function name where it's use need.
def a
print "This is executing because it was called from somewhere!"
end
a #calling a function
variable = a #called function value stored in this variable.
We can even store the called value in any variable we want.
Calling a function with parameters
This is part we have to care about while calling because if anything is wrong leads to a lot of damage. for example, if you define a function with 2 parameters and you just called the function with only 1 parameter then obviously it will through an error.
def a (x,y) #x,y are parameters
return x+y
end
puts a(2,4) #2,4 are arguments
output:
6
Technically the value that we pass while calling a function are called arguments and the variables that we place while constructing a function are called parameters.
Case
Just quickly revise if the conditional statement returns true if the condition is true otherwise false. Let's sketch a scenario we want to check 20 conditions to get more specific about the choice it is better to use a case statement rather than if-else. In ruby, we use the "case" instead of the "switch" and "when" in place of the "case".
case expresstion
when expresstion
#code
when expresstion
#code
.
.
.
else
#code
end
Don't forget to write else block at the last otherwise gives an error. close the case statement using the "end" keyword.
n = 5
case n
when 1..2
print "first when"
when 3..5
print "second when"
when 6..8
print "third when"
when 9..10
print "fourth when"
else
print "numbers more than 10"
end
output:
second when
Ternary conditional expression
As we know that conditional expressions are if and elif so fare we learned only these two. Here is another conditional statement whose working is similar to the simple if-else statement but very more optimized way.
syntax: condition? Do this if the condition is true: Do this if the condition false
puts "Enter the number"
num = gets.chomp
puts num == 5 "True":"False"
output:
Enter the number
10
False
Pushy
It was something unrelated to the topic that currently we are learning. But it was a super good technique in many cases. So, in arrays to add an element at the end of the arrays we use the append method to do. We can also do that by using << (shovel). It then also be used in concatenation.
It can be denoted by using this --> "<<"
l = [1,2,3,4,5]
print l << 6 #it adds the 6 at the end of the list
first_name = "Malavi"
second_name = "Pande"
puts first_name << second_name
output:
[1,2,3,4,5,6] MalaviPande
Conclusion
This is all about functions and case statements hope you learn something while reading. I will be back soon with another blog on another interesting topic until and now stay learning and keep growing.