Functions

Hi guys i did not really understand this code why when i call my_children(children_list) i have to put the argument children_list and when i defined my function i did not put my argument as children_list but instead i wrote children . How is it connected

children_list = ["far","ina","ilm","haad"]

def my_children(children):
  first_1 = "My first son is " + children[0]
  second_2 = "My second doughter is " + children[1]
  third_3 = "My third doughter is  " + children[2]
  fourth_4 ="My fourth son is " + children[3]
  return first_1, second_2, third_3, fourth_4


older1,older2,older3,older4 =  my_children(children_list)



print(older1)
print(older2)
print(older3)
print(older4)

What happens is whatever data you pass into the function as an argument, regardless of the name of variable passed in, becomes assigned to the parameter (in this case children, which you can use to access it. There isn’t a requirement for the parameter and the actual variable passed in to have the same name since you just use the name of the parameter anyways.

This is important because the use of a function is a chunk of code that should be repeated, often with different inputs, so it needs to be able to handle having different names for the inputted variable.

For example:

a = "a"
b = "b"

def print_letter(letter):
    print(letter)

# All of these are valid ways of calling the function, even though none of them are
# called "letter".
print_letter(a)
print_letter(b)
print_letter("c")
3 Likes