{ "cells": [ { "cell_type": "markdown", "id": "6838b2de", "metadata": {}, "source": [ "# Hello" ] }, { "cell_type": "code", "execution_count": null, "id": "4200d243", "metadata": {}, "outputs": [], "source": [ "\"\"\"\n", " Example to show scope of variable in a main() program.\n", "\n", "\"\"\"\n", "\n", "ag = 34.56 # Define in three global values.\n", "bg = 41.45\n", "x = 100.0\n", "\n", "def main():\n", " \"\"\"\n", " The main programme\n", " \"\"\"\n", " global ag # Make ag writable within main() \n", " # Print out values, not bg can be read.\n", " print(\"Value of ag in main is : \" + str(ag) + \" and bg is : \" + str(bg))\n", "\n", " ag += 10 # Undate value of ag inside main()\n", " print(\"New Value is ag is : \" + str(ag))\n", "\n", " x = 30.0 # Declare x inside main (note NEW variable) and NOT global.\n", " print(\"Internal value of x is : \" + str(x))\n", " \n", "# Run main rogram\n", "main()\n", "# Do an external print to check globals.\n", "print(\"External value of ag is : \" + str(ag))\n", "print(\"External value of x is : \" + str(x))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }