What is a module that returns a value back to the part of the program that called it?

(PHP 4, PHP 5, PHP 7, PHP 8)

return returns program control to the calling module. Execution resumes at the expression following the called module's invocation.

If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return also ends the execution of an eval() statement or script file.

If called from the global scope, then execution of the current script file is ended. If the current script file was included or required, then control is passed back to the calling file. Furthermore, if the current script file was included, then the value given to return will be returned as the value of the include call. If return is called from within the main script file, then script execution ends. If the current script file was named by the auto_prepend_file or auto_append_file configuration options in php.ini, then that script file's execution is ended.

For more information, see Returning values.

Note: Note that since return is a language construct and not a function, the parentheses surrounding its argument are not required and their use is discouraged.

Note: If no parameter is supplied, then the parentheses must be omitted and null will be returned. Calling return with parentheses but with no arguments will result in a parse error.

As of PHP 7.1.0, return statements without an argument in functions which declare a return type trigger E_COMPILE_ERROR, unless the return type is void, in which case return statements with an argument trigger that error.

warhog at warhog dot net

16 years ago

for those of you who think that using return in a script is the same as using exit note that: using return just exits the execution of the current script, exit the whole execution.

look at that example:

a.php
<?php
include("b.php");
echo
"a";
?>

b.php
<?php
echo "b";
return;
?>

(executing a.php:) will echo "ba".

whereas (b.php modified):

a.php
<?php
include("b.php");
echo
"a";
?>

b.php
<?php
echo "b";
exit;
?>

(executing a.php:) will echo "b".

J.D. Grimes

9 years ago

Note that because PHP processes the file before running it, any functions defined in an included file will still be available, even if the file is not executed.

Example:

a.php
<?php
include 'b.php'; foo();
?>

b.php
<?php
return;

function

foo() {
     echo
'foo';
}
?>

Executing a.php will output "foo".

Table of Contents

1 Subroutines - Procedures and Functions

What is a module that returns a value back to the part of the program that called it?

Learn It: What are subroutines?

Subroutines - In computer programming, a subroutine is a sequence of program instructions
that performs a specific task, packaged as a unit. In different programming languages, a
subroutine may be called a procedure, a function, a routine, a method, or a subprogram.
The generic term 'callable unit' is sometimes used. (Source Wikipedia.org)
Parameters - Parameters or arguments are pieces of data that are passed into a subroutine
in order for that subroutine to correctly function.
In this example: def displayTotal(total): 'total' is a parameter that will be passed to
the 'displayTotal' subroutine. Multiple parameters can be managed using commas,
for example: def displayTotals(total1, total2, total3, etc)
  • Subroutines are sequences of instructions that perform a specific task. It may return one or more values, but does not have to.
  • It may be easier to think of them as mini-programs within a large program.
  • Subroutines consist of modules of code that perform different tasks.
  • If these tasks are repeated throughout the program, they can be written as subroutines.
  • Each subroutine is given a unique name so that it can be called and executed quickly throughout the program, without having to write the code again.
  • This reduces the size of the code, making the program more logical and easier to read and maintain.
  • Let's look at an example of using subroutines:

What is a module that returns a value back to the part of the program that called it?
Step 1

  • Here the main program (shown in the flowchart on the left, beginning with the word “Start”) asks the user to input two numbers.
  • It then asks them to select an option from a menu.
  • If they enter the word “add”, it will run a subroutine called addNum (The smaller flowchart on the top right), if they enter anything else it will run the subtractNum subroutine (bottom right).
  • Let's look at the subroutine box in the main flowchart first:

What is a module that returns a value back to the part of the program that called it?
Step 2

  • We can see the name of the subroutine has been defined along with the variables; num1 and num2.
  • This means that the addNum subroutine is going to use the values of num1 and num2 and so has been passed these values.
  • Sending variables to a subroutine is known as =passing the variables.

Step 3

  • We can now look at the addNum subroutine flowchart.

What is a module that returns a value back to the part of the program that called it?

  • This subroutine will add together the num1 and num2 variables and store these in a new variable called answer.
  • It will then return the variable answer back to the main menu.
  • It will then go back to the main menu, once the addNum subroutine has been completed, where it will be able to display the answer variable.
  • If we had not returned the answer variable back to the main menu, it would not be able to display the answer in the output as the main program on its own has not defined the answer variable and it does not know what it is.

