I have a basic question below to help try get my head around functions in python (following the LPTHW tutorials in prep for uni). Could someone explain the syntax below, and
what is the purpose of having the arg1, arg2 in the parenthesis next to it?
arg1 and arg2 are the names of the inputs and from there the function can use those inputs. In your case what your function does is to print them. Other functions can do other things with these arguments. But let's go step by step.
def print_two_again(arg1, arg2):
print "arg1: %r, arg2: %r" % (arg1, arg2)
In the first two lines starting with def, you define a function. It does something. In your case prints the two arguments it takes.
print_two_again("Steve","Testing")
On the third line what you actually do is to call that function. When you call that function you tell the function to pass "Steve" and "Testing" arguments to the function definition.
Above line is literally called a function call. Let's say you have a program and you want it to print two words. You need to define how you want it to be done. This is called function definition, where you define how things work. That's OK, but not enough. You would want to make it happen. So what you do is to execute that function. This is called a function call.
print_two_again("First","Call")
print_two_again("Second","Call")
In the above lines, what we did is to call the previously defined function two times but with different arguments.
Now let's look at the second line, which probably confuses you.
print "arg1: %r, arg2: %r" % (arg1, arg2)
print is a built-in function in Python. What above line does here is to pass arg1 and arg2 arguments and print them with the format of "arg1: %r, arg2: %r"