Python Forum
Functions: why use a variable "display" in a UDF as a Boolean
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Functions: why use a variable "display" in a UDF as a Boolean
#1
Hello,
Can someone explain why the following code includes the "display" variable in the function?

def full_name(first, middle, last, display):
      name = first + " " + middle + " " + last
      if display:
             print(name)
      return name
to call function:
full_name("John", "W", "Doe", True)
complete_name = full_name("John", "W", "Doe", False)
print(complete_name)
Also, is it necessary to use the complete_name line of code?
Couldn't you just use print(full_name("John", "W", "Doe")
buran write May-25-2025, 05:22 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
If True it prints the name

def full_name(first, middle, last, display):
    name = f"{first} {middle} {last}"
    if display:
        return name


complete_name = full_name("John", "W", "Doe", False)
print(complete_name)

complete_name = full_name("John", "W", "Doe", True)
print(complete_name)
Reply
#3
It does so because the author thought it should. It is not a design decision I would make.
buran likes this post
Reply
#4
A function should return a value or print something, without returning anything.
If both is the case, your function has unwanted side effects.
Maybe the author of this function knows this and introduced the ability not to print, if the function is used.

But still, it's annoying if a function prints something, if you expect just a return value.
Sometime libraries are using excessive logging functions, which is also annoying, if activated by default. Then you've to deactivate this manually, if you do not want to have debug output in your program. Most people do not want this.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling functions by making part of their name with variable crouzilles 4 2,015 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  How to display <IPython.core.display.HTML object>? pythopen 3 51,284 May-06-2023, 08:14 AM
Last Post: pramod08728
  Whys is asterisk and random variable necessary in these functions? rrowhe4d 5 2,785 Aug-05-2022, 07:53 AM
Last Post: Gribouillis
  Store variable data and display sum after 60 seconds the_dude 11 5,935 Dec-16-2021, 07:07 PM
Last Post: deanhystad
Information Unable to display joystick's value from Python onto display box MelfoyGray 2 3,205 Nov-11-2020, 02:23 AM
Last Post: MelfoyGray
  Display Globar Variable N0m1t 1 2,257 Mar-19-2019, 10:29 PM
Last Post: metulburr
  How do you make functions that take a variable that is not defined? magic 6 6,118 Sep-24-2018, 01:30 PM
Last Post: gruntfutuk
  Boolean: if variable is capitalized Truman 9 19,399 Jan-09-2018, 11:04 PM
Last Post: Gribouillis
  Returning a Boolean operator and a variable both? PythonAndArduino 2 3,684 Nov-08-2017, 08:31 PM
Last Post: PythonAndArduino

Forum Jump:

User Panel Messages

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