Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9167365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:22:22+00:00 2026-06-17T15:22:22+00:00

How do you create a function which will return different things depending whether it

  • 0

How do you create a function which will return different things depending whether it is called on its own or within another function? My example is that in a function: make_wave_snapshot(size,wavelength,phase), it returns a 2-d array and also diplays a greyscale image. The other function: make_wave_sequence(size,wavelength,nsteps) returns a 3-d array and also a greyscale image that automatically cycles through the greyscale images of each step.

def make_wave_snapshot(size,wavelength,phase):
    waves_array = np.zeros((size,size),np.float)
        if size%2==0:
            for y in range(size):
                for x in range(size):
                    r = math.hypot((size/2 - x - 0.5),(size/2 - y - 0.5))
                    d = np.sin((2*math.pi*r/wavelength)-phase)/np.sqrt(r)
                    waves_array[y,x] = d
  !!!       # dp.display_2d_array(waves_array) #Shows visual representation
            return waves_array #Displays array showing values
        else:
            return 'Please use integer of size.'

def make_wave_sequence(size,wavelength,nsteps):
    waves_sequence = np.zeros((nsteps,size,size),np.float)
        if nsteps%1==0:
            for z in range(nsteps):
                waves_sequence[z] = make_wave_snapshot(size,wavelength,(2*math.pi*z/nsteps))
  !!!        #  dp.display_3d_array(waves_sequence)
                return waves_sequence #Displays array showing values
            else:
                return 'Please use positive integer for number of steps'

I know of if name = main and I think it would probably be the answer, but it’s just something I’ve been told is good for checking functions and I don’t have any idea how to fit it into an actual function. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:22:24+00:00Added an answer on June 17, 2026 at 3:22 pm

    How do you create a function which will return different things depending whether it is called on its own or within another function?

    You don’t. And it’s not even required for your use case.

    This would in theory be possible by using the inspect module or other ways of checking stack frames, but it would be an extremely bad idea. A function should be predictable and do the same thing regardless of where it’s called.

    Also note that checking __name__ won’t help you because it contains the module name (or __main__ if the script is the entry point) and does not have anything to do with the scope a function is called in.

    If you want your function to do two different things, one way would be to use an extra argument defaulting to None, which you use as a flag to indicate if the function should follow the second path:

    def example(a, b, do_b=None):
        if do_b is None:
            return a
        else:
            return b
    

    In your case (if I understand it correctly), you don’t want to display the image when calling make_wave_snapshot inside of make_wave_sequence. This has nothing to do with return values and can be solved by using adding an extra argument to the function as described above:

    def make_wave_snapshot(size,wavelength,phase,display=True):
        #your code here
        #...
        if display:
            dp.display_2d_array(waves_array) #Shows visual representation
    

    And now you simply call it with an extra False inside of the other function:

    waves_sequence[z] = make_wave_snapshot(size,wavelength,(2*math.pi*z/nsteps), False)
    

    Oh, and please never use return values as an indication of error conditions as you’re doing in the else case of your loops. You’re not writing C here, python is a civilized language that has Exceptions – use them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a function which will give me alphabet position when
I am trying to create a function which will take arguments arg1, arg2... then
I am currently trying to create a function which will allow me to pass
I am trying to create a vimrc function which will clean up a line
I have function some_func_1 which will create an object of type some_type and will
I'm currently writing a function what would create a zip file, which will be
All, I have the following array and function, which will create a multidimensional array
I want to create a function that can take different types of iterators which
I'd like to write a function that will create and return a set of
I'm trying to create a function in C# which will allow me to, when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.