Hello, everybody and welcome to a python 3 article. We're going to be discussing the basics of a python function.
The idea of a function is to assign a set of code and possibly variables known as parameters to a single bit of text.
So you can think of it a lot like why you choose to write and save a program rather than writing out the entire program every time you want to execute it or in a much smaller scale.
Create function
So to begin a function you define the function by using the Keyword def short for Define?
def food():
task
task
task
in this notifies python of the impending function definition
From there you're going to type out whatever name you want to refer to your function as now you want to be kind of careful
in the name of your function.
Just like you would want to be careful in the name of your variable you want to choose something unique, and it's not going to conflict with anything else.
def food():
print('2x bread')
print('3x cheese')
Rules
Now if you define a function and you call the time it's going to conflict with the time module should you use it so you don't want to use time?
So for us, we're just going to use example and after the definition of the functions name you have the parentheses and within the parentheses you have any parameters.
void hello()
{
printf("hello world\n");
printf("this is in C\n");
}
For now, we're just gonna leave those empty, but I will show you guys in some future article how to use parameters after.
Going to be your function block so as you type code and you hit enter you've got this is all contained in your function
def function():
inside
inside
inside
inside
outside
And then finally when you come over here and start typing more code that's outside of the function.
so this is the function so that's kind of the idea of python functions whereas other programming languages generally you're going to have curly braces and you type within the curly braces.
When do we use functions?
In Python, you use functions all the time. Even functions of other people or the python standard library like the input function
we use just indent and indent only so for example. We're just going to say print basic function. so this is the function basics
Top comments (0)