PHP Parameterized Functions In Hindi

📔 : PHP 🔗

पिछले topic में पढ़ा कि PHP में Functions कैसे define करते हैं , और कैसे call करते हैं , but वो simple functions थे जो सिर्फ static result generate / return करते थे , इस topic में हम Parameterized Function के बारे में पड़ेंगे जिससे हम dynamic value return करा सकें।

Parameterized Function वो function होते हैं जो  कि parameter accept करते हैं , इन functions को define करते समय हम  parameter भी define करते हैं जो कि ensure करते हैं कि call  करते  समय हम कितने argument pass करने वाले हैं।

function में हम अपनी need के according कितने ही parameter pass कर सकते हैं। और यही parameter उस function के लिए as a variable work करते हैं।

PHP Parameterized Function Syntax

function function_name(param1 , param2, param3...etc)
{
  // your logic
  return value;
}

? Technically देखा जाए तो किसी function के लिए Parameter और Argument दोनों अलग अलग हैं। Parameters function definition के समय define किये गए variables होते हैं , जबकि function call करते समय pass की गयी values / variables Arguments होते हैं।

PHP Parameterized Function Example

File : param_fun.php

CopyFullscreenClose Fullscreen
<?php
  /*function that returns sum of two numbers */
  function add($num1, $num2)
  {
    return $num1+$num2;
  }
  
  echo 'Sum of 234 and 123 is :'. add(234, 123).'<br>';
  echo 'Sum of 546 and 23 is :'. add(546, 23);

?>
Output
Sum of 234 and 123 is :357
Sum of 546 and 23 is :569

Example में आप देख सकते हैं कि PHP में किस तरह से parameterized functions use करते हैं।

इसके साथ साथ Function define करते समय हम parameters का Type भी define कर सकते हैं , जिससे जब हम function call करेंगे तो हमें उसी types की value function में pass करनी पड़ेगी। और अगर किसी दूसरे type की value pass हो रही है तो PHP Fatal Error Generate कर देगी।

See Example

File : param_fun2.php

Copy Fullscreen Close Fullscreen
<?php
  /*define function with the parameter type */
  function add(int $num1, int $num2)
  {
    return $num1+$num2;
  }
 
  echo ' Sum : ' .add(56, 123);
?>
Output
Sum : 179

PHP Function With Optional Parameter

PHP में आप function को optional parameter के साथ भी define कर सकते हैं , इसके लिए function declaration के समय null assign कर दिया जाता है , फिर function समय value देते हैं तब भी function run होगा और value pass न करें तो भी Run होगा।

See Example

File : param_fun3.php

Copy Fullscreen Close Fullscreen
<?php
  /*define function with optional  parameter  */
  function print_name($first_name, $last_name=null)
  {
    echo $first_name.' '.$last_name;
  }
  
  /*call function with both values */
  print_name('Rahul', 'Rajput').'<br>';
  /*now call function with only first value */
  print_name('Tom');
?>
Output
Rahul Rajput
Tom

PHP Function With Default Parameter

इसके अलावा PHP हमें Function में default parameter भी set करने की facility provide करती है , कई जगह हमें ऐसी जरूरत पड़ती है जिसमे हमें कुछ parameters default रखने होते हैं , अगर उस parameter की value pass की जाती है तब passed value use होगी otherwise default value use होगी।

File : param_fun4.php

Copy Fullscreen Close Fullscreen
<?php
  /* define function with default parameter*/
  function print_table(int $number=2)
  {
    for($n=1; $n <= 10; $n++)
    {
      echo $n*$number.' , ';
      
    }
  }
  
  /* first call function withowt argument */
  print_table();
  echo'<br>';
  /* now pass any number so that we can diferenciate*/
  print_table(5);
?>
Output
2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 , 18 , 20 ,
5 , 10 , 15 , 20 , 25 , 30 , 35 , 40 , 45 , 50 ,

अब आपको parametrized function , function with default parameter and function with optional parameter अच्छे से समझ आ गया होगा।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers