Python Forum
Help with Python functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Python functions
#1
Hi

I am trying one of the HackerRank challenges.

I have the following code in Python as part of a problem. What I need to do is have an output that says "Hello <firstname> <lastname>! You have just delved into Python"

Code:

def print_full_name(a, b):
 

if __name__ == '__main__':
    first_name = input()
    last_name = input()
    print_full_name(first_name, last_name)
So my understanding is that there is a function called print_full_name that is called in the second chunk of code. Prior to the function, the code asks for input; first_name and last_name

So I could write:

   print(f'Hello {first_name} {last_name}! You just delved into python.')
Which seems to work. What I don't understand is how a and b get into the picture. How does a and b relate to first_name and last_name? Because the code also works if I try the below

  print(f'Hello {a} {b}! You just delved into python.')
buran write Nov-15-2020, 08:54 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Maybe this helps: What is the difference between arguments and parameters?

Quote:Parameters are defined by the names that appear in a function definition, whereas arguments are the values actually passed to a function when calling it.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Thanks. So a and b are parameters, whereas first_name and last_name are arguments?

I still don't understand how a is related to first_name and b to last_name?
Reply
#4
It comes down to the scope of variables. This is a topic that may take a bit of time but is incredibly important to understand when programming in any language. I suggest starting HERE
Reply
#5
There are two kinds of function arguments: positional and named. In this example the determination is made by the caller. The first time the function gets called all arguments are passed by position. The second time half the arguments are passed by name and half by position.
def python_function(*args, **kvargs):
    print('\nPositional arguments', args)
    print('Named arguments', kvargs)

python_function(1, 2, 3, 4)
python_function('a', 'b', c='see', d='dee')
Output:
Positional arguments (1, 2, 3, 4) Named arguments {} Positional arguments ('a', 'b') Named arguments {'c': 'see', 'd': 'dee'}
Notice the mapping between the position argument and parameter is by position. The first parameter in the function call is the value of the first position argument in the function.

You can also see the "variable=value" argument syntax in the function arguments. This lets you provide a default value for an argument. If no parameter is provided for the argument the default value gets used.
def python_function(a, b, c='cee', d='dee'):
    print(a, b, c, d)

python_function(1, 2)
ppython_function(1, 2, 3, 4)
python_function(d=1, c=2, b=3, a=4)
Output:
1 2 cee dee 1 2 3 4 4 3 2 1
And finally, you can change parameter/argument mapping by using named arguments. In the last function call the names are used to pass 1 as the value for function argument "d".
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020