• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Rowell Dionicio

Get Techie With It

  • Home
  • About
  • Resources
    • Archives
    • Book List
    • YouTube
  • Learn
    • DevNet Associate
    • PCNSA Certified
  • Blog
  • Contact
  • Show Search
Hide Search

Python Day 1: Functions

September 6, 2018 By Rowell Leave a Comment

The start of my 30 Days of Code begins with Functions. I had already read about flow with For and While loops. 

Today I read into Functions. 

Functions are like independent programs within a program. And it is defined before it is ever called. 

[python]
def myFunction():
    print('Hello world')
[/python]

To utilize the function, it is called within the program or script using the function name:

[python]
myFunction()
[/python]

The purpose of a function is to return a value. 

Within a function, local variables can be defined. Local variables only exist (scoped) within the function and are destroyed or forgotten after the function has completed. While local variables are scoped only within the function, there is a concept of global variables. 

Global variables are scoped outside of a function and can be used inside of a function. Take the example below:

[python]
def myFunction():
    global myVariable
    print('Hello ' + myVariable)

myVariable = 'Rowell'
myFunction()
[/python]

The global variable statement is used inside the function but the variable is assigned outside of the function, globally. The output is as follows:

[python]
>>> myFunction()
Hello Rowell
[/python]

Share this:

  • Facebook
  • LinkedIn
  • Twitter

Related

Filed Under: Coding Tagged With: functions, python

About Rowell

Wi-Fi expert. Coffee addict ☕️. Tech nerd. Business owner.

Reader Interactions

Leave a Reply Cancel reply

Primary Sidebar

Recent Posts

  • Passed Palo Alto Networks Certified Security Administrator (PCNSA)
  • 5 Years Running
  • Q4 2021 and Yearly Income Report
  • I PASSED JNCIA-MistAI
  • Admins and Role-Based Access Control – PCNSA

Categories

  • bschool
  • Certifications
  • Coding
  • DevNet Associate
  • Events
  • Lab
  • Networking
  • Personal
  • Podcasting
  • Professional
  • Reviews
  • Security
  • Short Stories
  • Uncategorized
  • Wireless

Archives

  • May 2022
  • January 2022
  • December 2021
  • November 2021
  • August 2021
  • July 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • November 2018
  • September 2018
  • August 2018

Copyright © 2022 · Written by Rowell Dionicio · You're awesome.