mazembe11
Badges: 6
Rep:
?
#1
Report Thread starter 2 years ago
#1
What is a synonym for the word return in Python Programming. Please do not use the word return when explaining to me what it means. Another word for return would be helpful.
0
reply
KVJ
Badges: 11
Rep:
?
#2
Report 2 years ago
#2
Doesn’t it mean like “go back” to where the code branches and carry on without re-doing that branch? Ie “go back then skip this bit”
By branch i mean things you indent like for loops or while loops or functions etc
I think it can be used to end a loop when the value you need has been found.
Last edited by KVJ; 2 years ago
0
reply
mazembe11
Badges: 6
Rep:
?
#3
Report Thread starter 2 years ago
#3
Does return in Functions in the python programming language mean output or give back the result of something. Is one of these the synonym for return? If one of them is not the synonym then what is it?
0
reply
I'm God
Badges: 19
Rep:
?
#4
Report 2 years ago
#4
(Original post by mazembe11)
Does return in Functions in the python programming language mean output or give back the result of something. Is one of these the synonym for return? If one of them is not the synonym then what is it?
Well, in C++, when you return a function, you will get that output out of the function.

For example, if you use a function e.g. void sum(int a, int b);, and return a + b; , you will get the sum of the two integers.
EDIT: In main, you will have to call on the function e.g. sum(x, y) where x and y have been defined before.
This can then be used in the main function. Another example is, if you return 0 (usually what you do in main), the function will not return anything.

I think it should probably be the same in Python.

Hope it helped a bit
Last edited by I'm God; 2 years ago
0
reply
mazembe11
Badges: 6
Rep:
?
#5
Report Thread starter 2 years ago
#5
Does this mean that another word for return means output. For example returning a value is another way of saying outputting a value. Am I right? Yes or No.
0
reply
BFG9000
Badges: 14
Rep:
?
#6
Report 2 years ago
#6
What do you want it to do? Exit state, errors?
0
reply
winterscoming
Badges: 19
Rep:
?
#7
Report 2 years ago
#7
To understand what 'return' means, first take a step back to analyse the precisely what a function is and what your computer is really doing when you use a function.

In any of the 'C'-based languages (C, C++, C#, Python, Java, PHP, JavaScript and many others) a function could be described as a named block of code. However, in addition to being a named block of code, a function in those languages also has its own area of memory, called a frame, which resides in a memory space commonly known as "the stack". Every time a function begins in your program, a new frame is created on top of "the stack".

A block of code (whether named, or otherwise) defines a series of instructions - those instructions will be executed in sequential order - this happens by virtue of the fact that a computer program will always store a 'bookmark' to keep track of the next instruction to be executed. (the technical name for a 'bookmark' is 'pointer')

Programming languages use flow-control constructs (such as if, else, for, while, switch, break, continue or goto) to affect the next instruction pointer -- these constructs have the ability to modify the next-instruction pointer to cause the program to jump to a different line.

To illustrate with an example, the 'for' and 'while' construct can modify the flow of a program by 'pulling' the next-instruction pointer back to an earlier line at the end of its sequence, essentially causing the same block of code to repeat.

Those flow control keywords all have a simple limitation -- they only operate within the boundary or scope of a function. in other words, it is impossible for keywords like 'for' or 'while' or even 'goto' to jump outside of the confines of the function in which they are used.

As it happens, return is yet another flow-control keyword, except its capability is all about being able to jump outside of the current function, but only in a very limited fashion. Specifically, its unique capability is about 'ending' control of the current function and recalling the line of code which passed control of the next-instruction pointer to that function in the first place.

Actually, it operates rather like a breadcrumb trail - before a program passes control of the next-instruction pointer into a function, a hidden variable is placed onto the function's frame (i.e. that area of memory on the stack belonging to the function) which remembers the value of the next-instruction pointer before jumping into a new function).


Given that any line of code is able to start a function and pass control of the next-instruction pointer IN to that function at any time, it makes a lot of sense from the point of view of symmetry that a programming language should also have the ability to do the inverse -- i.e. a function should be able to end itself and pass control of the next-instruction pointer back OUT at any time too, then pick up wherever it left off.

This is what the return keyword is for, and indeed, the ability to end a function and pass back control of the next-instruction pointer is the primary capability of the return keyword. Obviously the only way for it to be able to pick up where it left off is to be able to go back to the previous function's frame, and dig out the previous function's next-instruction bookmark.

The return keyword also has a secondary, optional capability; given that a line of code which passes control IN to a function has the capability of providing inputs (often known as 'arguments'), it makes a lot of sense from the point of view of symmetry that a programming language should also have the ability to do the inverse -- i.e. a function should be able to provide outputs. There are actually multiple ways in which a function can do this, but the return keyword is one of several such ways of being able to do this.


To summarise:

- return will always end the current function and pass back control of the next instruction pointer back to the line of code which called its function. It achieves this by "popping" the function's frame off the top of "the stack" -- this terminates the current function and allows the program to go back to the previous function's frame, which, as I mentioned above, will have stored the previous function's next instruction pointer, allowing the program to essentially resume from where it left off (i.e., picking up the breadcrumb trail)

- return can optionally be used to attach some output at the same time as passing back control. This is a useful capability, but there are many cases where a programmer wouldn't care to use it (for example, a function whose sole purpose is just to write some data out to the screen probably wouldn't pass back any kind of output).


