+ 1
What is function argument
Explain it simply with a an example
3 Respostas
+ 4
Example (Python 3.6):
# Declaring function with two parameters
def sum(a, b): # Parameters are a, b
print(a + b)
# Now we call function sum with two arguments
sum(5, 10) # Arguments are 5, 10
# Note that we need to pass the same amount of arguments as the amount of the parameters.
# Wrong calls of our sum function:
sum() # No arguments
sum(5) # One argument
sum(5, 10, 15) # Three arguments
+ 6
When you call print(“hi”), it knows what to print because you gave it the argument, “hi”. It works the same way with functions you define. Inside the function, those arguments act as new variables that get deleted when the function ends. At the beginning, they are given the values of the arguments passed to the function. After that, you can use them just like variables inside the function.
+ 2
The variables that are used in the FUNCTION are commonly called Parameters...
And the variables/values that are passed to the function is called Arguments....
eg.:
def fnsub(a,b):
return a-b
#function call
print(fnsub(10,5))
where the variables a and b are used with in the function...as Parameters.
and the values 10 and 5 are passed to the function...as Arguments...