0

I am finding different functions in which the arguments seem to work or behave differently

I've tried a basic function with two parameters that are defined as variables inside the function, but the function i want to understand does not define the argument inside the function

For example:

 function myFunction($name, $age)
  {
    $name = ('maj');
    $age = ('31');
  }

Should, in theory(when I call the function), print to screen:

maj 31

But what about this example?

 function createTable($name, $query)
  {
    queryMysql("CREATE TABLE IF NOT EXISTS $name($query)");
    echo "Table '$name' created or already exists.<br>";
  }

 function queryMysql($query)
  {
    global $connection;
    $result = $connection->query($query);
    if (!$result) die($connection->error);
    return $result;
  }

So, here's where I am confused. Where in these functions are the arguments $name and $query defined?

  • Those functions already defined, you need to call these in the place where you want to execute to see the output ex: createTable('name', 'query') – Casper Sep 01 '19 at 01:09

2 Answers2

1

Parameters are primarily (not always) for passing things INTO the function

So if you want your function to print those values you give to it, it would look like this.

function myFunction($name, $age) {
    echo $name;
    echo ' ';
    echo $age;
    echo PHP_EOL;
}

// call the function and pass the paramters into it

myFunction('maj', 31);
myFunction('John', 32);

RESULT:

maj 31
John 32

You dont define those 2 variables inside the function, they are defines and exist insode the function only by virtual of being added to the parameter list.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Thanks for all the comments and answers. From what I think you are saying, arguments are passed through to the function and then parameters are given later when the function is called to give a value to the arguments. Is that correct? – Majdi Kanaan Sep 01 '19 at 01:31
  • @MajdiKanaan - I think you are confusing the concept, parameters and arguments are nearly the same thing. The only difference is the context in which you are using/defining them. Maybe this [question](https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter) will help you understand. Basically, in `function myFunction($name, $age) {}`, `$name` & `$age` are the *parameters*. `myFunction('maj', 31);` "maj" and "31" are the *arguments*. – EternalHour Sep 01 '19 at 01:45
  • OK, that helps a bit, however, how does that apply to the $name and $query parameters above(in my initial question)? I don't understand how they are summoned by or to the function. – Majdi Kanaan Sep 01 '19 at 01:46
  • They are defined when you call the function. RiggsFolly's answer explains it. – EternalHour Sep 01 '19 at 01:48
1

A function is a block of statements that can be used repeatedly in a program. You have already defined the function now you need to call them where you want to execute. Information can be passed to function through argument which is comma delimited list of expressions. Arguments are evaluated from left to right. Following function has two arguments $name, $query Therefore when createTable function call you also need to pass those two arguments and those arguments are used in inside the function.

//define function
function createTable($name, $query) {
    queryMysql("CREATE TABLE IF NOT EXISTS $name($query)");
    echo "Table '$name' created or already exists.<br>";
}

//call the function
createTable('user', 'id INT NOT NULL AUTO_INCREMENT,
        PRIMARY KEY(id),
        NAME VARCHAR(30) NOT NULL');

For more info, you can refer PHP documentation.

Casper
  • 1,469
  • 10
  • 19