[Swift] Playgrounds Learn to Code 1-02. Functions

BOMY·2022년 8월 2일

Playgrounds

목록 보기
2/6
post-thumbnail

<TIL 작성 목표>

  • Playgrounds Learn to Code 1, 2의 챕터, 목차를 내 언어로, 한 문장으로 설명할 수 있을 때까지 해보기! 초등학생한테 이해시킬 수 있을 정도로!
  • 주변 개념까지 정리하고 Playground의 설명으로 충분하지 않으면 검색해서 찾아보고 내 생각을 찾아서 정리해 보기!
    ex) 논리 연산자가 뭐예요? 왜 써요? 종류는요?


나의 iOS 개발 커리큘럼

  • Playgrounds Learn to Code 1
    - Commands ✔
    - Functions ✔
    - For loops
    - Conditional Code
    - Logical Operators
    - While Loops
    - Algorithms
  • Playgroubds Learn to Code 2
    - Variables
    - Types
    - Initialization
    - Parameters
    - World Building
    - Arrays
  • iOS App Dev Tutorials - 3개월간 2~3회독
  • Boostcourse - 6개월


Playground Learn to Code 1

👉 Functions (함수) - Grouping Tasks

  • function: A named set of commands that can be run whenever needed. 필요할 때마다 실행될 수 있는 이름 붙여진 명령어들의 집합

  • Functions are "self contained" modules of code that accomplish a specific task. 함수는 특별한 일을 성취하기 위한 독립된 코드 모듈이다.

    • 모듈이란? 프로그램을 구성하는 구성요소, 프로그램을 구성하는 시스템을 기능 단위로 독립적으로 분리한 것. 단순히 여러개로 분리한 것이 아니라 하나 이상의 논리적인 기능을 수행하기 위한 명령어들의 집합!
     Every day, you perform a range of tasks automatically,
      without thinking about what you're doing.
      
      Even a simple task, like tying your shoe, took time to atutomate.
      
      You learned to do it using a sequence of steps.
      
      To tie your shoe, you do the following:
      	loop
      	swoop
      	pull
      
      In programming, a function allows you to name set of commands,
      which you can then run any time you want.
      
        func tieMyshoe() {
            loop
            swoop
            pull
        }
      
      After you define your function, call it by name to run its commands.
      
  • To define a function, use func and choose a name. Always follow a function name with (). And give your function its behavior by adding commands inside the curly braces '{}'.

  • ex) func tieMyshoe() {
    	loop()
        swoop()
        pull()
    	}
    
  • Functions part words

    • a range of ~: a number of different things of the same general kind.
      같은 종류의 여러가지 다른 것(다양한)
    • take time to do something: to spend enough time to do something well or carefully
      어떤것을 잘하거나 신중하게 하기 위해 충분한 시간을 보내는 것
    • composition: The process of combining small parts of an application to solve a larger problem.
      큰 문제를 해결하기 위한 기능의 작은 부분들을 결합하는 과정
    • efficient : achieving maximum productivity with minimum wasted effort or expense.
      최소한의 노력과 비용 소비와 함께 생산성을 최대로 달성하는 것(효율적인, 효과적인)
    • beneath: in or to a lower position than someone or something, under someone or something
      어떤 사람/사물보다 낮은 위치에 있거나 아래에 있는 것
    • typically: in a way that shows all the characteristics that you would expect from the stated person, thing, or group 사람, 사물, 조직에서 시작할 때 예상할 수 있는 특징을 보여주는 방식(일반적으로)
    • define: To provide the explicit value or behavior of a newly created peice of code, such as function, variable, or custom type. For example, you define a function by providing a set of commands within the function to tell it what to do. 함수나 변수, 사용자가 정의한 유형과 같이 새로 생성된 코드의 명확한 값이나 행위를 제공하는 것. 예를 들면, 함수 안에 명령어 집합을 제공함으로서 함수를 정의하여 그것이 무엇을 해야하는지 지시할 수 있다.(정의하다)
    • explicit: clear and exact 명확하고 정확한
    • call: To call a function in code is to instruct that function to run, performing the actions defined inside it. For example, calling the moveForward() function performs its actions that result in a forward move. 코드에서 함수를 호출하는 것은 함수를 실행하고, 그 안에 정의된 행동을 수행하기 위해 지시하는 것이다. 예를 들면, moveForward() 함수를 호출하는 것은 그것이 결과적으로 앞으로 움직이는 행위를 수행하도록 한다.(호출하다)
    • pattern: A repeating set of circumstances or data. 상황이나 데이터의 반복되는 집합
    • circumstance: a fact or event that makes a situation the way it is 상황을 있는 그대로 만드는 사실이나 사건(상황)
    • at a glance: from one's first look; immediately 한 눈에 보자마자, 즉각적으로
      • glance는 to shine, reflect light, or sparkle으로 섬광을 의미한다! 번쩍! 하자마자! 이런 느낌으로 이해하면 될 듯!
    • decomposition: The process of breaking a large problem into smaller, more manageable pieces. 큰 문제를 작고 더 다루기 쉬운 부분으로 분해하는 과정이다.
  • 예제 문제: turnRight() 만 사용하여 turn to right 하기

    func turnRight() {
        turnLeft()
        turnLeft()
        turnLeft()
    }

👉 한 줄로 요약한 Functions

Functions(함수)필요할 때마다 불러서 사용할 수 있도록 이름지어진 명령어들의 집합이다.

ex) 오른쪽으로 회전하는 작업을 수행하기 위한 함수!

func turnRight() {     -> moveRight 라는 이름의 함수를 정의
  turnLeft()
  turnLeft()
  turnLeft()           -> 왼쪽 방향으로 세 번 회전하는 명령어 추가
}
  • 추가적으로, 함수를 정의하기 위해서는 func를 사용하고 이름을 선택해야 한다. 함수는 항상 이름 뒤에 괄호'()'가 있어야 하고, 기능을 부여하기 위해서는 중활호'{}' 안에 명령어들을 추가해야 한다

  • CompositionDecomposition차이
    composition큰 문제를 해결하기 위해 함수들을 결합하는 것이고, decomposition큰 문제를 작고, 더 다루기 쉬운 조각들로 분해하는 것이다.




22.08.05 TIL 끝!


썸네일은 Banner Maker로 제작하였습니다.

profile
한 걸음 한 걸음!

0개의 댓글