Swift: A function that does not return anything

This entry is part 12 of 41 in the series Swift

As you can see, we use func to define a function.  The earlier example shows how to define a function that returns a string.  If you do not need the function to return anything, this is how we define it.

import UIKit


func sayHello(personName: String) {
    let greeting = "Hello, " + personName + "!"
    print(greeting)
}

sayHello("John")

swift

C++: Function

This entry is part 16 of 61 in the series C++

So far, the code that we have worked on is all contained within a function called main.  Main is the starting point of a program and so in a way, we have already used a function in our code.

A function is a self-contained block of code with a specific purpose.

A function prototype declaration consists of function’s return type, name, and arguments list.

In the example below, we create a function called printout.  This function did nothing but print out a string of characters.

#include <iostream>
#include <string>
using namespace std;

void printout(string str)
{
	cout << str;
}

void main()
{
	string greeting = "Hi, there";

	printout(greeting);

	cout << endl;
}

The type of the function printout is a void which means it will not return anything.  The name is called printout and it takes in a string argument.

PHP: Returning an array in a function

This entry is part 34 of 54 in the series PHP Tutorial

Returning an array is actually quite simple with return array().

We just have to pass the 2 amounts into the array() function with

return array($total_notip, $total_tip);

The 2 array elements will then be accessible by $totals[0] and $totals[1].

<?php

function restaurant_check($meal, $tax, $tip)
{
    $tax_amount = $meal * ($tax / 100);
    $tip_amount = $meal * ($tip / 100);
    $total_notip = $meal + $tax_amount;
    $total_tip = $meal + $tax_amount + $tip_amount;

    return array($total_notip, $total_tip);
}

$totals = restaurant_check(30.50, 7, 15);

echo "The total without tip is $totals[0]";
echo "\n";
echo "The total with tip is $totals[1]";

?>