Skip to main content

Introduction

Parameters allow you to pass data into functions, making them more flexible and reusable. Instead of working with fixed values, functions can process different inputs.

Basic Parameter Syntax

Define parameters inside the parentheses with their type and name:
int operacion(int valorUno, int valorDos) {
  int suma = valorUno + valorDos;
  return suma;
}

Key Components

  • Parameter type: int - specifies what type of data the parameter accepts
  • Parameter name: valorUno, valorDos - names used to reference the values inside the function
  • Multiple parameters: Separated by commas

Passing Arguments

When calling a function, provide values (arguments) for each parameter:
int resultado = operacion(21, 35);
print('El Resultado es: $resultado');
// Output: El Resultado es: 56
Arguments are the actual values you pass when calling a function, while parameters are the variables that receive those values in the function definition.

Different Ways to Use Parameters

Store Result in Variable

int resultado = operacion(21, 35);
print('\nEl Resultado es: $resultado');
// Output: El Resultado es: 56

Direct String Interpolation

print('\nEl Resultado es: ${operacion(7, 3)}');
// Output: El Resultado es: 10

Direct Print

print(operacion(20, 2));
// Output: 22

Complete Example

int operacion(int valorUno, int valorDos){
  int suma = valorUno + valorDos;
  return suma;
}

void main() {
  int resultado = operacion(21, 35);
  print('\nEl Resultado es: $resultado');

  //Otra manera de usar es:
  print('\nEl Resultado es: ${operacion(7, 3)}');

  //De otra forma más:
  print(operacion(20, 2));
}

Parameter Order

Positional parameters must be provided in the exact order they are defined. The first argument matches the first parameter, the second argument matches the second parameter, and so on.
int operacion(int valorUno, int valorDos) {
  return valorUno - valorDos;
}

void main() {
  print(operacion(10, 5));  // 10 - 5 = 5
  print(operacion(5, 10));  // 5 - 10 = -5
}

Multiple Parameter Types

Functions can accept parameters of different types:
void mostrarPersona(String nombre, int edad, double altura) {
  print('Nombre: $nombre');
  print('Edad: $edad años');
  print('Altura: $altura m');
}

void main() {
  mostrarPersona('Juan', 25, 1.75);
}

Parameter Examples by Type

Numeric Parameters

double calcularPromedio(double a, double b) {
  return (a + b) / 2;
}

String Parameters

String concatenar(String a, String b) {
  return '$a $b';
}

Boolean Parameters

void verificar(bool esValido) {
  print(esValido ? 'Válido' : 'Inválido');
}

Mixed Parameters

void registrar(String nombre, int id, bool activo) {
  print('$nombre (#$id): $activo');
}

Best Practices

  • Use descriptive parameter names that indicate their purpose
  • Limit the number of parameters (ideally 3 or fewer)
  • Order parameters from most to least important
  • Consider using named parameters for functions with many parameters
  • Always specify parameter types for better code clarity
  • Ensure arguments match the expected parameter types
  • Document complex parameter requirements

Build docs developers (and LLMs) love