+ 3
What is a function in programming? I can’t seem to grasp the definition of it
I have been trying my hand at python, and other programming languages. They all have functions but what is a function simply?
4 Respostas
+ 9
A function is just a block of code that gets executed whenever it is called.
This is the simplest definition that I could create
+ 3
Meet Bob, Bob's job is to go to the shop for his old mother.
His mother calls and asks bob to go to the shop.
Using this example bobs function is to go shopping and the call off his mother is the function call itself. Hope that adds real world clarity to it. Happy coding!
+ 2
Agree with Arsenic.
Look at this code:
text = 'hello world'
print(text.capitalize() + '!')
# Hello world!
But if we need to do like this with many sentences? I think it would have been very tough, applying many times the same operation. Here come functions.
Look at this little function:
def exclm(a):
return a.capitalize() + '!'
Now we can use it every time we need to edit our sentences:
text = 'hello world'
print(exclm(text))
# Hello world!
Function - code, that requests (not always) something, does some operations with something, and returns other, edited thing. Main usage - not to repeat the same actions, but it is just a little of it's usage.
+ 2
A function is just a block of code that can be reused over an over again. If you have something you'd like to do in a program over and over again at different times, creating a function is the best way to go.