Can I assign value within a parameter? [closed]

1

I have a work in pseudocode, where I needed to create a function to receive a value from the user and make the mod by 2, so I did this:

Função Módulo (x,b=2: inteiro) : inteiro

Var
   i:inteiro
Inicio
 Escreva("Digite um número:")
 Leia (x)
 i=x mod b
 Escreva("O módulo de "+x+" é "+i)
Fim_função"

Can I add value 2 to variable b within the parameter?

    
asked by anonymous 01.06.2017 / 19:19

1 answer

0

It depends on the language, as it is pseudo-code, it will depend on whether your teacher will accept it or not.

This is called a "predefined parameter" (or anything that has the same meaning).

In general, the feature works by assigning the value set in the method signature if there is no argument corresponding to that parameter.

The fact is that many languages accept this and some (like JavaScript) have adapted for this to work.

A very simple example in JS.

function modulo(x, mod = 2) {
  return x % mod;
}

console.log(modulo(5));    // Aqui, vai fazer mod 2, pq não tem segundo argumento
console.log(modulo(8, 3)); // Aqui, vai fazer mod 3
    
01.06.2017 / 19:29