The closest synonym for 'return', while imperfect, would be 'respond', although that terminology belongs to a completely different programming paradigm, and the word 'respond' has a looser definition than 'return'.

Nonetheless, some programming paradigms use the concept of sending/receiving messages to model functions (In that paradigm, a function is a message pattern called Request-Response). Under a message-driven paradigm, programs are split into separate objects which communicate with each other by sending and receiving data between each other.

The Request-Response messaging pattern is simply where one object sends a message to another then is forced to "wait" for the other object to send back a response. This is semantically and logically the same thing that happens with functions in a C-style procedural program, however the underlying mechanics are completely different because there are no stacks or frames involved with messaging.

Request-Response is a bit like being put on hold on the telephone when you ask the operator at the other end for assistance, and they tell you that it will take them a few minutes to do their job before they can tell you the outcome, so you sit on the phone doing nothing but waiting. When the operator finally gets back to you, they are passing back a response to your request, then you finish the call and get on with doing something else.

So, one way to think of 'calling a function' and 'returning from a function' would be that it's the Procedural paradigm's way of doing Request-Response.
2
reply
mazembe11
Badges: 6
Rep:
?
#8
Report Thread starter 2 years ago
#8
Is another word for call a function use or name a function. Please do not use the word call when explaining the meaning of it. Another word for call is helpful.
0
reply
mazembe11
Badges: 6
Rep:
?
#9
Report Thread starter 2 years ago
#9
Is the word call a metaphor? Am I right Yes or no.
0
reply
winterscoming
Badges: 19
Rep:
?
#10
Report 2 years ago
#10
(Original post by mazembe11)
Is the word call a metaphor? Am I right Yes or no.
No, it isn't a metaphor, and neither are any of the other words you've been asking about either. As has been explained to you multiple times, these words are all standard, well-defined programming terms which describe specific programming language features.
0
reply
mazembe11
Badges: 6
Rep:
?
#11
Report Thread starter 2 years ago
#11
Is there a way of understanding the standard and well defined programming terms? As some of the programming terms are not on the oxford dictionary.
0
reply
winterscoming
Badges: 19
Rep:
?
#12
Report 2 years ago
#12
(Original post by mazembe11)
Is there a way of understanding the standard and well defined programming terms? As some of the programming terms are not on the oxford dictionary.
The only way really to understand them is to spend time learning to program. Any language would be fine really, but Python is a good language to start with, so I'd recommend spending time with Codecademy and work through these lessons carefully, making sure you complete all the lessons in-order and understand what's happening - you can go back and repeat the same lessons if you think you missed anything first time around:
https://www.codecademy.com/learn/learn-python-3
0
reply
mazembe11
Badges: 6
Rep:
?
#13
Report Thread starter 2 years ago
#13
Is the word return a metaphor? A function can't actually return a value. I think the function is giving the answer but normally you would say A function returns a value. Am I right yes or no.
0
reply
mazembe11
Badges: 6
Rep:
?
#14
Report Thread starter 2 years ago
#14
Are functions in programming similar to functions to mathematics? Am I right yes or no?
0
reply
winterscoming
Badges: 19
Rep:
?
#15
Report 2 years ago
#15
(Original post by mazembe11)
Are functions in programming similar to functions to mathematics? Am I right yes or no?
There are similarities but also differences. The term 'function' did originate in mathematics; functions in both programming and mathematics could be described as processes which have inputs and outputs.

That's where the similarities end though. Firstly, the 'process' portion of a function in a programming language is just a chunk of code (a series of one or more programming language instructions). Indeed, programming language functions are often described as 'a chunk of code which has a name'.

Secondly, for a programming language function, it's not actually necessary to have inputs or outputs -- meaning that it's possible for a function in a programming language to just be a simple named chunk of code without any inputs or outputs.

Lastly, functions in a programming language can have "side effects" becuase any chunk of code in a programming language can interact with the computer itself - including its memory, storage, connected devices, etc. A 'side effect' might be something like changing the content of the computer's memory, or writing data onto a storage device, or sending data over a network, or updating something on the display, etc.
Last edited by winterscoming; 2 years ago
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Feeling behind at school/college? What is the best thing your teachers could to help you catch up?

Extra compulsory independent learning activities (eg, homework tasks) (5)
3.73%
Run extra compulsory lessons or workshops (23)
17.16%
Focus on making the normal lesson time with them as high quality as possible (24)
17.91%
Focus on making the normal learning resources as high quality/accessible as possible (19)
14.18%
Provide extra optional activities, lessons and/or workshops (39)
29.1%
Assess students, decide who needs extra support and focus on these students (24)
17.91%

Watched Threads

View All