Call by value and Call by reference is a fundamental topic in a programming language. It tells you how your data will be manipulated in scopes.
Value types are Pass by Value. This means that copies of their value are used when passed to functions or assigned to other variables.
This behavior is called Pass by Value because you are passing a copy of the value to the function. Primitive types (e.g., numbers, strings, booleans) are immutable in Swift, this means their values cannot be changed after creation.
These values are like locked boxes – you can't change what's inside. If you modify the value inside a function, it won't change the original value outside the function because you're working with a separate copy. When you modify a primitive type inside a function, you're working with a new copy of the value, leaving the original value unaffected.
Int- Represents signed integers.
UInt- Represents unsigned integers.
Specific sizes like
Int64are also available.
Double- Represents double-precision floating-point numbers.
Float- Represents single-precision floating-point numbers.
Bool- Represents Boolean values (true or false).
Character- Represents a single Unicode character.
String- Represents a collection of characters.
Array- Represents an ordered collection of elements of the same type.
Dictionary- Represents an unordered collection of key-value pairs.
Set- Represents an unordered collection of unique elements.
Range- It is a fundamental data type used to represent a range of values. Ranges are often used for iterating over a sequence of values, accessing elements in a collection, or performing various other operations where a span of values is needed.
Tuples - Tuples are value types that allow you to group multiple values together. They are commonly used for returning multiple values from functions.
Structs - Structs are value types that can encapsulate properties and methods. They are typically used for small, self-contained pieces of data.
Enums - Enums can also be value types in Swift and are often used to represent a group of related values or states.
Reference Types are Passed by Reference. The primary reference type is classes. When you pass a reference type (a class) as an argument, you're passing a reference to the same object in memory. Changes inside the function affect the original object because both the function and the calling code reference the same memory location.
Classes: Classes are reference types and are used to create objects with properties and methods. They have reference semantics, meaning when you pass a class instance as an argument, you're passing a reference to the same object in memory.
Functions: Functions are reference types in Swift. You can assign functions to variables, pass them as arguments, and return them from other functions.
Closures: Closures, like functions, are reference types. They capture and store references to variables and constants from the surrounding context.
Objects and Instances: Any custom objects or instances created from classes are reference types. This includes instances of custom data types you create.