How can I pass an argument and return it with this line of code: | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can I pass an argument and return it with this line of code:

https://code.sololearn.com/cIuX696724Gm/?ref=app

27th Aug 2021, 7:46 AM
DN Josh
DN Josh - avatar
20 Answers
+ 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
27th Aug 2021, 9:46 AM
Sacar
Sacar - avatar
+ 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?")
27th Aug 2021, 10:15 AM
Sacar
Sacar - avatar
+ 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' ..
27th Aug 2021, 11:21 AM
Sacar
Sacar - avatar
+ 3
def myfunction (arg1): return arg1 Print(myfunction(10))
27th Aug 2021, 8:36 AM
Sacar
Sacar - avatar
+ 3
Simplest example: def greet_user(): """Display a simple greeting.""" print("Hello!") greet_user()
27th Aug 2021, 10:21 AM
Sacar
Sacar - avatar
+ 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
27th Aug 2021, 8:05 AM
Lothar
Lothar - avatar
+ 2
Sacar your explanation is beyond comparison. I mean, this is what exactly I was looking for.
27th Aug 2021, 9:59 AM
DN Josh
DN Josh - avatar
+ 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.
27th Aug 2021, 10:01 AM
DN Josh
DN Josh - avatar
+ 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)<~~~
27th Aug 2021, 10:50 AM
Lothar
Lothar - avatar
+ 2
I've found the solution finally. what my code needed was a quote "a" and not a
28th Aug 2021, 12:47 PM
DN Josh
DN Josh - avatar
+ 1
define a function is like building a machine. then you use the machine and an argument is what you put inside the machine
27th Aug 2021, 9:29 AM
Lardaneo
Lardaneo - avatar
+ 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
28th Aug 2021, 6:38 PM
Muhammad Binyameen
0
def myFunction(arg1): return arg1 print(myFunction(2+2))
27th Aug 2021, 8:14 AM
Lardaneo
Lardaneo - avatar
0
Lothar ,Sacar and Lardaneo please you guys should help me with the explanation of "pass an argument". I'm confused with the term
27th Aug 2021, 9:27 AM
DN Josh
DN Josh - avatar
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
27th Aug 2021, 9:53 AM
Lardaneo
Lardaneo - avatar
0
https://code.sololearn.com/c6KzFnlPh2K7/?ref=app
27th Aug 2021, 11:06 AM
DN Josh
DN Josh - avatar
0
you print the function. you are calling it. put an argument in the parenthesis when you call it
27th Aug 2021, 11:11 AM
Lardaneo
Lardaneo - avatar
0
Stehr you mentioned "pass value" in your explanation. Could please write a short program that will help in expressing your idea? Thanks 🙏🙏
27th Aug 2021, 1:35 PM
DN Josh
DN Josh - avatar
0
Def function_name(x): Return x*x Print(function_name(6))
28th Aug 2021, 4:49 PM
khààlid Mohamud
khààlid Mohamud - avatar