{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4dd1330b-6e78-44b3-88d1-3bf894bb5466",
   "metadata": {},
   "source": [
    "```{index} function; definition\n",
    "```\n",
    "\n",
    "(a-first-function)=\n",
    "\n",
    "# Introduction\n",
    "\n",
    "Methods/functions are reusable, callable, and customizable pieces of code in a computer program. When a function is a part of a class, it’s called a method. In OOP languages, functions are called methods because they are always declared inside and operated through designated classes. It is, therefore, by definition, more accurate to call these subroutines methods rather than functions in C# since C# is an OOP language. However, when discussing the construct of subroutine in general, the general term **function** seems appropriate and you will see people use the terms interchangeably from time to time.\n",
    "\n",
    "In C# or other object oriented programming (OOP) languages, methods are usually associated with a class. In other languages, you may hear the term function. A function is similar to a method, but it is not tied to a class. Due to the similarity in overall usage, it is common to use the terms methods and functions as synonyms.\n",
    "\n",
    "```text\n",
    "Method (class member)        Local function                 Standalone function\n",
    "                             (inside method)                (other languages)\n",
    "+-----------------------+  +--------------------------+  +-----------------------+\n",
    "| class Program         |  | class Program            |  | [code...]             |\n",
    "| {                     |  | {                        |  | int add(int a, int b) |\n",
    "|   static int Add(...) |  |   static void Main()     |  | {                     |\n",
    "|   { return a + b; }   |  |   {                      |  |   return a + b;       |\n",
    "| }                     |  |     int Add(...) { ... } |  | }                     |\n",
    "+-----------------------+  |   }                      |  | [code...]             |\n",
    "                           | }                        |  +-----------------------+\n",
    "                           +--------------------------+\n",
    "```\n",
    "\n",
    "You have seen the **Main()** method in C# console app programs generated using the .NET template with `--use-program-main`. In the traditional class-based style, the Main() method is the application’s entry point. When the application is run, execution begins at the start of the Main() method. In modern C#, programs may also use top-level statements without an explicit Main() method.\n",
    "\n",
    "Up until this unit, you wrote all code in the main method, but now you will be creating new methods that can be called by the main method. Methods are **named** code blocks that can be reused (called) whenever we needed. Methods are an **abstraction** of your code because methods abstract away details (you use the methods by their names only) and that serves to organize your code by function and reduce the repetition of code. In addition, it helps with debugging and maintenance since changes to that block of code only need to happen in one place. Here are some of the main reasons to use multiple methods in your programs:\n",
    "\n",
    "- Reusing Code: Avoid repetition of code.\n",
    "- Reducing Complexity: Divide a problem into sub-problems to solve it a piece at a time.\n",
    "- Maintainability and Debugging: Smaller and specific code blocks are easier to trace and debug. Also, you only need to maintain one piece of code even if it's used all over the place.\n",
    "\n",
    "When you see duplicate lines of code, that is a signal for you to make a new method. A method is a named set of statements. When we want to execute the statements, we **call** the method using its name. Methods are called in two different ways:\n",
    "\n",
    "- **Instance methods**: methods that are called using an object, referred to as instance methods or object methods. \n",
    "- **static methods**: the methods in this unit are called without an object, so they are static methods. Static methods are also referred to as **class methods**.\n",
    "\n",
    "Some important characteristics of methods/functions are:\n",
    "\n",
    "- **SRP**: In general, a method should do a single thing, which we call single responsibility principle (SRP). \n",
    "- **Method composition**: You combine a sequence of simple methods to build more complex desired behavior.\n",
    "- **Method call**: Methods will not run until it is **called**. We say we are making a “**function call**” or “method call” when we invoke/use the subroutine.\n",
    "- **Arguments**: When calling a method, we often pass data into the method and. The data passed to the method are called arguments.\n",
    "- **Parameters**: The variables in the method heading defined to receive those arguments are called parameters. \n",
    "- **Return**: We usually design the method to return values when called. \n",
    "\n",
    "Observing a C# method: The Main method in a console application is a method and it looks like the following when you generate it\n",
    "with `dotnet new console --use-program-main`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a761e8a1",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "(5,17): warning CS7022: The entry point of the program is global code; ignoring 'Program.Main(string[])' entry point.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "namespace test2;\n",
    "\n",
    "class Program\n",
    "{\n",
    "    static void Main(string[] args)\n",
    "    {\n",
    "        Console.WriteLine(\"Hello, World!\");\n",
    "    }\n",
    "}\n",
    "\n",
    "// Program.Main(Array.Empty<string>());"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7fb08a2",
   "metadata": {},
   "source": [
    "The Main method, however, is unique by design because it is designated to be the entry point of an\n",
    "application. Although you can write code in the Main method, you will learn how to write methods\n",
    "in addition to, and outside of, the Main method to extend the functionality of your code.\n",
    "\n",
    "```{important}\n",
    "**One entry point (the `Main()` method) per project, but a project can have many `.cs` files.**\n",
    "\n",
    "Each `.csproj` project can only have one `Main()` — or one file with top-level statements. However, a project can include as many additional `.cs` files as needed; those files simply define extra classes and methods that `Main()` calls. If two programs each need their own `Main()`, they must be two separate projects (two separate `.csproj` files).\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "footnotes",
   "metadata": {},
   "source": [
    "```{rubric} Footnotes\n",
    "```\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "file_extension": ".cs",
   "mimetype": "text/x-csharp",
   "name": "polyglot-notebook",
   "pygments_lexer": "csharp",
   "version": "13.0"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "name": "csharp"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
