Functions in Python ProgrammingWatch
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.
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?
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
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.
- 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.
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.
Are functions in programming similar to functions to mathematics? Am I right yes or no?
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.