CSC300 / CSC402: Function declarations [7/11] Previous pageContentsNext page

PythonJava
01
02
03
04
05
06
07
08
09
def addHello(x):
    return "Hello " + x

def main():
    print(addHello("Bob"))
    print(addHello("Alice"))

if __name__ == "__main__":
    main()
01
02
03
04
05
06
07
08
09
10
11
package algs11;
import stdlib.*;
public class Hello {
  public static String addHello (String x) {
    return "Hello " + x;
  }
  public static void main (String[] args) {
    StdOut.println (addHello ("Bob"));
    StdOut.println (addHello ("Alice"));
  }
}

Java requires declaration of return type and parameter types.

Because of types, java compiler can catch more errors, as below. What's wrong?

PythonJava
01
02
03
04
05
06
07
08
09
def addHello(x):
    "Hello " + x

def main():
    print(addHello("Bob"))
    print(addHello("Alice"))

if __name__ == "__main__":
    main()
01
02
03
04
05
06
07
08
09
10
11
package algs11;
import stdlib.*;
public class Hello {
  public static String addHello (String x) {
    "Hello " + x;
  }
  public static void main (String[] args) {
    StdOut.println (addHello ("Bob"));
    StdOut.println (addHello ("Alice"));
  }
}

Previous pageContentsNext page