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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:23:16+00:00 2026-06-09T06:23:16+00:00

Why is the array shifted at the beginning of the subroutine? sub max {

  • 0

Why is the array shifted at the beginning of the subroutine?

sub max {
    my($max_so_far) = shift @_;
    foreach (@_) {
        if ($_ > $max_so_far) {
           $max_so_far = $_;
        }
    }
    $max_so_far;
}    

Is it just to give $max_so_far an initial value? The program runs exactly the same with

    my($max_so_far) = undef;

Is there a particular reason to shift the array to start with? (I ask because I spent about 10 min trying to figure out why that shift was essential to the subroutine.)

  • 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-09T06:23:18+00:00Added an answer on June 9, 2026 at 6:23 am

    It’s to initialise the return value for the function and something the rest of the function can compare against.

    Consider some scenarios and trace through the code. For example, say it is invoked like this:

    my $max = max(1,2,3);
    

    Inside max, the first line sets $max_so_far to 1 and @_ becomes (2,3). Now when we run through the foreach loop we have an initial value and avoid undef errors. It first compares $max_so_far to 2, updates it to 2, and so on.

    Another example is if max is invoked like this:

    my $max = max(1);
    

    Inside max, the first line sets $max_so_far to 1 and @_ becomes (). When we hit the foreach loop, it has nothing to iterate through and just returns the initial value $max_so_far.

    Another example is if max is invoked like this:

    my $max = max();
    

    Inside max, the first line sets $max_so_far to undef because @_ is empty. There’s nothing to iterate through in the foreach loop, so the function just returns undef.

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

Sidebar

Related Questions

I writing a MIPS program in mars. I am looping through an array of
I need to shift to the right and to the left an array by
array moves contains just a couple of move I wanna draw on canvas. basically
I have an UInt16[1000,1000] array of 10-bit intensity values which I want shifted from
array_pop() returns the last value of an array and removes that value from the
I know that shift, push, and pop are Array methods used to add/remove elements
I have got an array of chars that I'm trying to bitwise shift right
array(2) { [0]=> array(4) { [id]=> string(1) 1 [name]=> string(3) Lim [subproject_id]=> string(1) 5
Array ( [0] => Array ( [name] => A [id] => 1 [phone] =>
Array.Copy and Buffer.BlockCopy both do the same thing, but BlockCopy is aimed at fast

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.