Python Forum
Calling function-- how to call simply return value, not whole process
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling function-- how to call simply return value, not whole process
#1
Hello,

The program I am creating will take in a value produced by the user and output both that value and the square of the value. I am to use functions for both intaking and squaring the value. However, my output returns the "Enter a number: " statement twice, leading me to believe that when I invoke the getValue() function in my "The square of the value is " statement in main I am calling beyond the value and going for the whole process of getValue. How do I forego this and simply return the value? Output is as follows currently, code below:

Enter a value: 2
The value entered is 2
Enter a value: 2
The square of the value is 4

def getValue():
    val = int(input("Enter a number: "))
    return val
def squareValue():
    val = getValue()

    square = val * val
    return square

print("The value entered is", getValue())
print("The value squared is", squareValue())
Reply
#2
It asks for a value twice because you call getValue twice. First it is called on line 10. Then on line 11 you call squareValue. The squareValue function calls getValue again on line 5.

The easy way to fix this is to remove line 10. Then you only call it once.

If you want to retain the output on line 10, there are a few ways to do it. I would suggest first changing squareValue so it accepts val as a parameter:

def squareValue(val):
    square = val * val
    return square
Then change the last couple lines to save the value from getValue and pass it to squareValue:

value = getValue()
print("The value entered is", value)
print("The value squared is", squareValue(value))
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
I figured it out! Can't believe I forgot to put the value in the formal parameters:/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Use of function/return Paulman 6 2,371 Oct-24-2021, 11:07 PM
Last Post: Paulman
  Multiple return from function Grimmar 7 3,567 Mar-22-2021, 09:20 PM
Last Post: Grimmar
  can you call a function from a list? KEYS 20 6,194 Nov-10-2020, 06:36 PM
Last Post: KEYS
  Lambda function not return value mbilalshafiq 4 3,320 Jul-04-2020, 07:47 AM
Last Post: ndc85430
  Child class function of Log return "None" mbilalshafiq 2 2,222 Jun-30-2020, 07:22 PM
Last Post: mbilalshafiq
  Question on "define function"; difference between return and print extricate 10 4,720 Jun-09-2020, 08:56 PM
Last Post: jefsummers
  Programming (identifier, literal and function call) ledangereux 5 4,981 May-05-2020, 12:37 PM
Last Post: gumi543
  [split] problem with function return value ops 1 3,349 Apr-13-2020, 01:48 PM
Last Post: buran
  Function to return today's month, day, and year sbabu 9 4,927 Jan-28-2020, 06:20 PM
Last Post: snippsat
  Calling a function from another function johneven 2 2,887 Jul-09-2019, 12:42 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020