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

  • SEARCH
  • Home
  • 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 4617316
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:06:26+00:00 2026-05-22T02:06:26+00:00

Is it possible to have a function that takes (int n,bool wantall) and returns

  • 0

Is it possible to have a function that takes (int n,bool wantall) and returns a different type based on the bool?
The example that made me think about this was generate the fibonacci numbers.
If wantall = true, then return an array (or list) of the first n numbers, else, just return the nth number.
Is there a way to do this?

  • 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-05-22T02:06:27+00:00Added an answer on May 22, 2026 at 2:06 am

    No, you can’t change the return type.

    For your example, ideally you should have different overloads for what you are talking about

    GetFirstFibonacciNumbers(int count)
    GetSingleFibonacciNumber(int nth)
    

    Passing in a bool for this is just ugly.

    But if you insist on a single method

    IEnumerable<int> GetFibonacciNumbers(int n, bool wantall) {
        if(!wantall) {
            return new[] { GetSingleFibonacciNumber(n); }
        }
        else {
            return GetFirstFibonacciNumbers(n);
        }
    }
    

    But, please, don’t do this.

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

Sidebar

Related Questions

I have a struct that takes a function pointer, like this: typedef int (*node_transition_func)(
Suppose I have a function which takes some form of predicate: void Foo( boost::function<bool(int,int,int)>
Is it possible to have a PHP function that is both recursive and anonymous?
I have a function that takes an array as a parameter. The function then
Assume that I have a boost::function of with an arbitrary signature called type CallbackType
I have grep function in C( embedded programming ) that takes a void pointer
Is it at all possible to have a table valued function in MSSQL that
Is it possible to create a function that can be called by multiple different
I have a function similar to the following that takes an integer as a
I have a function that takes a parameter and produces a result. Unfortunately, it

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.