Raspberry Pi_Eng_24.4.8 Function


Published Book on Amazon


All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


24.4.8  Function

 

24.4.8.1    Definition of Function

 

Function is a unit of statements that can be executed repeatedly within a program. Users can freely define and use a function with arbitrary contents under certain constraints. It is defined in the following format.

 

function  functionName(  argument1,  argument2,  argument3) {
    code to be executed;

    [  return;  ]

    code to be executed;
}

 

The function name must begin with a letter or underscore and is case insensitive.

 

The argument in function is optional. Multiple arguments have to be separated by comma (,). A program that calls a function can pass necessary data to the function using arguments, and the arguments are used as a variable inside the function.

 

Inside a function, you can use any valid PHP code, or you can use another function or class definition.You can use "return" statement inside a function to stop processing of the function, return to the program that called the function, and return the result of function processing in to the calling program by specifying it in "return" statement.

 

[Used Example]

 

<?php

function foo($arg_1, $arg_2, $arg_3)

{

    echo "예제 함수.n";

    return $retval;

}

?>


 

24.4.8.2    Using Function

 

A function is executed when the function is called from other program, not when it is loaded into the system. When calling a function in another program, you have to use the function name, and you can specify the required value for each argument. It is called in the following format.

 

[ $return = ]  functionName(  argument1-value,  argument2-value,  argument3-value )

 

A function must be defined before use. If a function definition is in another function or is inside a conditional statement, the function is defined so that the statement is executed.

 

If you execute "return" statement by specifying a particular value within a function, you can use this value for various purposes.

 

All functions and classes in PHP have global scope. Even if a function is defined inside another function, it can be called from the outside, and vice versa.

 

Inside PHP, there are over 1000 built-in functions provided by the system. They are free to use without any special definition. For more detailed information, Please refer to the followings:

    http://php.net/manual/en/funcref.php  

 

[Used Example]

Here is an example of a function.

 

<?php

function writeMsg() {

    echo "Hello world!";

 }

 

writeMsg(); // call the function

?>

 

Here is another example of a function.

 

<?php

function familyName($fname, $year) {

    echo "$fname Refsnes. Born in $year <br>";

}

 

familyName("Hege", "1975");

familyName("Stale", "1978");

familyName("Kai Jim", "1983");

?>

 

The following is an example of specifying a particular value while executing "return" statement within a function.

 

<?php
function square($num)
{
    return $num * $num;
}
echo square(4);   // outputs '16'.
?>

 

The following is a case where a function is defined inside another function. In this case, the inside function is not defined until the outside function is executed.

 

<?php


function foo()   {
  function bar()  {
    echo "bar() don't exist until foo() is called.
n";
  }
}

 

// poo() can be called normally.

poo();

// We can't call bar() yet since it is not defined  

// bar();  

 

// the execution of foo() defines bar() function and make it accessible. 
foo();

 

// As foo() is executed and bar() is defined, Now we can call bar(),
bar();

 

function poo() {

  echo "I exist immediately upon program start.\n";

}

?>