# 関数の定義は誰でもできる def max( x, y ) if x >= y then return x else return y end end i = 3; j = 5 x = max( i, j ) puts( "#{x} is the maximum of #{i} and #{j}" ) # 関数の定義と呼び出し@ def max( x, y ) if x >= y then return x else return y end end i = 3; j = 5 x = max( i, j ) puts( "#{x} is the maximum of #{i} and #{j}" ) i = 10; j = 15 x = max( i, j ) puts( "#{x} is the maximum of #{i} and #{j}" ) i = 5; j = -9 x = max( i, j ) puts( "#{x} is the maximum of #{i} and #{j}" ) # 関数の定義と呼び出しA def f( x, y ) z = x*x+y*y return z end (0..9).each{ |x| (0..9).each{ |y| print( " x = " , x , " y= " , y , " f= " , f( x , y ) , "\n" ) } } # 関数の定義と呼び出しB def f( x, y, z ) w = x*x+y*y+z*z return w end (0..9).each{ |x| (0..9).each{ |y| (0..9).each{ |z| print( " x = " , x , " y= " , y , " z= " , z , " f= " , f( x , y , z ) , "\n" ) } } } # 2数の最小値を値とする関数 def min( x, y) if x <= y then return x else return y end end i = 3; j = 5 x = min( i, j ) puts( "#{x} is the minimum of #{i} and #{j}" ) # 3数の最大値を値とする関数 def max( x, y, z ) if ( x >= y ) then if ( y >= z ) then return x else if ( x >= z ) then return x else return z end end else if ( x >= z ) then return y else if ( y >= z ) then return y else return z end end end end i = 3; j = 5; k = 7 x = max( i, j, k ) puts( "#{x} is the maximum of #{i}, #{j}, and #{k}" ) # 関数が複数個定義されている場合 def max( x, y) if x >= y then return x else return y end end def min( x, y) if x <= y then return x else return y end end i = 3; j = 5 puts( "#{max( i, j )} is the maximum of #{i} and #{j}" ) puts( "#{min( i, j )} is the minimum of #{i} and #{j}" ) # 関数は関数を使えるか?@ def max2( x, y) if x >= y then return x else return y end end def max3( x, y, z ) return max2( x, max2( y, z) ) end i = 3; j = 5; k = 7 puts( "#{max2( i, j )} is the maximum of #{i} and #{j}" ) puts( "#{max3( i, j, k )} is the maximum of #{i}, #{j}, and #{k}" ) # 関数は関数を使えるか?A def max2( x, y) if x >= y then return x else return y end end def max4( x, y, z, u ) return max2( max2( x, y) , max2( z, u) ) end i = 3; j = 5; k = 7; l = 9 puts( "#{max2( i, j )} is the maximum of #{i} and #{j}" ) puts( "#{max4( i, j, k, l )} is the maximum of #{i}, #{j}, #{k}, and #{l}" ) # (注意!)変数に値を戻すことはできない@ def f( x , y ) x = x + 1 y = y + 1 end a = 3 b = 5 print( " a = " , a , " " , " b= " , b , "\n" ) f( a , b ) print( " a = " , a , " " , " b= " , b , "\n" ) # (注意!)変数に値を戻すことはできないA def f( x , y ) x = x + 1 y = y + 1 end x = 3 y = 5 print( " x = " , x , " " , " y= " , y , "\n" ) f( x , y ) print( " x = " , x , " " , " y= " , y , "\n" ) # 戻り値のない関数 def sayHello( n ) n.times{ |i| i.times{    print( " " ) } puts( "Hello!" ) } end sayHello( 4 ) # 戻り値のない関数の例@ def max( x, y ) if x >= y then z = x else z = y end puts( "#{z} is the maximum of #{x} and #{y}" ) end i = 3; j = 5 max( i, j ) i = 9; j = 15 max( i, j ) i = -5; j = -7 max( i, j ) # 戻り値のない関数の例A def max( x, y ) if x >= y then z = x else z = y end puts( "#{z} is the maximum of #{x} and #{y}" ) end i = 3; j = 5 k = max( i, j ) print( " k= " , k , "\n" ) # 戻り値のない関数の例B def f( x ) if x % 2 == 0 then print( x , "は偶数です\n" ) else print( x , "は奇数です\n" ) end end n = 15 f( n ) # 引数も戻り値もない関数 def max() x = 3 y = 5 if x >= y then z = x else z = y end puts( "#{z} is the maximum of #{x} and #{y}" ) end max() # 配列を引数として受け取る関数@ def average( a ) s=0.0 a.each{|x| s += x } return s/a.length end x = [1,2,3,4,5,6,7,8,9,10] print( "#{average( x )} is the average of: "); p( x ) # 配列を引数として受け取る関数A def average( a ) s=0.0 a.each{|x| s += x } return s/a.length end def var( a ) ave = average( a ) s = 0.0 a.each{ |x| s += ( x - ave ) * ( x - ave ) } return s /(a.length-1) end x = [1,2,3,4,5,6,7,8,9,10] print( "#{average( x )} is the average of: "); p( x ) print( "#{var( x )} is the variance of: "); p( x ) # 配列を引数として受け取る関数B def inner_product( a , b ) s = 0 a.length.times{ |i| s += a[ i ] * b[ i ] } return s end x = [1,2,3,4,5] y= [6,7,8,9,10] print( inner_product( x , y ) , "\n" )