on (1) 最初の行で関数を定義します。
on (2) 5 行目で同じ関数を定義します。
どちらが速いですか?必要なときに「近い」関数を定義することは、大規模なプログラムの複雑さのために実用的ではありません(ただし、高速になります)。または、必要な場所から「遠くに」関数を定義すると、実行時に大規模なプログラムが遅くなります(ただし、より実用的です)。
私はパイソンを使用しています。もちろん、ここでの違いはおそらくナノ秒単位ですが、これは単なる例です。
1-
def t(n1,n2):
v=n1-n2
return abs(v)
a = int(input('how old are you? \n'))
b = int(input('how old is your best friend? \n'))
c=t(a,b)
if a==b:
print ('you are both the same age')
else:
print('you are not the same age\nthe difference of years is %s year(s)' % c)
input()
2-
a = int(input('how old are you? \n'))
b = int(input('how old is your best friend? \n'))
def t(n1,n2):
v=n1-n2
return abs(v)
c=t(a,b)
if a==b:
print ('you are both the same age')
else:
print('you are not the same age\nthe difference of years is %s year(s)' % c)
input()