Skip to content
← Back to Community
Python Functions Tutorial!
Profile icon
CodingElf66

Python Functions Tutorial!

I already posted my first version of the Python Functions Tutorial, but this one will go a little deeper and detailed into functions.


Why You Should Learn Functions


There are plenty of reasons why you should learn functions. First of all, functions are pretty easy to work with, and it shortens the code. For example, if you want to get the sum of five random numbers, and you want to do it three times? Well, you can just write the function and put it in a for loop! Another reason why you should use functions is that it makes your code look more organized, step-by-step, instead of just directing writing the code all at once.


Other sources

If you find this tutorial confusing or still do not understand the concepts, I suggest you look at the links given below.

Now, let's get to the main part!


Making Functions


** Example 1: **

def func(): print("Hello, world!")

In the first line, we make a name for this function. It is called "func". They can be named any valid variable name.

def func():

In the second line of the code, we write what is done inside the function. We print "Hello, world!", so when we call the function, it basically runs the code that is inside the function. Note that any code inside the function must be indented.

print("Hello, world!")

** Example 2: **

def addition_facts(): print("5 + 2 = 7") print("9 + 28 = 37")

In the first line of the code, the function name is initialized. It is "addition_fact".

def addition_fact():

In the second and third lines, we describe what is inside the function. Note that functions can have unlimited lines; it has no limits!

That is the first part of my tutorial on functions! Let's move on to the next part!


Calling Functions


Calling functions is quite easy. All it requires is one line of code!

** Example 1: **

For example, we'll repeat our functions from the last part. To call them, we just need to write their function name with a full set of parentheses "()". For example, with the function in Example 1 in Part 1, to call it, we just simply do:

func()

This line of code calls the functions. In other terms, we basically run whatever is inside the function.

Output:

Hello, world!

** Example 2: **

addition_facts()

Same scenario here. It just runs the function.

Output:

5 + 2 = 7 9 + 28 = 37

You just learned about calling functions! Let's move on!


Parameters


Parameters are like variables inside the function, and just like variables, you have to define them! Let's do some examples!

** Example 1: **

def add(num1, num2): print(num1 + num2)

In this first example, we have two parameters; num1 and num2. They can be named any valid variable name, too, and there is no limit to how many parameters they are. For now, the parameters are unknown, until the user calls the function and defines the parameters. Let's think about this function for now. Suppose we say that num1 is 4, and num2 is 8. Therefore, this function would return 12. Let's test it out!

To call a function with parameters, we just call the function, then inside the parentheses, we put the values of the parameters.

add(4,8)

As expected, we get 12.

Ouput:

12

** Example 2 **

In this example, we will make a greeting function!

def greeting(name): print("Hello, " + name + "!")

When we call the function, we will give in a value for the name! Then, this function will return "Hello, (name value)!"

Let's say my name is Andrew.

greeting("Andrew")

Output:

Hello, Andrew!
Voters
Profile icon
EthanRachall
Profile icon
bohnj0390
Profile icon
LoWing
Profile icon
10dub
Profile icon
EthanWright23
Profile icon
MajdGhaith
Profile icon
FatimaNahle
Profile icon
capere24
Profile icon
Klara-Marie-Gil
Profile icon
NHec
Comments
hotnewtop
Profile icon
zariradvance

I want a 'class' Tutorial. Pls

Profile icon
TomPilibi

je ne comprends rien du tout!

Profile icon
CodingElf66
Profile icon
GoncaloRod
Profile icon
MultiLangCoder

It's French for "I didn't understand anything at all!"@CodingElf66

Profile icon
CodingElf66

@MultiLangCoder Use Google Translate and paste everything in there. I don't understand French, haha. I'm sorry for that.

Profile icon
Alpha420

Using this example, it seems to me like this is longer. Why don't I just write...

greeting = "Andrew"
print(greeting)

I don't really understand the use of this function.

Profile icon
jgcompany

@Alpha420 i believe its just a lazy example and you definitely have a point. typically developer asks for name and assign it a variable for reuse. then take the variable and create a function from it.

Profile icon
UltimatePython

@Alpha420 I think @CodingElf66 has a point, because if you wanted to greet multiple people, for example, you wouldn't just do this:

greeting = "Andrew" print(greeting) greeting = "Bob" print(greeting) greeting = "Mason" print(greeting) # and so on...

You would create a function, which shortens the code:

def greeting(name): print("Hello", name) greeting("Andrew") greeting("Bob") greeting("Mason")

Functions just make the code neater, don't you think? It just organizes stuff and labels it, also making it easier when you need repetition of that code.

