Kara-Moon Forum
March 28, 2024, 03:20:55 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: You can go back to the main site here: Kara-Moon site
 
   Home   Help Search Login Register  
Pages: 1 [2]
  Print  
Author Topic: Omitted parameters in a subroutine call  (Read 5091 times)
bvdp
Kara-Moon Master
****
Posts: 1436


WWW
« Reply #15 on: December 12, 2020, 06:22:51 PM »

BTW, do any modern languages support this concept? I know that Python certainly doesn't (unless you use default values).
Logged

My online life: http://www.mellowood.ca
sciurius
Sr. Member
****
Posts: 443



« Reply #16 on: December 12, 2020, 07:48:11 PM »

In Perl, arguments are passed in an array. If you pass two arguments, the array will have a length of 2. If you access anything beyond it will yield an undefined value (Perl has a real OOB value 'undef'). So if you define a subroutine that expects three parameters, and you call it two, the third will be undef.

Code:
sub foo {
    my ( $a, $b, $c ) = @_;
    unless ( defined($c) ) {
print("it was undefined\n");
    }
}

foo(1,2);

Javascript:
Code:
function foo(a,b,c) {
    if ( c === undefined )
console.log("it was undefined")
}

foo(1,2)

This prints "it was undefined".
Logged
Pages: 1 [2]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.047 seconds with 19 queries.