Last time I introduced you to basic variable commands and input.
This time I will show you how to use functions, and give you a deeper understanding of variables.
Like last time, we need to start up our IDE and open up our program.
![](https://images.hive.blog/768x0/https://cdn.steemitimages.com/DQmUxZB4chJjqcvWHsg86vCkZZKZvKeMZzphgGrkuJUnRRf/image.png)
Now, to program our function.
Basic syntax for a python function is as follows:
def myFunction():
code_goes_here()
So to start off type this above all of our code:
def start():
It should look like this
![](https://images.hive.blog/768x0/https://cdn.steemitimages.com/DQmZZdodcjn8Vn4oLDSV6UhoVayYYB1yN7V7wyigAeH3mVS/image.png)
Now, underneath your "def" line write a simple print string line.
![](https://images.hive.blog/768x0/https://cdn.steemitimages.com/DQmW4DwW6mDjsgF8aKvh7JQJHb3nVHtACjqTBdhpQninpC7/image.png)
If we run our code at this stage nothing different will happen. We need to initialize our function.
Underneath all of our code call the function. Here is the syntax:
myFunction()
In our case it would be like this:
![](https://images.hive.blog/768x0/https://cdn.steemitimages.com/DQmTMESKPyi8gQYDQScyRqv3i6eGeA1Mmwqz3udKeZv8DUs/image.png)
If we go ahead and run our code again it will look something like this
![](https://images.hive.blog/768x0/https://cdn.steemitimages.com/DQmVPJNMpCQJE3ncxY9TwBKFVikBAWZzAqVqWCTTkApZDQZ/image.png)
Let's deal with more complex functions. Functions involving variables
Inside our functions we can store variables native to the function.
For example,
def myFunction():
variable = 10
print(str(varible))
myFunction()
x = 5
print(str(varible + x))
This code would not work. This is because 'variable' is only declared in myFunction, and can not be globally accessed (most of the time)
![](https://images.hive.blog/768x0/https://cdn.steemitimages.com/DQmPXTS26XsQAHMePT4e8XJBcxj7XKbdKgH4k4bK1WjksHX/image.png)
With functions we can also substitute it parameters, which we define before calling the functions.
Here is what I mean:
def myFunction(string):
print(string)
x = "hello!"
myFunction(x)
![](https://images.hive.blog/768x0/https://cdn.steemitimages.com/DQmbEV8ctZzV24pXURvi7PWmu4h6yUBXYzCLri6udodSJ27/image.png)
Now we can implement this into our code.
You can try it by yourself, it should look something like this:
def start(inputStr):
print("Hey from inside my function!")
print("Here is your input: " + inputStr)
print("Hello World!")
x = input("Input needed: ")
start(x)
Now let's run our program:
![](https://images.hive.blog/768x0/https://cdn.steemitimages.com/DQmVagqDbQ2DA2rTmFfx5giecChGbkQqn1JCAa2QxZRZtgw/image.png)
Now we can get our input back without having to repeat our commands!
![](https://images.hive.blog/768x0/https://cdn.steemitimages.com/DQmWpLovByuA2jsxjL9TVoXF9ipJLdZF5z1iTygYF6kyBaz/image.png)
Here is a more advanced function:
![](https://images.hive.blog/768x0/https://cdn.steemitimages.com/DQmSuScXdVBq96PFBs6BS79BxHEhson6Pr9TydmUaNYNT95/image.png)
Today I taught you how to use functions and use variables in conjunction. This will give you a basic understanding of how to make complex programs for user input, etc.
Next time we will go over IF, ELIF, and ELSE statements.
we love coding
![](https://images.hive.blog/768x0/https://codingforspeed.com/images/i-love-coding.jpg)
Reply !stop to disable the comment. Thanks!Hello! Your post has been resteemed and upvoted by @ilovecoding because ! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!