Categories
scala

Expressions and functions in scala

In this post , let us learn about the expressions and functions in scala

Expressions

Expressions are the one which gets evaluated  in scala . We have different operators to achieve this .Will look at those below .

Mathematical operators:
+ -> Addition
– -> Subtraction
* -> Multiplication
/ -> Division
& -> bitwise AND
| -> bitwise OR
^ -> bitwise exclusive OR
<< -> bitwise left shift
>> -> bitwise right shift
>>> -> right shift with zero extension only in scala

Relational operators:
== -> Equal to
!= -> not equal to
> -> greater than
>= -> greater than or equal to
< -> lesser than
<= -> lesser than or equal to

Boolean operators :
! -> negation(unary operator)
&& -> logical AND(binary operator)
|| -> logical OR(binary operator)

other operators:

+= , -= , *= , /=

Functions

Following is the function used for adding two values .

def – keyword

a and b – parameters

Int – data type (First letter must in capital)

1. Normal function :
def func (a: Int, b: Int ): Int =
  {
    a + b
  }
  println(func(20,10))
30

Its not mandatory to provide return type in the case of normal function . Below is the way I tried without any return type .

def func (a: Int, b: Int ) =
  {
    a + b
  }
  println(func(20,10))
30
2. Function without parameter

Function can also be without any parameter as like below .

// invoking with parenthesis   
def func1 () : Int =42
   println(func1())
42

Other way to invoke parameterless function in scala is the below option.

// invoking without parenthesis
def func1 () : Int =42
   println(func1)
42
3.Recursive function

A function gets called continuously by itself .

def func1(a: String ,b: Int) : String = {
    if (b==1)  a
    else a + (func1("Success",b-1))
  }
  println(func1("Success", 5))
SuccessSuccessSuccessSuccessSuccess

We must specify the return type for this .

def func1(a: String ,b: Int) = {
    if (b==1)  a
    else a + (func1("Success",b-1))
  }
  println(func1("Success", 5))
identifier expected but '=' found.
    def func1(a: String ,b: Int) :  = {
Categories
python

Returning multiple values in python

In this post, let us learn about returning multiple values in python.

Will it possible in python?

Yes, returning multiple values in python is possible. There are several ways to do it.

1 . Return as Tuple :

The tuple stores the values of various datatypes. But we cannot modify those values in any case.

Please refer to the below link to understand it better.

https://beginnersbug.com/difference-between-list-and-tuple/

The tuple is created with or without a round bracket as below.

# Sample program to return as Tuple without bracket
def first():
 a="string"
 b=10
 return a,b
a,b=first()
print(a,b)

The above python function returns more than one value.

string 10

Tuple returns within a bracket as follows.

# Sample program to return as Tuple with bracket
def first():
 a="string"
 b=10
 return (a,b)
a,b=first()
print(a,b)
string 10
2 . Return as List :

The list is also a collection created with square bracket.

The list helps us in returning multiple values in python .

It is mutable in nature.

# Sample program to return as List
def second():
 a="apple"
 b=50
 return [a,b]
second_list=second()
print(second_list)

We return multiple values from the python function using the list.

['apple', 50]

We can identify the datatype of the return by using the below command