i want more understand about return function. Is return function the function have a return value,what is that mean ? Plis give example and explanation about function have a return value and not have a return value.
return is a statement for functions, so let's say you have a bit of code like print(myFunction()).
myFunction's return value is what print is printing.
so if I have
# define function
def myFunction():
# this gives "Hello World" to whatever runs it
return "Hello World"
# put the return value in a variable
a = myFunction()
# print the variable data
print( a )
when you run a = myFunction the function is returning "Hello World" and storing it in the variable a. then you're just printing value of a.
Think of it this way,
Sometimes you want your function to do something for example add something to a variable:
a = 1
def add_1(x):
x += 1
# This would add 1 to the variable a. No return needed.
add_1(a)
# Now a is 2.
but sometimes you would want a function to return something that you could work with. For example printing it or saving it to another variable:
a = 1
def add_1(x):
return x + 1
b = add_1(a)
# The function returns a + 1, which is 2, and saves it to the variable b.
print (add_1(b))
# The function prints the returned value of b + 1, which is 3 and would print 3.
Hopefully this was helpful.
Related
Suppose f(x,y) is a bivariate function defined along the following lines. So f uses the second variable only conditionally.
def b
.
.
.
def f(x,y):
if b == True
return (x,1)
else
return (x,y)
I want to rewrite f as a univariate function of x which at a later stage calls a function which takes y, but only if necessary!
So I want to curry and write something along the following lines.
def g(x):
if b == True
return (x,1)
else
return f(x,?)
Where f(x,?) is itself the function i_1x(y)=(x,y).
So I want to only inject the second variable conditionally. Evidently g returns two different types, which is likely a problem. We can resolve this artifially by having the first condition returns the function p_1(y)=(x,1), but that would return a function which would still ask for another input.
function [TC]=Translate(T0,Base)
end
I know that Translate is a function and T0 and Base his parameter but what is [TC]?
Octave (and matlab) have a rather unique way of returning variables from functions. Instead of defining explicitly what to return from the function using a return keyword, they define from the outset which variables will be returned when the function exits, and octave simply looks for those variables by name at the time the function exits, and returns their values, whatever they may be by that point.
Your function may return nothing:
function returnsNothing();
disp('hello, I return nothing');
end
or it may return one output:
function Out = returnsOne(x)
Out = x+5
disp('This function will return the value of Out');
end
or it may return more than one outputs:
function [Out1, Out2] = returnsTwo(x)
Out1 = x+5;
Out2 = x+10;
end
You would call the last function from the octave terminal (or script) like this:
[a,b] = returnsTwo(5); % this will make a = 10 and b = 15
I am trying to use both functions below:
def func1():
print('blahblah')
func2()
def func2():
check = 0
while check < 1:
first = input('Integer or pass ')
if first == 'pass':
first = 0
func1()
break
else:
first = int(first)
second = input('Integer or pass')
if second == 'pass':
second = 0
func1()
break
else:
second = int(second)
third = input('Integer or pass' )
if third == 'pass':
third = 0
func1()
break
else:
third = int(third)
check = 1
return first, second, third
The func2 returns None instead of the inputs when once "pass" was entered. What am I doing wrong?
edit: the results should be 3 integers, no matter how many times 'pass' was entered.
After changing the indentation I get following error: UnboundLocalError: local variable 'second' referenced before assignment (in case I start with pass)
For clarifcation: func2 asks for input, if input is "pass" then func1 is called.
After the print func1 calls func2 again . This repeats until 3 integers are input. Their valus shall be returned in th end.
Your indentation is wrong. You need to move the last statement return first, second, third one tab before.
Im trying to return the value of an index of a table which is inside of another table and am doing so using the args given when a function is run so that the arg resolves to a variable name.
function getsubindex(varname,index)
local tbl = {}
tbl.first = 99
tbl.subTbl = {10,20,30}
if not index then
return tbl[varname]
else
return tbl[varname[index]]
end
end
returning tbl[varname] works because you can use a string as an index for example.
getsubindex("first")
Would return 99, but I need to get into the subTbl take this example of running the func.
getsubindex("subTbl",2)
I'm wanting this to return 20
Change:
return tbl[varname[index]]
to:
return tbl[varname][index]
function isEven(x)
print("Checking if "..x.." is even.\nWill return state as 1 if true.")
if math.fmod(x, 2) == 0 then
state = 1
end
return state
end
I know that I can just run isEven and then use the state variable. But is there a way to do it in one line?
Like isEven(8).state?
Any and all help is appreciated.
As Egor said in a comment, this is precisely what return values are meant to do. When you see a function call in your code, such as isEven(8), it evaluates into that function's return value.
function isEven(x)
print("Checking if "..x.." is even")
return (math.fmod(x, 2) == 0)
end
print( isEven(8) )
print( isEven(7) )
if isEven(8) then
print("a")
else
print("b")
end
Finally, I would just like to point out a couple of things about the isEven function: First of all if you want you could use the % operator instead of math.fmod. Secondly, in the example I used the function returns a boolean value (true or false) instead of a number (0 or 1).