0
How can I pass an argument and return it with this line of code:
20 Réponses
+ 6
For this you need to understand what parameters and arguments are
When you define a function using 'def', what you write inside round brackets () are called parameters. Parameters are like variables which store values we give during function call.
The values we give during function call are arguments.
Lets take an example:
def info(name, age):
Print(name + " " + str(age))
info("Josh", 24)
Here, in line 1 you define a function named info.
'name' and 'age' inside brackets are parameters
In last line, you call the function. 'Josh' and '24' inside brackets are arguments.
So when you run the code, the arguments, 'josh' and '24' are passed to the function where name = Josh and age = 24
+ 4
Your argument, josh is string so you need to put it inside " " or ' '
So when your code runs, 'Josh' and 'Hows everything' go to name and msg respectively and python runs the second line, which is print statement in our case
I think you wanted to write this code :
def greet(name, msg):
print(name + ", " + msg)
greet("Josh", "How is everything?")
+ 4
DN Josh There are 2 issues..
First is you didn't return anything
and second , you didn't pass any argument
return stores the value to the function
For example if you write - return a
This means 'a' is saved to the function
In your case, since you didnt return anything, there's nothing stored in the function and printing the function gives 'None' ..
+ 3
def myfunction (arg1):
return arg1
Print(myfunction(10))
+ 3
Simplest example:
def greet_user():
"""Display a simple greeting."""
print("Hello!")
greet_user()
+ 2
it can be done like this:
def myFunction(arg1):
return arg1 * 2
result = myFunction(7) # call the function with 7 as argument. the value that is returned from the function is stored in variable result
print(result)
# the result is 14
+ 2
Sacar your explanation is beyond comparison. I mean, this is what exactly I was looking for.
+ 2
I finished a whole chapter on function but couldn't understand what they really talked about. I love ur explanation. Thanks for the good contributions. Let me create one or two more programs and you will access them in terms of clarity.
+ 2
Lardaneo ,
# i was not aware that it is possible to have so many issues in few lines of code:
a=“” # what is the purpose of this variable? anyway - it uses wrong quotes. should be ""
def function(a( # the function header needs to have an opening and a closing parenthesis! it also needs a colon at the end of the line
return “a” # what is the purpose of returning the letter "a"? also this line of code needs to have indentation
function(a)<~~~
+ 2
I've found the solution finally.
what my code needed was a quote
"a" and not a
+ 1
define a function is like building a machine. then you use the machine and an argument is what you put inside the machine
+ 1
def myFunction(arg1):
return arg1
x=myFunction("a")
print (x)
You are returning value but you are not saving that value.you should save incoming value in a variable then display that variable simple
0
def myFunction(arg1):
return arg1
print(myFunction(2+2))
0
instead of printing in the function you return the values.
def function(x, y):
return x-y
print(function(3, 2))
outcome will be 1
0
https://code.sololearn.com/c6KzFnlPh2K7/?ref=app
0
you print the function. you are calling it. put an argument in the parenthesis when you call it
0
Stehr you mentioned "pass value" in your explanation.
Could please write a short program that will help in expressing your idea? Thanks 🙏🙏
0
Def function_name(x):
Return x*x
Print(function_name(6))