Profile icon
ZACHARYRETTIG

@Alpha420 Why not print('Andrew') ? Basically it is just an example as to show what a function is, as the example you provided is how variables work

Profile icon
MARTINPHILLIPS1

@Alpha420 Its simply an example when you have longer code you don't want to repeat it every time and that is where functions come into play.

They also make your code neater, similar to how a paragraph works in an essay.

Profile icon
Steek
Profile icon
hipohaha

I have completed it successfully, your instructions are excellent and accurate paper minecraft

Profile icon
mycfavisit

Thanks for sharing
www.mycfavisit.blog.

Profile icon
Davidhowdy

Take surveys win prizes.
https://www.thetechsurveys.com/

Profile icon
MuhammadAslam1

Hey There. I found your blog using msn. This is a very well written article. I’ll be sure to bookmark it and come back to read more of your useful info. Thanks for the post. I’ll definitely return. financial service advisor https://murrayfinancialservices.com/

image

image

Profile icon
NomanAnsari2

I like your concept of explaining each parts step-by-step. I think i will learn more or more with the help of uhh guys..thank you very much for this presentation for us.👍...
..and if u have some time i just want to know something like why we use "def"...i am too confused with this..can you explain us please.

Profile icon
EssayUK

In Python, we may access the lesson's properties and processes by utilizing the self keyword. It associates the belongings with the statements delivered. The cause you should utilize self. Python does not employ the syntax to direct instance details, therefore online course support help is needed. https://www.expertessaywriters.co.uk/write-my-essay.php

Profile icon
jilljbently

It's really a very useful and informative topic, I'm playing basketball legends , so maybe you guys would like to try it out.

Profile icon
NaomiWhittaker

@jilljbently it's great to spend time on this once you've finished working on a project. Mastering Python is not easy for me, but I found professional help here. Python is not the easiest language and therefore it is better to approach this issue with the help of knowledgeable people.

Profile icon
Homedepot000

Get amazing gifts by participating in the survey at the official website https://www.homedepotcom-survey.com/

Profile icon
EdikanTheGreat

thank you

Profile icon
UltimateCoder40

Nice tutorial, I found this very helpful, thanks!
Can I help with your project WebViber? I just saw it, and it looks pretty cool!

Profile icon
MustafaTalha1

Print("Uschak King," adamsın)

Profile icon
bendloyed

I thoroughly liked reading your article and found it to be quite informative. If you have any extra time, you are welcome to join me in playing among us

Profile icon
PeterMoss

In Python, we may access the class's properties and methods by using the "self" keyword. It associates the properties with the arguments provided. The reason you should use self. Python does not employ the @ syntax to refer to instance attributes, thus online course help assistance is required.https://onlineclasshelppro.com/take-my-courses.php

Profile icon
Willard4

I essentially love to make and present my power for music to other people who feel the same way. Have a good one.
https://www.mcdvoice.tips/

Profile icon
00-CREDIT

cool

Profile icon
tescoviews

Thanks for sharing, I found a lot of interesting information here.
https://www.tescoviews.live/tesco-views-survey/

Profile icon
NeilDunleavy

this was helpful thanks

Profile icon
13pilkington

woah

Profile icon
Timgb11

Thanks this really helped me!

Profile icon
Wisoky

Great work thanks for sharing really information.

https://www.talktowendys.ltd/

Profile icon
Johnmash2
Profile icon
tnt1234567890

I already knew this so it’s boring .haha

Profile icon
Omay238

@tnt1234567890 Well, some people don't know it. I know how, but it is a good tutorial.

Profile icon
SimonaPaun-Trif

This was really good

Profile icon
[deleted]

kl

Profile icon
DynamicSquid

Hey, with your other tutorial: https://replit.com/talk/learn/Python-Functions-Tutorial/141877

I would recommend combining this one with that one to make one mega tutorial on functions! (it would also be preferable if the tutorial was also in the description instead of being in the repl) Replit always likes big tutorials :)

Profile icon
CodingElf66

@DynamicSquid Sorry, I'm not done with the tutorial. I'm new to markdown, so I'm writing it, then slowly seeing how it works I will try to complete it

Profile icon
CodingElf66
Profile icon
DynamicSquid

@JeffreyChen13 No worries then! Also, if you want a guide to markdown, you can use this link :)

Profile icon
CodingElf66

@DynamicSquid It's ok, in the bottom, it says "Style your post with Markdown!", I got it :)

Profile icon
Bookie0

Nice tutorial, but it's a bit short; it's just about functions. Perhaps add some more content? :)