Concept
How do you declare functions and specify their return values in PHP?
ComputerScienceOne / Declaring Functions
"In PHP, to declare a function you use the keyword function . Because PHP is dynamically typed, a function can return any type. Therefore, you do not declare the return type (just as you do not declare a variable’s type). After the function keyword you provide an identifier and parameters as the function signature. Immediately following, you provide the function body enclosed with opening/closing curly brackets. Typically, the documentation for functions is included with its declaration. Consider the following examples. In these examples we use a commenting style known as “doc comments.” This style was originally developed for Java but has since been adopted by many other languages.\n\n/**\n\n* Computes the sum of the two arguments.\n\n*/\n\nfunction sum($a, $b) {\n\nreturn ($a + $b);\n\n}\n\n/**\n\n* Computes the Euclidean distance between the 2-D points,\n\n* (x1,y1) and (x2,y2).\n\n*/\n\nfunction getDistance($x1, $y1, $x2, $y2) {\n\n$xDiff = ($x1-$x2);\n\n$yDiff = ($y1-$y2);\n\nreturn sqrt( $xDiff * $xDiff + $yDiff * $yDiff);\n\n}\n\n/**\n\n* Computes a monthly payment for a loan with the given\n\n* principle at the given APR (annual percentage rate) which\n\n* is to be repaid over the given number of terms (usually\n\n* months).\n\n*/\n\nfunction getMonthlyPayment($principle, $apr, $terms) {\n\n$rate = ($apr / 12.0);\n\n$payment = ($principle * $rate) / (1-pow(1+$rate, -$terms));\n\nreturn $payment;\n\n}\n\nFunction identifiers (names) follow similar naming rules as variables, however they do not begin with a dollar sign. Function names must begin with an alphabetic character and may contain alphanumeric characters as well as underscores. However, using modern coding conventions we usually name functions using lower camel casing. Another quirk of PHP is that function names are case insensitive . Though we declared a function, getDistance() above, it could be invoked with either getdistance() , GETDISTANCE or any other combination of uppercase/lowercase letters. However, good code will use consistent naming and your function calls should match their declaration. The keyword return is used to specify the value that is returned to the calling function. Whatever value you end up returning is the return type of the function. Since you do not specify variable or return types, functions are usually referred to as returning a “mixed” type. You could design a function that, given one set of inputs, returns a number while another set of inputs returns a string. You can use the syntax return; to return no"
Related Ideas
- What happens when a PHP function does not return a value?ComputerScienceOne · Calling Functions
- How do you call a function in PHP?ComputerScienceOne · Calling Functions
- What are the key features of functions in PHP?ComputerScienceOne · Declaring Functions
- How do default parameter values work in PHP functions?ComputerScienceOne · Optional & Default Parameters
- How does PHP pass arguments by reference?ComputerScienceOne · Passing By Reference
- How do functions work in programming languages?ComputerScienceOne · Functions
- How are strings, statements, and code blocks written in PHP?ComputerScienceOne · Basic Syntax Rules
- How do C functions return values to the calling function?ComputerScienceOne · Void Functions