How to properly return a function in python?

I am learning python, so I wrote this function, but I see I can’t print the values. How can I retrieve those values outside the function?

txt = "KİMLİK BİLGİLERİ Kimlik No 511345216748 Adı Soyadı : : Marco : Polo Adres Tipi Adres Türü Adres No Adres Yerleşim Yeri Adresi Yurtiçi 28693222128232166 CEREALİ MAH. 17 SK. GATTİ APT. BLOK NO: 24  İÇ KAPI NO: 18 PIOLTELLO / MILANO AÇIKLAMALAR"
def f(text, idcard, name, surname, addressN, addressLong):
  list_word = txt.split()
  for i in range(len(list_word)):
    if list_word[i]=="Kimlik":
      idcard=list_word[i+2]
    if list_word[i]=="Soyadı":
        name=list_word[i+3]
        surname= list_word[i+5]
    if list_word[i]=="Adresi":
      addressN=list_word[i+2]
  start = txt.find("Adresi") + len("Adresi")
  end = txt.find("AÇIKLAMALAR")
  sub = txt[start:end]
  addressLong = ' '.join(sub.split(' ')[3:])
  return idcard, name, surname, addressN, addressLong

idCard =""
name = ""
surname = ""
addressN = ""
addressLong = ""
f(txt, idCard, name, surname, addressN, addressLong)
print("id",idCard,"name", name,"surname", surname,"addressN", addressN,"long", addressLong)

You need to assign the value return from a function to a variable. Since a function call has a value, you can print the returned value directly also.

def add(a, b) :
  return a + b

c = add(10, 30)
print(c)
print(add(4, 8))

Arguments are for passing values to a function. You cannot return a result through parameter in Python.
If you want to return more than one values, then you can do this, for example

def sumprod(a, b):
    return a+b, a*b

s, p = sumprod(5, 8)
1 Like

you need to bind it to a variable if you want the values to persist. in this example, I’ve assigned the return to the variable x

Thank you for your answer!

Thank you for your answer and time!

You’re really passing just one string to the function, so it should be more like

def f(txt):
  ...
  return a, b, c, d, e //with a, b, c, d, e named appropriately

result = f(txt)

Or

a, b, c, d, e = f(txt)  //with a, b, c, d, e renamed appropriately

If you have just two or three return values, you might name them individually instead of using indexing. (You can also take an OOP approach and return an object.)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.