What is a function in programming? I can’t seem to grasp the definition of it | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

3rd Nov 2019, 7:44 AM
Theodore206
Theodore206 - avatar
4 Answers
+ 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
3rd Nov 2019, 7:49 AM
Arsenic
Arsenic - avatar
+ 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!
3rd Nov 2019, 10:54 AM
Michael Smyth
+ 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.
3rd Nov 2019, 11:03 AM
Asman-H
Asman-H - avatar
+ 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.
4th Nov 2019, 11:01 PM
Jianmin Chen
Jianmin Chen - avatar