Bifalgorithm post · Computer science one · Declaring Functions

How do you declare functions and specify their return values in PHP?

How do you declare functions and specify their return values in PHP? (Computer science one · Declaring Functions) — "In PHP, to declare a function you use the…

Ideas in this flowchart

  • Déclaration et utilisation des fonctions en PHP
  • Le mot-clé function introduit la déclaration d’une fonction.
  • La signature comprend un identifiant et des paramètres.
  • Le corps de la fonction est placé entre des accolades ouvrante et fermante.
  • La documentation est généralement placée avec la déclaration de la fonction.
  • Le typage dynamique de PHP influence les valeurs retournées.
  • Le type de retour n’est pas déclaré.
  • Une fonction peut retourner n’importe quel type et est donc souvent dite de type mixed.
  • Selon les entrées, une même fonction peut retourner un nombre ou une chaîne.
  • Les commentaires de documentation accompagnent les fonctions.
  • Les exemples utilisent le style de commentaires appelé doc comments.
  • Exemples de fonctions PHP
  • sum calcule la somme de deux arguments.
  • getDistance calcule la distance euclidienne entre deux points bidimensionnels.
  • getMonthlyPayment calcule le paiement mensuel d’un prêt.
  • Règles et conventions des identifiants de fonction
  • Un nom de fonction commence par une lettre et peut contenir des caractères alphanumériques et des underscores.
  • Les conventions modernes utilisent généralement le lower camel casing.
  • Les noms de fonctions PHP ne sont pas sensibles à la casse.
  • Les appels doivent néanmoins respecter une casse cohérente avec la déclaration.
  • Le mot-clé return contrôle la valeur renvoyée à la fonction appelante.
  • La valeur retournée détermine le type de retour effectif de la fonction.
  • La syntaxe return; permet de ne retourner aucune valeur.
Feed/

How do you declare functions and specify their return values in PHP?

No cover

Channel

Computer science one

1 page

"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"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.