Steemers Coding Class 04 - Learn Coding 10 Minutes a Day For Very Beginners
Yo, It's Pooria. What's up?
This is epidose four of a series of articles for Steemrers to learn coding with only 5-10 minutes of practice a day. If it's the first time you see this topic follow from the beginning.
Previous episodes:
Steemers Coding Class - Episode 01
Steemers Coding Class - Episode 02
Steemers Coding Class - Episode 03
functions
Functions are one of the most important concepts of programming in any language. To put it simply, functions are separate part of codes that do specific tasks for you. Evertime you see a statements with paranthesis like this print()
it's a function. Some of them are hardwired inside your programming languge like print()
but you can also make your own functions.
Most of the functions get one or more input and return an output.
you define a function with def
statement and return it's output with return
def test(x):
return x
it's a simple function that return the same input we are sending to it. try to print it like this in your editor:
def test(x):
return x
print(test(5))
print(test(7))
print(test(9))
Pay attention that you are sending inputs of 5, 7 and 9 to your function and print the returned output. Not that exciting huh?
change the function to this and check the same result:
def test(x):
return x + 1
Awesome! we did calculation inside our function. We can have more than one input per function:
def test(x, y):
return x + y
Write the function above and try to print its result like we did before with two inputs.
Tip: while having more than one input, separate them using,
A practical example
Let's say we need a program to get the age of the user, if it's more than 21 offer her a mug of beer and if not offer her orang juice.
We can do it like this(the old way):
age = input("enter your age")
if int(age) > 21:
message = "have some beer"
else :
message = "have some orange juice"
print(message)
But to do it with functions we can separate the logic of selecting drink, send the age to a function with the name select_drink()
and get the message as output and just print it.
def select_drink(age):
if int(age) > 21:
return "have some beer"
else :
return "have some orange juice"
age = input("enter your age")
message = select_drink(age)
print(message)
Try it in your editor. Change the legal age for drinking as you prefer it to be in the world!
Tip: notice that I am using indention (those extra spaces before each line) for all the codes that I want to have inside my function. However if some codes inside my function still need indents (like the if in example above) I add more indention. Otherwise, it doesn't work as expected.
Do it yourself
Don't just read and upvote. Do something yourself! Write a program using functions to say hello to each name it gets. Use a loop to ask names repeatedly for 10 times and say hello to each of them before asking the next name.
If you forgot about loops, read episode three again!
Ask me any questions you had and write your codes in the comments to help other future coders too.
Be consistent with your learning goals so you feel awesome about yourself!
Cheers,
Pooria
Image source: https://www.instagram.com/minimalsetups/
Thanks for posting. Great tips.
You're welcome! hope it helps :-)
Just wondering why you flagged my recent post... were you not satisfied with something?
Sorry that was a development mistake (happened while working with API) I Upvoted that post now.
Thanks for explaining. The flag thing is a touchy subject for me. I tend to snap now after someone ran a bot on two of my articles and gave me about 15 flags or more all in one shot. I appreciate your honesty.
I understand. I will try to be more careful about this matter from now.
Sorry for bothering you and writing this on your blog. Have a good day.
No problem at all. Same to you my friend.
Thank you for this, ill have to catch up with your recent posts about this :)