Course source code: https://codistwa.com/guides/functions. More courses on https://codistwa.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ============================================================ | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ============================================================ | |
# 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