Learn It: Advantages of using Subroutines in programs

Advantages of using Subroutines

  • Breaking down or decomposing a complex programming task into smaller sub-tasks and writing each of these as subroutines, makes the problem easier to solve.
  • Subroutines can be used several times within a program.
  • It saves the programmer time as it reduces the amount of code that needs to be written or amended by allowing you to reuse code without having to write it again.
  • If you are working as part of a team you can divide a large program into smaller sections and allow individuals to simultaneously work on those sections.
  • It makes the code easier to read if you use sensible subroutine labels as the headings tell the reader what that section of code is doing.
  • By reducing the amount of repeating tasks you also reduce the risk of introducing errors in a program.
  • Easy to maintain as each subroutine can be tested separately.

Learn It: Passing Data within Programs

A parameter is a variable that is “passed” to a subroutine.

Using Parameters to pass data within Programs

  • A parameter is a variable used in a subroutine to refer to data that is inputted into the subroutine.
  • If a subroutine requires a value that has been used in another part of the program, then this variable has to be “passed” to the subroutine.
  • You can have more than one parameter passing into a subroutine at a time.

What is a module that returns a value back to the part of the program that called it?

  • This example demonstrates how to use more than one parameter to be passed into our subroutine.
  • In this case num1 and num2 are variables inputted by the user in the main program.
  • Once the variables have been inputted, the addNum subroutine is called and the two parameters (num1 and num2) are passed to that subroutine so they can be used.

Learn It: Returning values from a Subroutine

A parameter is a variable that is “passed” to a subroutine.

Returning Single Values

  • We can alter the previous code example and instead of outputting the total as part of the subroutine, we can use the variable in the main program or even in another subroutine to pass that value back into the main (calling) program as shown below:

What is a module that returns a value back to the part of the program that called it?
Returning Multiple Values

  • We can return more than one value back to the main (calling) program, however you need to create something called a tuple.
  • A tuple is a short list that holds values temporarily.
  • The following pseudocode example, returns the numbers and the total back to the main program.

What is a module that returns a value back to the part of the program that called it?

  • Here num1, num2 and total have been combined in the subroutine into a tuple called returningValue and this is retuned to the program using the line num1, num2, total ← addition().
  • This will only work as long as the data is presented in the same order as it appears in the tuple.
  • The values that are being returned are grouped in a tuple and then used in the main program, once they have been returned in the same order as the tuple.

Badge It: Coding Challenge

Silver - Return values from a subroutine

  1. Using the Trinket below and the pseudocode above, create a a working Python program. (4 Marks)

Upload to Fundamentals of 3.2 Programming - 3.2.7 Subroutines (Procedures and Functions): Silver on BourneToLearn

Learn It: Local and Global Variables

Global Variables - A variable that is declared in the main program.
Local Variable - A variable that is declared and only used in one subroutine.

Using Local and Global Variables within Programs

  • Subroutines may declare their own variables, called local variables, and that local variables only exist while in the subroutine executing and are only accessible within the subroutine.
  • Global variables are declared outside any function and they can be accessed on any function in the program.
  • Local variables are declared inside a function and can only be used inside that function.
  • It is therefore possible to have local variables with the same name in different functions.

Why use local variables?

What is a module that returns a value back to the part of the program that called it?

  • The example shown above, num1 and num2 are global variables, as they can be used anywhere in the program or subroutines as long as they have been passed to the subroutines as parameters.
  • However, userNum is only used within the changeNumbers subroutines, so is a local variable as it is not used anywhere else.
  • As soon as the changeNumbers subroutine has finished running the data stored in userNum will be deleted.

Advantages of using Local Variables

  • Using local variables in a subroutine is good practice because it keeps the subroutine self-contained.
  • This subroutine can then be used in any program and there is little chance of confusion over which variable names in the main program might conflict with similar names used in the subroutine.
  • Another advantage is that the program would be easier to debug and maintain.
  • Local variables save memory as the space used by local variables is freed up when the subroutines have finished.

