2018年11月29日 星期四

Function

# php build a function
#!/usr/bin/php
<?php

 function max_num($a,$b) {
  if ($a>$b) {
     $c=$a;
     }
  else {
     $c=$b;
  }
  return $c;
 }

  echo max_num(3,2)."\n";
?>


#Python Build a function
>>> def helloworld():
...  print("HelloWorld!")
...  return
...
>>> helloworld()
HelloWorld!

Passing arguments
>>> def helloworld(str):
...  print str
>>> helloworld("HelloWorld!")
HelloWorld!
>>> def  addprice(plist):
...  return (sum(plist))
...
>>> plist=[1,2]
>>> addprice(plist)
3
>>> addprice([1,3])
4

Build a class
>>> class Shopping:               
...  def __init__(self,item,price):
...   self.item=item               
...   self.price=price             
...
>>> instance=Shopping("Bread",0.85)
>>> instance.item
'Bread'
>>> instance.price
0.85

#shell script build a function
#!/bin/bash

 max_num() {
  if [ $1 -gt $2 ]
  then
  c=$1
  else
  c=$2
  fi
  return $(($c))

}

max_num 10 20
echo $c