Functions






Costco Hot dogs.



Functions may be:

Examples of functions created for us






Functions we might create




C

Convert Temperature.


#include <stdio.h>

// define the C to F function
float c2f(float c) {
    float f = (c * 9/5) + 32;
    return f;
}

// define the display function
void displ(float c) {
    printf("%3.2f degrees C  is %3.2f degrees F\n", c, c2f(c));
}

// Display C to F conversions
int main() {
    displ(20);
    displ(30);
    displ(0);
    displ(-40);
}


  • Running the above gives:
20.00 degrees C  is 68.00 degrees F
30.00 degrees C  is 86.00 degrees F
0.00 degrees C  is 32.00 degrees F
-40.00 degrees C  is -40.00 degrees F


Convert Mileage


#include <stdio.h>

// define the to MPG  function
float mpg(float lp) {
    // convert l to imp. gal. (.22)  km to miles (1.6)
    float m = 1 / (lp/100 * .22 * 1.6);
    return m;
}

int main() {
    printf("mpg for 4 l/100km is %3.2f mpg\n", mpg(4));
}


mpg for 4 l/100km is 71.02 mpg

PHP 

Convert Temperature.

#!/usr/bin/env php
<?php

// Define the C to F function
function c2f($c) {
    return ($c * 9/5) + 32;
}

// Define the display function
function displ($c) {
    echo ("$c degrees C is " . c2f($c) . " degrees F\n");
}

displ(20);
displ(30);
displ(0);
displ(-40);

?>

  • Running the above gives:
20 degrees C is 68 degrees F
30 degrees C is 86 degrees F
0 degrees C is 32 degrees F
-40 degrees C is -40 degrees F

Convert Mileage.


#!/usr/bin/env php
<?php

// define the to MPG  function
function  mpg($lp) {
    // convert l to imp. gal. (.22)  km to miles (1.6)
    $m = 1 / ($lp/100 * .22 * 1.6);
    return $m;
}

echo ("mpg for 4 l/100km is " .  round(mpg(4),2));
?>


  • Running the above gives:
mpg for 4 l/100km is 71.02


Convert litres per 100 km to mpg


lp/100km

Mileage for 14 l/100km is 20.29 mpg.

Python

#!/usr/bin/env python3

def c2f(c):
    return (c * 9/5) + 32

def displ(c):
    print(str(c) + " degrees C is " + str(c2f(c)) + " degrees F");

displ(20)
displ(30)
displ(0)
displ(-40)


  • Running the above gives:
20 degrees C is 68.0 degrees F
30 degrees C is 86.0 degrees F
0 degrees C is 32.0 degrees F
-40 degrees C is -40.0 degrees F

Convert Mileage.


#!/usr/bin/env python

# define the to MPG  function
def mpg(lp):
    # convert l to imp. gal. (.22)  km to miles (1.6)
    m = 1 / (lp/100.0 * .22 * 1.6)
    return m

print ("mpg for 4 l/100km is " +  str(round(mpg(4),2)))

  • Running the above gives:
mpg for 4 l/100km is 71.02


Bash


#!/usr/bin/env bash

c2f () {
    f=$(( $1 * 90 / 5 / 10 + 32 ))
    echo "$1 degrees C is $f degrees F";
}

echo $(c2f 20)
echo $(c2f 30)
echo $(c2f 0)
echo $(c2f -40)



  • Running the above gives:
20 degrees C is 68 degrees F
30 degrees C is 86 degrees F
0 degrees C is 32 degrees F
-40 degrees C is -40 degrees F



Convert Mileage.

#!/usr/bin/env bash
# We use bc because bash does not do floating point arithmetic.
mpg () {                                           
    # Pipe the mpg calculation to bc and save the result in m
    # If bc is invoked with the -l option, a math library is preloaded and the
    #     default scale is set to 20.
    m=`echo "1 / ($1 / 100.0 * .22 * 1.6)" | bc -l`;
    echo $m                      
}                                                                                                  
                                 
printf "mpg is %.2f \n"  $(mpg 4);


  • Running the above gives:
 mpg is 71.02



Homework

  1. Choose your favorite programming language (of the 4 presented here)  and modify the mpg program so that it takes input in l/100km and outputs mp(imperial)g.
  2. Write your own function that does something that you find you do frequently (don't think washing dishes  or mowing the lawn will work without additional hardware).