Addition of Two Numbers Using Function in PHP

Addition of Two Numbers Using Function in PHP. Using this program we can print the addition of two numbers using function. A function is a piece of code that takes another input in the form of a parameter, processes it, and then returns a value. Although these are built-in functions, PHP also allows you to construct your own.

<?php
function num(int $a, int $b)
{
return $a + $b;
}
echo num(1, "1");
return num
?>

 

Output:

2

Return Statement in PHP

Returns control of the program to the calling module. Following the invocation of the called module, execution restarts at the expression. When called from within a function, the return statement terminates the current function’s execution and returns its argument as the function call’s value. 

The intended data types of the result that a function or a class method should return are specified in the return type declaration. PHP, for example, can convert the integer result into a string before returning the function result because of the return type declaration. Using the return statement with a value or object, a function can return a value. 

Return signals the conclusion of the function’s execution and returns the value to the calling code. No, a function cannot have two returns; the first return will terminate the function, requiring you to build an object.

Function in PHP

In PHP, a function is a reusable piece of code that performs a certain operation. When called, functions can either return values or just conduct an action without returning any value. PHP includes around 700 built-in functions that accomplish various tasks. PHP offers us with two kinds of functions: Built-in functions: PHP comes with a plethora of built-in library functions. User Defined Functions: In addition to the built-in functions, PHP allows us to write our own customised functions, known as user-defined functions.