Creating and Calling Functions!

--Originally published at TC101 – Peaz Cooper

Well well well! First of all! Ask yourself the following questions:

How can you create a function on python? How can I call it? Why DC Comics doesn’t know how to make good movies? Is it weird to call my function Martha? Together we will learn the answers to these awesome and mind-blowing questions!


The main idea of a function in Python is to assign a set of code and variables to a text. It is however referred as “why you choose to write and save a program, rather than writing out the entire program every time you want to execute it.” by the Python guru Sentdex, and personally, I think there’s no better example of it.

1) To begin a function: you define the function by using the keyword “def” that stands for “define”.

     def

2) Naming the function as you pleased, but you have to be careful when naming the variable and the function (try to use names that you know you will remember).

     def example

3) The keyword def should be followed by your function name (check out step #2)  and a parentheses ” ( ) “. YOur function may need parameters, and they should be placed inside of the parentheses. The double dot sign ” : ” needs to be put on the end of the parentheses in order to say “here’s where it stops”.

     def example ():

4) If you want to run the code you’ll see that nothing happens! And please do not panic cause it’s otganic. In order to run it you must type print a value. In this example I’m printing a basic function (z=3+1). Later all we need to do is to close again by using “print”.

     def example ():

   

      print(‘basic function’)

            z = 3+1

            print (z)

5) Once again! If you smash the run button you’ll panic again because it didn’t do anything. Cause we are not actually calling the function into action, so in order to do that you can type it out.

     def example ():

            print(‘basic function’)

            z = 3+1

            print (z)

     example ()

6) Once you run it you’ll see that it actually works!

basic function

4


If you follow these steps you’ll be coding like a bawse in no time!!!

#PeazCooperOut #TC101