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 8511107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:53:46+00:00 2026-06-11T03:53:46+00:00

I am not a big programming guy but have listen from programmers a lot

  • 0

I am not a big programming guy but have listen from programmers a lot of times that we should always return values from a function. I want to know the reason.

  • 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-11T03:53:48+00:00Added an answer on June 11, 2026 at 3:53 am

    A function needn’t return anything… If you look at C(++) function, many of them don’t (well, not explicitly):

    void nonReturningFunction(const int *someParam);
    int main()
    {
        int i = 12;
        nonReturningFunction(&i);
        return 0;
    }
    void nonReturningFunction(const int *someParam)
    {
        printf("I print the value of a parameter: %d",someParam);
    }
    

    The latter returns nothing, well, it returns a void. The main function does return something: 0, this is generally a signal to the system to let it know the programme is finished, and it finished well.

    The same logic applies to PHP, or any other programming language. Some functions’ return value is relevant, another function may not be required to return anything. In general functions return values because they are relevant to the flow of your programme.

    Take a class, for example:

    <?php
        class Foo
        {
            private $foo,$bar;
            public function __construct()
            {
                $this->foo = 'bar';
            }
            public function getFoo()
            {
                return $this->foo;//<-- getter for private variable
            }
            public function getBar()
            {
                return $this->foo;//<-- getter for private variable
            }
            public function setBar($val = null)
            {
                $this->bar = $val;
                return $this;//<-- return instance for fluent interfacing
            }
            public function setFoo($val = null)
            {
                $this->foo = $val;
                return $this;
            }
        }
        $f = new Foo();
        $f->setFoo('foo')->setBar('bar');//<-- fluent interface
        echo $f->getFoo();
    ?>
    

    If the setter function didn’t return anything, you’d have to write:

    $f->setFoo('foo');
    $f->setBar('Bar');
    

    So in this case, return values are relevant. Another example where they’re irrelevant:

    function manipulateArray(array &$arr)//<-- pass by reference
    {
        sort($arr);
    }
    $arr = range('Z','A');
    manipulateArray($arr);
    var_dump($arr);//array is sorted
    

    as opposed to:

    function manipulateArray(array $arr)//<-- pass by copy
    {
        sort($arr);
        return $arr;
    }
    $arr = range('Z','A');
    manipulateArray($arr);
    var_dump($arr);//array is not sorted!
    $arr = manipulateArray($arr);//<-- requires reassign
    

    Passing by reference is deemed risky in many cases, that’s why the latter approach is generally considered to be better. So in many cases the functions needn’t return a value, but they do all the same because it makes the code safer overall.
    That might be why you’re under the impression that functions must always return a value.

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

Sidebar

Related Questions

I have a program that I am writing, not too big. Apart from the
I started programming in C++. It was my first language, but I have not
i am not that big on programming and i need to sample an audio
This is not a big deal, but is there any way in F# to
Not sure if this is a big deal. But wondering why when the site
Does any know of a good calendar (not datepicker, but a BIG browsable calendar)
Hey everyone. I'm not big into UI programming so this may be an easy
This might not be a hard core programming question, but it's related to some
I see that Ruby is a big success when it comes to web programming.
I am programming an app that shows a lot of verses/poems so text wrapping

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.