Problem returning a value in function python

python function return variable
python function return multiple values
python multiple return statements
how to use return value in another function python
python function return type
function with argument and return value in python
write a function called same_thing that returns the parameter, unchanged
how to access return value in python

I´m trying to return the current time but for some reason I cant. I return this:

<function getHorarioActual at 0x000001E292E53950>

My function is quite simple

def getHorarioActual():
    now = datetime.datetime.now()

    horaActual = now.hour
    minutoActual = now.minute
    auxiliar = str(horaActual)+":"+str(minutoActual)
    horarioActual = auxiliar

    return horarioActual

You don't need all that variables to format the time in HH:MM. Your function can be defined just as:

from datetime import datetime

def getHorarioActual():
    return datetime.now().strftime('%H:%M')

Here is a proof of concept:

Python 3.7.5 (default, Oct 17 2019, 12:16:48) 
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> def getHorarioActual():
...     return datetime.now().strftime('%H:%M')
... 
>>> getHorarioActual()
'19:05'
>>>

6.2. Functions that Return Values — How to Think like a Computer , Python has a built-in function for computing the absolute value: Furthermore, functions like range , int , abs all return values that can be used to build more The problem with this function is that even though it prints the value of the square, � return means, "output this value from this function". print means, "send this value to (generally) stdout" In the Python REPL, a function return will be output to the screen by default (this isn't quite the same as print). This is an example of print: >>> n = "foo bar" #just assigning a variable.

You are apparently not calling the function properly, writing

x = getHorarioActual

rather than

x = getHorarioActual()

12.5. Returning a value from a function — Foundations of Python , The problem with this function is that even though it prints the value of the squared input, that value will not be returned to the place where the call was done. You may want to split a complex problem into several smaller ones. You are already familiar with loops or iterations. In some situations recursion may be a better solution. In Python, a function is recursive if it calls itself and has a termination condition. Why a termination condition? To stop the function from calling itself ad infinity.

import datetime

def getHorarioActual():
    now = datetime.datetime.now()

    horaActual = now.hour
    minutoActual = now.minute
    auxiliar = str(horaActual)+":"+str(minutoActual)
    horarioActual = auxiliar

    return horarioActual

t = getHorarioActual()
print(t)

Learning with Python 3, But if the function should return a value, make sure all execution paths do find the second problem above, consider what happens if you call this function with� Some try to solve this problem by returning a tuple, list or dict with the required values after the function terminates. It is one way to do it and works like a charm: It is one way to do it and works like a charm:

Python Tutorial: Functions, Calling this function once without an argument, returns the expected result: what is going on, but you may ask yourself how to overcome this problem. Any function containing a yield keyword is a generator function; this is detected by Python’s bytecode compiler which compiles the function specially as a result. When you call a generator function, it doesn’t return a single value; instead it returns a generator object that supports the iterator protocol.

Python return statement, A return statement is used to end the execution of the function call and “returns” the result (value of the In Python, we can return multiple values from a function. The return statement defines the value returned by calling a function. It does not return the name, only the value pointed to. def testing(): test = 1 # test refers to the value 1 return test # return test => value 1 The value returned by a function is like any other value - be it from a literal, lookup, or other.

Python Return Statements Explained: What They Are and Why You , All functions return a value when called. If a return statement is followed by an expression list, that expression list is evaluated and the value is� Discussion / Question python - Function returning a value 259751/function-returning-a-value-multi function in python ; Thread and queue problem in call

Comments
  • Please include your call to getHorarioActual
  • You aren't calling the function. Also you can just return the expression, both auxiliar and the reassignment to horarioActual are pointless.
  • You also don't need to do this from scratch. Just use datetime.datetime.now().strftime("%H:%M").
  • Variable names should follow the lower_case_with_underscores convention, not camelCase or anything else. Please see PEP 8 for more. Also, this doesn't look like it's your full code. Can you share a minimal reproducible example?