Learn It: Functions and Procedures

Functions and Procedures

  • There are two different types of subroutines that we mainly use:
    • Functions.
    • Procedures
  • Functions return values back to the main program and procedures do not return a value back to the main program.
  • From the previous example program, addNum and subtractNum are both functions and not procedures as they both return values back to the main menu.

Try It: Functions

  • Look at the following trinket windows, which demonstrate the use of functions to return values that are then used in the main program.
  • In Python code the subroutines are defined BEFORE the main program.
  • Follow through the main program and following the subroutine this is known as “calling” a subroutine.
  • The following code demonstrates another example of how a function is created and called in the main program:

What is a module that returns a value back to the part of the program that called it?

Try It: Procedures

  • Look at the following trinket windows, which demonstrate the use of procedures being passed parameters (values) that are then used in the main program.
  • In Python the subroutines are defined BEFORE the main program.

Learn It: Structured Programming

Structured Programming - Structured programming (sometimes known as modular programming)
is a subset of procedural programming that enforces a logical structure on the program
being written to make it more efficient and easier to understand and modify.
Structured programming frequently employs a top-down design model, in which developers map
out the overall program structure into separate subsections. A defined function or set of
similar functions is coded in a separate module or submodule, which means that code can be
loaded into memory more efficiently and that modules can be reused in other programs.

What is a module that returns a value back to the part of the program that called it?

  • A structured programming approach is one which has the following characteristics:
    • It uses a modularised approach. This means that it uses subroutines to break down the program into manageable chunks of code.
    • Each subroutine should have a clear, well documented interface (parameters and return value).
    • It just uses the constructs of; sequence, selection and iteration.
    • Note, that the term subroutine interface refers to the number, type and order of the parameters that appear in the subroutine header, and the type of the return value.

Advantages of Structured Programming:

  • The structured approach has the advantages of using subroutines listed above.
  • By following a four-step approach, you will have planned your solution and there should be no surprises when you start to develop the solution.
  • You know the variables you are using and you know about the processes that each subroutine needs to perform and how they link together.
  • It also uses just three or four programming structures making the program quite easy to:
    • Understand.
    • Debug.
    • Maintain.

Badge It: Exam Questions

Gold - Answer the following questions:

  1. What is a subroutine? (1 Mark)
  2. What is the difference between a procedure and a function? (2 Marks)
  3. When writing pseudocode or Python code, where should the subroutines be put, above or below the main program? (2 Marks)

Upload to Fundamentals of 3.2 Programming - 3.2.7 Subroutines (Procedures and Functions): Gold on BourneToLearn

Badge It: Exam Question and Coding Challenge

Platinum - Exam question/Code Challenge

  1. The function roll(n) simulates the outcome of one random roll of an n-sided dice. E.g. roll(6) will randomly return either 1,2,3,4,5 or 6.
    • a) Noel has declared a local variable inside the function. Explain two differences between local and global variables? (4 Marks)
    • b) Noel wants to use his function in a dice game where two identical dice are rolled at the same time.
      • i) The player can choose the number of sides that the dice have.
      • ii) The player's score is the number of rolls it takes until both dice have landed on the same number.
      • iii) Write a sub-program that takes the number of sides of the dice as a parameter and returns a player's score. (5 Marks)

Upload to Fundamentals of 3.2 Programming - 3.2.7 Subroutines (Procedures and Functions): Platinum on BourneToLearn

What is the datatype of the value returned by the random library function?

The random. random() method returns a random float number between 0.0 to 1.0. The function doesn't need any arguments.

What function returns a string that is within another string?

This example uses the InStr function to return the position of the first occurrence of one string within another.

What is the purpose of the return statement in a function quizlet?

What is the purpose of the Return statement in a function? The Return statement specifies the value that the function returns to the part of the program that called the function. When the Return statement is executed, it causes the function to terminate and return the specified value.

What term is used in the ending terminal symbol of a function flowchart return End function End function return none of these?

The end terminal for function/subroutine must use “Return” instead of “Stop”. Process: Used whenever data is being manipulated. One flow line enters and one flow line exits. Input/Output: Used whenever data is entered (input) or displayed (output).