CodeBlanks Logo Icon CodeBlanks

Summary - Functions

Write a store calculation function with types.

Summary - Functions

Functions combine type annotations, optional and default parameters, rest parameters, and overloading to create powerful and flexible definitions. Choosing the right features improves both type safety and usability.

function hesapla(fiyat: number, oran: number = 10): number {
  return fiyat - (fiyat * oran) / 100;
}

These features together enable writing safe and flexible functions.

Practice Store calculation function

Define a function named indirimli with fiyat: number and oran: number = 10 parameters and return type number. The function should return fiyat - (fiyat * oran) / 100. Print the result of calling indirimli(200) with console.log.

Starter code

index.ts
// indirimli adında bir fonksiyon tanımla
// fiyat zorunlu sayı, oran varsayılan 10 ile sayı olsun
// dönüş türü number, fiyat - (fiyat * oran) / 100 döndürsün
// indirimli(200) sonucunu yazdır

Sample solution

index.ts
function indirimli(fiyat: number, oran: number = 10): number {
    return fiyat - (fiyat * oran) / 100;
}
console.log(indirimli(200));

Expected output

180

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which feature lets a function accept any number of arguments?

  • Default parameters
  • Rest parameters Correct answer
  • Return type annotation
  • Optional parameters

Explanation: A rest parameter (<code>...args</code>) collects a variable number of arguments into a single array; optional and default parameters only provide skippable parameters.

2 Add the default parameter value

Question
function indirim(fiyat: number, oran = ____): number { return fiyat - (fiyat * oran) / 100; }
Answer
function indirim(fiyat: number, oran = 10): number { return fiyat - (fiyat * oran) / 100; }

Explanation: The default discount rate must be <code>10</code> (a number); <code>"10"</code> is a string, <code>true</code> is a boolean, and <code>oran</code> is a self-reference (undefined).

3 Function concepts summary

Term Meaning
function f(a: number): number Typed function
oran?: number Optional parameter
...sayilar: number[] Rest parameter
oran: number = 10 Default parameter

All lessons in Beginner