Basic knowledge in method definition

Method definition

The basic part of defining a method We will share it here as an output! !!

About the method

In the first place, ** method ** is a collection of some processing in programming, and you can execute the processing of the described method by describing the name of the method.

For example, if you write ** length method **, you can output the number of characters in the string.

irb(main):001:0> "baseball".length

=> 8

Method definition

Method definition description

There are many such methods in advance, but you can create them by defining them as well as the existing ones. When creating the method, write as follows.

def method name
  #Code of processing to execute
end

If you try to create an example sentence based on this Example sentence ↓

def my_hobby
  puts "My hobby is watching professional baseball games."
end

my_hobby
#Output result
My hobby is watching professional baseball games.

By defining a method called my_hobby as above By writing my_hobby, you can execute the process of outputting "My hobby is watching professional baseball games."!

The order in which the code is read is usually from the top, but if there is a defined method, the description of that method is passed through without being read. In the above example, the process is executed by calling my_hobby, which is a method defined when it is written as my_hobby and loaded.

The final value of the executed method

The value that is output when a method is defined and the processing of that method is executed is called ** return value ** or ** return value **, and is referred to in the above example sentence. ** "My hobby is watching professional baseball games." ** The output result is the return value.

Also, when using the length method, the return value will be the number output as the number of characters.

And for the defined method, the result of the processing of the last line of the processing in the method is output as a return value.

Example sentence ↓

def week
  "Sunday"
  "Monday"
  "Tuesday"
  "Wednesday"
  "Thursday"
  "Friday"
  "Saturday"  #The last line is the return value!
end

puts week
# => Saturday

In this way, the last line is output for the processing in the method, You can specify the return value by writing a ** return statement ** in the method!

Example sentence ↓

def week
  "Sunday"
  "Monday"

  return "Tuesday"  #This will be the return value and the process will end!

  "Wednesday"  #Not called
  "Thursday"  #Not called
  "Friday"   #Not called
  "Saturday"  #Not called
end

puts week
# => Tuesday

By specifying return as described above, the part to be processed can be specified as the return value, and the part that describes return is output.

Possible range of values

The range in which the defined variable can be used is limited, and that range is called ** scope **. (1) Variables defined outside the defined method cannot be used inside the method, and vice versa (2) Variables inside the defined method cannot be used outside the method An error will occur.

Example sentence in case of ① ↓

def introduce
  puts name
end

name = "Kinoshita"
introduce

# =>error!

Example sentence in case of ② ↓

def introduce
  name = "Kinoshita"
end

puts name

# =>error!

In either case, the variables defined because they are out of scope cannot be used, resulting in an error. To use a value defined outside the scope of this, use the value ** argument (hikisu) **. To use the argument, write as follows.

Example sentence ↓

def method name(Formal argument)
  #Description of processing
end

Method name(Actual argument)

** Formal argument ** is an argument that is described when defining a method and used during processing. ** Actual argument ** is an argument that describes the value received when calling the method. Also, multiple arguments can be used, in which case Describe as method name (first argument, second argument).

The names of the formal and actual arguments do not necessarily have to match, but the number of arguments must ** match. ** **

Let's use an argument to solve the error in the above example sentence. Example sentence ↓

def introduce(name)
  puts name
end

teacher = "Kinoshita"
introduce(teacher)

# => "Kinoshita"

As described above, by describing the teacher defined outside the introduce method as an actual argument, the variable teacher defined outside the method is passed as the formal argument name in the introduce method, and the processing inside the method is executed. .. This makes variables defined outside the method available!

Recommended Posts

Basic knowledge in method definition
Basic knowledge
Concurrency Method in Java with basic example
Java basic knowledge 1
[Servlet] Basic method
java basic knowledge memo
definition of ruby method
[Ruby] Method definition summary
[Java] Basic method notes
Basic knowledge of SQL statements
Ruby algorithm (inject, method definition)
Implement Basic authentication in Java
[Java] Basic terms in programming
Run LibreOffice Basic macros in Docker
Automatic photo resizing method in Java
Basic knowledge of Ruby on Rails
String output method memo in Ruby
[Rails] About helper method form_with [Basic]
[Swift] Protocol concept and definition method
[Ruby] undefined method `dark?'occurs in rqr_code
[Basic knowledge of Java] Scope of variables
Call the super method in Java