# 関数の定義は誰でもできる 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" ) # 配列を戻り値として返す関数@ def add( a , b ) c = [] a.length.times{ |i| c[ i ] = a[ i ] + b[ i ] } return c end x = [1,2,3,4,5] y= [6,7,8,9,10] z = add( x , y ) p z # 配列を戻り値として返す関数A def min_max( a ) max = a[ 0 ] min = a[ 0 ] (1..a.length-1).each{ |i| if max < a[ i ] then max = a[ i ] end if min > a[ i ] then min = a[ i ] end } c = [] c[ 0 ] = min c[ 1 ] = max return c end x = [1,2,5,3,4] z = min_max( x ) p z # 二次元配列を引数として受け取る関数@ def Average( x ) s = 0.0 count = 0 (0..x.length-1).each{ |i| (0..x[i].length-1).each{ |j| s += x[i][j] count += 1 } } return s/count end d = [ [1,2,3], [4,5,6], [7,8,9] ] a = Average( d ) print( a , "\n" ) # 二次元配列を引数として受け取る関数A def getAverage( x ) y = Array.new( x.length ) (0..x.length-1).each{ |i| s = 0.0 (0..x[i].length-1).each{ |j| s += x[i][j] } y[i] = s/x.length; } return y end d = [ [1,2,3], [4,5,6], [7,8,9] ] a = getAverage( d ) (0..d.length-1).each{ |i| print( "#{a[i]} is the average of: ") p( d[i] ) } # 二次元配列を戻り値として返す関数 def Mul( x , y ) z = [] (0..x.length-1).each{ |i| z[ i ] = [] (0..x[i].length-1).each{ |j| z[ i ][ j ] = y * x[i][j] } } return z end c = [ [1,2,3], [4,5,6], [7,8,9] ] d = 3 a = Mul( c , d ) p a # 変数に値を戻すことはできない def getAverage( x, y ) s = 0.0 (0..x.length-1).each{ |j| s += x[j] } y = s/x.length; end d = [1,2,3] a = 0.0 getAverage( d, a ) print( "#{a} is??? the average of: "); p( d ) # でも配列に結果を戻すことはできる def getAverage( x, y ) (0..x.length-1).each{ |i| s = 0.0 (0..x[i].length-1).each{ |j| s = s + x[i][j] } y[i] = s/x.length } end d = [ [1,2,3], [4,5,6], [7,8,9] ] a = Array.new( d.length ) getAverage( d, a ) (0..d.length-1).each{ |i| print( "#{a[i]} is the average of: ") p( d[i] ) }