{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "aabc1201",
   "metadata": {},
   "source": [
    "```{index} method; summary of syntax\n",
    "```\n",
    "\n",
    "(static-method-summary)=\n",
    "\n",
    "# Static Methods\n",
    "\n",
    "This chapter has introduced static methods: those used in procedural programming as opposed to {ref}`instance-methods` used to implement object-oriented programming."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45c3706d",
   "metadata": {},
   "source": "## Method definition\n\nThe general syntax for defining a static method is:\n\n```csharp\nstatic returnTypeOrVoid MethodName ( parameter_list )\n{\n   // statements in the method body...\n}\n```\n\nThings to know about defining a method in C#:\n\n1. **Parameters**: The *parameter list* can be empty or contain one or more comma separated parameter entries. Each parameter entry has the form \n   > type parameterName\n\n2. **Modifier**: If the method is going to be called from outside its class, the heading needs to start with `public` before the `static`.\n\n3. **Return Type**: If **returnTypeOrVoid** in the heading is not `void`, there must be a *return statement* in the method body. A return statement has the form\n\n   > `return` *expression* `;`\n\n   where the expression should be of the same type as in **returnTypeOrVoid**.\n   Execution of the method terminates immediately when a return statement\n   is reached.\n\n4. In the traditional class-based style, execution starts at a method with a heading including\n\n   > `static void Main`\n\n   In modern C#, programs may also use *top-level statements* without an explicit `Main` method."
  },
  {
   "cell_type": "markdown",
   "id": "fbc6312a",
   "metadata": {},
   "source": "## Method Calls\n\n1. A method call takes the form\n\n   **MethodName** `(` actual parameter list `)`\n\n   A method call makes the method definition be *executed*.\n\n2. The actual parameter list is a comma separated list of the *same*\n   length as the parameter list. Each entry is an expression.\n   The entries in an actual parameter list do *not* include type declarations.\n\n   Effectively, the method execution starts by assigning to each\n   parameter variable the corresponding value from\n   evaluating the actual parameter expression.\n   In particular, that means the actual parameter values must be allowed\n   in an assignment statement for a variable of the parameter's type!\n\n3. If the method has return type `void`, it can only be used syntactically\n   as an entire statement (with a semicolon added). After the method\n   call completes, execution continues with the next statement.\n\n4. If there is a non-void return type, then the method call is syntactically\n   an expression in the statement where it appears.\n   The execution of such a method must reach a return statement. The value\n   of the method-call expression is the value of the expression in this\n   return statement.\n\n5. A method with a return value can also legally be used as a whole statement.\n   In this case the return value is lost. Though legal, this is often an error!\n\n"
  },
  {
   "cell_type": "markdown",
   "id": "49d555a8",
   "metadata": {},
   "source": [
    "## Scope\n",
    "\n",
    "1. A variable declared inside a method definition is called a *local variable*.\n",
    "   This declaration may be in either the parameter\n",
    "   list or in the body of the method.\n",
    "\n",
    "2. A local variable comes into existence after the method is called, and ceases\n",
    "   to exist after that method call terminates. A local variable is invisible\n",
    "   to the rest of the program. Its *scope* is just within that method. Its\n",
    "   lifetime is just through a single\n",
    "   method call. Its *value* may be transferred outside of the method scope\n",
    "   by standard means, principally:\n",
    "\n",
    "   - If it is the expression in a return statement, its value is\n",
    "     sent back to the caller.\n",
    "   - It can be passed as an\n",
    "     actual parameter to a further method called within its scope."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7099956e",
   "metadata": {},
   "source": [
    "## Static Variables\n",
    "\n",
    "1. There may be a declaration prefaced by the word `static` that appears\n",
    "   *inside* a class and *outside* of any method definition in the class.\n",
    "   Static variables are visible within each method of the class, and may\n",
    "   be used by the methods.\n",
    "2. A common use of a static variable is to give a name to a constant\n",
    "   value used in multiple methods in the class."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "footnotes",
   "metadata": {},
   "source": "```{rubric} Footnotes\n```\n"
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "csharp",
   "notebook_metadata_filter": "-all"
  },
  "language_info": {
   "name": "csharp"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}