LoginSignup
0
0

メソッドの練習

Posted at

これだと実行するとエラーになるので、
なんでエラーになるのかの理由と、これから引数を使った正しいソースコードを書く。

stock_price =1000

def stock_price_with_corporate_tax
  corporate_tax = 0.3
  return stock_price + stock_price * corporate_tax
end

stock_price_with_corporate_tax

エラーが起きた原因は、stock_price=1000がメソッド内で使われていたから。
引数を使い訂正した正しいコードはこちら。

stock_price =1000

def stock_price_with_corporate_tax(stock_price)
  corporate_tax = 0.3
  return stock_price + stock_price * corporate_tax
end

stock_price_with_corporate_tax(stock_price)

メソッド外の変数はメソッド内では使えない。
def定義の外で中の値を使うとエラーになるので引数が必要になる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0