Before we discuss the  behavior of functions/methods in dynamically and statically typed programming languages, we need to understand the meaning four terms related to typing of programming languages - Dynamically typed, statically typed, strongly typed and weakly typed programming languages.

Note that dynamically typed does not mean weakly typed, neither is statically typed the same as strongly typed!

Dynamically typed programming languages e.g. PHP, Python, and JavaScript

If a language variable type is checked at run time, then it is dynamically typed. It means you can declare a variable in one line, give it a value, then change it's value to another type in the next line.

Examples of languages, which fall in this category are PHP, Python, and JavaScript. From the list, you can tell that these languages are interpreted and not compiled.

For instance, see code snippets below:

//PHP
$a = "Hey";
$a = 3;
echo $a; // 3
//JavaScript
let a = "Hey";
a = 3;
console.log (a) //3
#Python
a = "Hey";
a = 3;
print (a); # 3

What we have done above cannot work in languages such as Java.

Statically typed programming languages - e.g. Java, C++, Scala and Typescript

If a programming language checks variable data types at compile time instead of at run time, then such a programming language is statically typed. It means that once you assign a data type to a variable, it cannot be changed. If you tried to change it and are using an intelligent IDE, then it would red-underline it to mean that it is an error.

Examples of languages, which are statically typed include Java, C, C++, Scala, TypeScript (a superset of JavaScript), etc...

Thus, you cannot try the following because it would be an error.

//Java
String a = "Hey";
a = 3;// error:=>Type missmatch: cannot convert from int to string.

Strongly typed programming languages - e.g. Java and Python

If a programming language requires that variables be in a specific data type when required to be so, then that language is a strongly typed.

For instance in Java,  an addition operation of integers expects that both variables are of integer type.

//Java
String a = "1234";
int b = 34;
int c = a + b; //error => Type mismatch: cannot convert from String to int

What you noticed is that a string containing digits does not magically change to an integer or the integer to a string as it may happen in JavaScript. If you want "1234" as an integer, then you will have to convert it explicitly as seen below.

//Explicit conversion
int c = Integer.valueOf(a) + b; 
This would still be an error if the string is not made of numbers. 

Lets try this in Python:

#Python
a = "1234";
b = 34;
c = a + b; 
print (c); //TypeError: can only concatenate str (not "int") to str

We get an error with Python as well - we would not expect Python to convert "1234" to an integer. Thus, Python too is a strongly typed programming language.  

Weakly typed programming languages - e.g. PHP and JavaScript

For weakly typed languages, variables have a type but are not bound to that specific data type i.e conversion can happen to fit a situation.

Let's try JavaScript and PHP with what we tried with Java and Python.

//JavaScript 
let a = "1234";
let b = 34;
let c = a + b; 
console.log (c);// 123434
//a concatenation happened, variable b was converted to a string
//PHP
$a = "1234";
$b = 34;
$c = $a + $b; 
echo $c; //1268

With PHP, a string "1234" was converted to an integer to suit the addition operation. This was possible because all the characters in the string were digits.

What would happen if $a = "12ab";

//PHP 
$a = "12ab";
$b = 34;
$c = $a + $b; 
echo $c; //46; 34 added to 12, with a Notice: A non well formed numeric value encountered

What would happen if $a = "abcd";

//PHP
$a = "abcd";
$b = 34;
$c = $a + $b; 
echo $c;//34 with a warning: A non-numeric value encountered in

We have learned that a programming language can be dynamically typed, but turns out to be strongly typed. See the summary below.

Programming language classification in terms of dynamic, static, strong and weak 

How do methods behave in dynamically and statically typed programming languages?

Suppose we want to solve the problem described below using a function:

Given an array and a number x, write a function, firstIndex, which returns the array index of  the first occurrence of x. If x is not found, return false.

From our discussion earlier, only dynamically typed programming languages will meet the demands of the above problem.  For this reason, statically typed languages can return -1, if the target element was not found at all.

Let's write the function signatures of the function for Java, JavaScript, PHP, and Typescript.

//Java
public int firstIndex (int [] arr, int x) {

}
//JavaScript 
function firstIndex (arr,x){

}
//PHP
function firstIndex ($arr, $x){

}
//TypeScript
function firstIndex(arr: : number[], x:number): number {

}

It can be observed that JavaScript and PHP do not specify the function return type, and therefore a value of any data type can be returned. On the other hand, TypeScript and Java must specify the return type.

Additionally, the parameters passed to the functions in the dynamically typed languages, could be of any type. This might be a good thing but it reduces programmer productivity when using intelligent IDEs. For instance, if you want to see the methods available on that received data structure (if it was an object), an IDE may not be able to show or do any auto-completion because it is unclear the type of data type/data structure to be received.

Let's complete the code in the functions.

//Java 
public int firstIndex (int [] arr, int x) {
    for (int i = 0; i < arr.length; i++) {
        if(x == arr[i]) {
        	return i;
        }
    }
    return -1;
}
//PHP
function firstIndex ($arr,$x){
    return array_search($x,$arr);
}
//We make use of PHP's inbuilt function array_search().
//JavaScript
function firstIndex (arr,x){
  if (arr.indexOf(x) == -1){
    return false;
  }
  return arr.indexOf(x);
}
//TypeScript
function firstIndex(arr:number[], x:number): number{
  return arr.indexOf(x);
}

I hope you enjoyed reading this article.

You've successfully subscribed to Decoded For Devs
Welcome back! You've successfully signed in.
Great! You've successfully signed up.
Your link has expired
Success! Your account is fully activated, you now have access to all content.