I do not know why the output is like this!

I ran two almost identical programmes but the output changed. But I don't understand why the output changes. I would like to know. First Program
def greet(name: String):
name = "Hello, " + name + "!"
print(name)

fn main():
var name = "Mojo"
try:
greet(name)
except e:
print("Error: " , e)
def greet(name: String):
name = "Hello, " + name + "!"
print(name)

fn main():
var name = "Mojo"
try:
greet(name)
except e:
print("Error: " , e)
First output
Hello, Mojo!
Hello, Mojo!
Seccond Program
def greet(name):
name = "Hello, " + name + "!"
print(name)

fn main():
var name = "Mojo"
try:
greet(name)
except e:
print("Error: " , e)
def greet(name):
name = "Hello, " + name + "!"
print(name)

fn main():
var name = "Mojo"
try:
greet(name)
except e:
print("Error: " , e)
Seccond output
'Hello, Mojo!
'Hello, Mojo!
Using my environment ・MacOS ・Python3.12 ・Poetry ・mojo 24.4.0 (2cb57382)
1 Reply
Brad Larson
Brad Larson5mo ago
In the first, you've provided a type to the name argument (String), whereas in the second you've left name untyped. In the latter case, Mojo will default to object for the argument in a def function. This causes the slightly different printing behavior between the two types.
Want results from more Discord servers?
Add your server