What are the Functions in swift ?

 Function In Swift

                Functions are self-contained chunks of code that perform a specific task. You give a function a name that identifies what it does, and this name is used to “call” the function to perform its task when needed.


                Swift’s unified function syntax is flexible enough to express anything from a simple C-style function with no parameter names to a complex Objective-C-style method with names and argument labels for each parameter. Parameters can provide default values to simplify function calls and can be passed as in-out parameters, which modify a passed variable once the function has completed its execution.


                Every function in Swift has a type, consisting of the function’s parameter types and return type. You can use this type like any other type in Swift, which makes it easy to pass functions as parameters to other functions, and to return functions from functions. Functions can also be written within other functions to encapsulate useful functionality within a nested function scope.

Functions are self-contained chunks of code that perform a specific task

Working with a function in Swift has two parts:

  1. Defining the function
  2. Calling the function
                
                When you define a function, you can optionally define one or more named, typed values that the function takes as input, known as parameters. You can also optionally define a type of value that the function will pass back as output when it’s done, known as its return type.

                Every function has a function name, which describes the task that the function performs. To use a function, you “call” that function with its name and pass it input values (known as arguments) that match the types of the function’s parameters. 

                A function’s arguments must always be provided in the same order as the function’s parameter list.

Defining the function :






Calling the function :






Function Parameters and Return Values :

                Function parameters and return values are extremely flexible in Swift. You can define anything from a simple utility function with a single unnamed parameter to a complex function with expressive parameter names and different parameter options.

Functions Without Parameters :

                Functions aren’t required to define input parameters. Here’s a function with no input parameters, which always returns the same String message whenever it’s called:






                The function definition still needs parentheses after the function’s name, even though it doesn’t take any parameters. The function name is also followed by an empty pair of parentheses when the function is called.

Functions With Multiple Parameters :

                Functions can have multiple input parameters, which are written within the function’s parentheses, separated by commas.

                This function takes a person’s name and whether they have already been greeted as input, and returns an appropriate greeting for that person:








                You call the greet(person:alreadyGreeted:) function by passing it both a String argument value labeled person and a Bool argument value labeled alreadyGreeted in parentheses, separated by commas. Note that this function is distinct from the greet(person:) function shown in an earlier section. Although both functions have names that begin with greet, the greet(person:alreadyGreeted:) function takes two arguments but the greet(person:) function takes only one.

Functions Without Return Values :

                Functions aren’t required to define a return type. Here’s a version of the greet(person:) function, which prints its own String value rather than returning it:






                Because it doesn’t need to return a value, the function’s definition doesn’t include the return arrow (->) or a return type.

NOTE : Strictly speaking, this version of the greet(person:) function does still return a value, even though no return value is defined. Functions without a defined return type return a special value of type Void. This is simply an empty tuple, which is written as ().

The return value of a function can be ignored when it’s called:








                The first function, printAndCount(string:), prints a string, and then returns its character count as an Int. The second function, printWithoutCounting(string:), calls the first function, but ignores its return value. When the second function is called, the message is still printed by the first function, but the returned value isn’t used.

NOTE :  Return values can be ignored, but a function that says it will return a value must always do so. A function with a defined return type can’t allow control to fall out of the bottom of the function without returning a value, and attempting to do so will result in a compile-time error.

Functions with Multiple Return Values :

                You can use a tuple type as the return type for a function to return multiple values as part of one compound return value.

                The example below defines a function called minMax(array:), which finds the smallest and largest numbers in an array of Int values:








                The minMax(array:) function returns a tuple containing two Int values. These values are labeled min and max so that they can be accessed by name when querying the function’s return value.

                The body of the minMax(array:) function starts by setting two working variables called currentMin and currentMax to the value of the first integer in the array. The function then iterates over the remaining values in the array and checks each value to see if it’s smaller or larger than the values of currentMin and currentMax respectively. Finally, the overall minimum and maximum values are returned as a tuple of two Int values.

                Because the tuple’s member values are named as part of the function’s return type, they can be accessed with dot syntax to retrieve the minimum and maximum found values:






A parameter is the “parking spot”. An argument is the “car” you park in the “parking spot”.

Comments

Popular posts from this blog

POWER BI

Face Recognition

BOOTSTRAP