Skip to content

Instantly share code, notes, and snippets.

@codistwa
Last active February 23, 2022 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codistwa/8105ae833f10d84535f65358ec41ec3a to your computer and use it in GitHub Desktop.
Save codistwa/8105ae833f10d84535f65358ec41ec3a to your computer and use it in GitHub Desktop.
Course source code: https://codistwa.com/guides/functions. More courses on https://codistwa.com
// ============================================================
// Without parameter
// ============================================================
function double () {
console.log('Hello world')
}
console.log(double()) // Hello world
// ============================================================
// One parameter
// ============================================================
function double (x) {
return x * 2;
}
const square = (x) => x ** 2;
console.log(double(4)); // 8
console.log(square(4)); // 16
// ============================================================
// Two parameters
// ============================================================
const ids = (firstName, lastName) =>
`My name is ${firstName} ${lastName}`;
console.log(ids('Mickael', 'Jackson'));
// ============================================================
// Scope
// ============================================================
const apple = 3;
const getApple = (apple) => apple - 2;
console.log(getApple(30)); // 28
// ============================================================
// Optional, default
// ============================================================
const id2 = (firstName, age = 30) => {
if (firstName) {
return `My name is ${firstName} and I have ${age} years old`;
}
return `I have ${age} years old`;
}
console.log(id2('Francis')); // My name is Francis and I have 30 years old
console.log(id2()); // I have 30 years old
# ============================================================
# Without parameter
# ============================================================
def double():
print('Hello world')
print(double()) # Hello world
# ============================================================
# One parameter
# ============================================================
def double(x):
return x * 2
def square(x):
return x ** 2
print(double(4)) # 8
print(square(4)) # 16
# ============================================================
# Two parameters
# ============================================================
def ids(first_name, last_name):
return f'My name is {first_name} {last_name}'
print(ids('Mickael', 'Jackson'))
# ============================================================
# Scope
# ============================================================
apple = 3
def get_apple(apple):
return apple - 2
print(get_apple(30)) # 28
# ============================================================
# Optional, default
# ============================================================
def id2(first_name='', age=30):
if first_name:
return f'My name is {first_name} and I have {age} years old'
return f'I have {age} years old'
print(id2('Francis')) # My name is Francis and I have 30 years old
print(id2()) # I have 30 years old
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment