Functions and their return....

  • Thread starter fog37
  • Start date
  • Tags
    Functions
In summary, the return statement in C serves to indicate the output of a function. It can be used to return a single value or to modify a variable passed in as a pointer or reference. In C, all functions are declared as functions, even if they do not return a value. However, the void keyword can be used to specify that the function does not return a value. The return statement can only be used within a function and is used to terminate the function and return a value to the calling function. It is important to specify the return type of the function in order to ensure that the correct type of value is returned.
  • #1
fog37
1,568
108
Hello Forum,

I am trying to get clear on the return statement when defining functions in C. A function is a group of statements that together perform a certain task. A function usually receives some input arguments which it uses to produce some output arguments. In C, we must specify what type of data the output is going to be. Some functions can possibly have no input arguments and no output arguments. For example, void main(void) receives and outputs nothing...I guess it simply initiates the code that it contains which could possibly produce inputs and outputs...

In general, does the return statement represent/indicate the output of the function?

Thanks,
fog37
 
Technology news on Phys.org
  • #2
fog37 said:
In general, does the return statement represent/indicate the output of the function?

At the assembler level, there is a specific register that you write the return value to. If the function doesn't return anything, you just don't write to the register.

I wouldn't consider the value written to this register to be the exclusive output of the function. You may also write to other memory addresses or registers as well inside the function.
 
  • #3
What is one of the possible reasons to return something to a specific register in the CPU?
 
  • #4
The returned value is placed in the position of the function call and can be easily used. You don't need to save it into a declared variable.
Two common uses of the return from a function is:
1) If there is only one value to get from the function, the return allows you to use it in equations. In x = sin(a) + cos(b), sin and cos have return values.
2) If the success or failure status of the function operation is in question, you would often like to return the status.
Code:
if( ! set_up_shared_memory() ){
   printf("ERROR: Shared memory not set up\n");
}
 
Last edited:
  • #5
fog37 said:
What is one of the possible reasons to return something to a specific register in the CPU?

C code is converted into assembly language in the compilation process. There you don't really have the notion of functions quite like you have in C. It's more like just writing values to registers and memory addresses and using goto like statements to jump around in the code.

For the return value, it's a convention that it's stored in a specific register. This is done simply so that assembler coders or C or C++ compilers agree on where the value returned from a C function should be put. This way, the calling code can look in the register to retrieve the value after calling the function. So you basically can rely on the fact that when a C or C++ function returns something, what it returns will be in a specific place.

Registers are also used for various things by the operating system for example, and some code may write to those registers behind the scenes when you do things like print, draw, get user input etc. You may also simply pass pointers ( memory addresses ) as arguments to the function and then write values to their corresponding cells of memory. I would consider all of these things output I guess. I suppose at the fundamental level I would consider the output to be the set of changes to the state of the machine resulting from calling the function.
 
Last edited:
  • #6
fog37 said:
I am trying to get clear on the return statement when defining functions in C. A function is a group of statements that together perform a certain task.
Languages other than C and C++ distinguish between functions (that return a value) and procedures or subroutines (that perform some task). In C and C++ there are just functions, though. A function that is declared and defined as a void function does not return a value. A function that is declared and defined as int fun() or char fun() or float fun() etc. returns a single int, char, or float, respectively.

A void function is the C/C++ equivalent of a subroutine (Fortran designation) or procedure (Pascal designation).
fog37 said:
A function usually receives some input arguments which it uses to produce some output arguments. In C, we must specify what type of data the output is going to be. Some functions can possibly have no input arguments and no output arguments. For example, void main(void) receives and outputs nothing...I guess it simply initiates the code that it contains which could possibly produce inputs and outputs...

In general, does the return statement represent/indicate the output of the function?
Yes, absolutely.

Here's an example definition:
Code:
int sum(int a, int b)
{
   return a + b;
}
The function above takes the int values passed as parameters, adds them, and then returns that sum, as an int. It would be used like so:
C:
.
.
result = sum(4, 6);
.
.
 
Last edited:
  • #7
Mark44 said:
Languages other than C and C++ distinguish between functions (that return a value) and procedures or subroutines (that perform a value).

I think at the end of this sentence you meant to write something like "(that perform an operation)".
 
  • #8
I'll add to Mark's example that you can also do it as follows, using a void function that has a third argument for passing the result back to the calling function via a pointer:
C:
void add (int a, int b, int *c)
{
  *c = a + b;
}

You would call it like this:
C:
.
.
add (4, 6, *result);
.
.

You can call Mark's function twice in the same statement if you like:
C:
result = sum(4, 6) + sum(3, 5);

but you can't do this with a void function, so you have to use a more cumbersome process to get the same result:
C:
add (4, 6, *term1);
add (3, 5, *term2);
result = term1 + term2;

In C++ most programmers would use a reference instead of a pointer argument:
Code:
void add (int a, int b, int &c)
{
  c = a + b;
}
Code:
.
.
add (4, 6, result);
.
.

(C doesn't have references.)
 
Last edited:
  • #9
Hello,
After reading your useful replies and some other resources, I believe I understand that the return statement works this way:
A certain function A (it could be the main function or any other function) "calls" another function B. When function B is called and passed certain parameters to work with, the code inside the curly brackets of function B is executed. Function B can manipulate the passed parameters, do calculations with them, etc. Function B can take the result from one of its executed instructions/operations and return it (pass it) back to the calling function to be used by it, if necessary. Some functions do no return any value to the calling function. In that case we can simply write return or write nothing. Those functions execute their code, can produce some outputs which do not get necessarily passed back to the calling function to be used by other functions nested inside the calling function...

For example, let me write the C program example below:

#include <stdio.h>
long int multiply( int var1, int var2) /* this line represents prototyping the function
main ()
{
int a, b;
long int answer;
printf("What is the first number to multiply?");
scanf(%d, &a);
printf("What is the second number to multiply?");
scanf(%d, &b);

answer=multiply(a,b)
printf("the product equals %ld.", answer);
return 0;
}
**************************
long int multiply(int var1, int var2)
{
int locAnswer;
locAnswer= var1 * var2;
return (locAnswer)
}

In the simple code above, the function multiply() is called by the main() function. The function multiply() is passed the parameters a and b and produces the output variable answer used inside the main() function. For the output variable answer to be used by main(), multiply() must first return it to the main(). The expression given to the return statement in multiply() is the local variable locAnswer whose name can be different from the output variable name, answer in this case, used in the main() function...

thanks,
fog37
 
  • #10
Mark44 said:
A function that is declared and defined as a void function does not return a value.
That's not how I would teach it. It does return a value, it's of type void. Void is a type just like int, float, string... you can even cast to it.

Code:
(void)0;
Is commonly used to represent a no-operation, it takes the value 0 and casts it as void.
 
  • #11
Mark44 said:
A function that is declared and defined as a void function does not return a value.
newjerseyrunner said:
That's not how I would teach it. It does return a value, it's of type void. Void is a type just like int, float, string... you can even cast to it.
I disagree. A void function cannot return a value. My compiler (VS 2013) flags the following as an error:
C:
void nop()
{
   int retvalue = 7;
   return retvalue;
}
Of course, if I change the last line above to return (void)retvalue; the compiler accepts this change, but why would I want to do this? As far as I can tell, the compiler optimizes out the revised return statement. In any case, the code that is generated does not alter the value in the EAX register, the register typically used in x86 code to hold return values.

A few quotes from my copy of K & R, 2nd Ed.
Bold emphasis added
P. 30
Some functions return a useful value: others, like copy (Mark44: defined earlier), are used only for their effect and return no value. The return type of copy is void, which states explicitly that no value is returned.
P. 196
The void type specifies an empty set of values. It is used as the type returned by functions that generate no value.
P. 199
A6.6 Void
The (nonexistent) value of a void object may not be used in any way, and neither explicit nor implicit conversion to any non-void type may be applied. Because a void expression denotes a nonexistent value, such an expression my be used only where the value is not required, for example as an expression statement or as the left operand of a comma operator.
An expression my be convered to type void by a cast. For example a void cast documents the discarding of the value of a function call used as an expression statement.
newjerseyrunner said:
Code:
(void)0;
Is commonly used to represent a no-operation, it takes the value 0 and casts it as void.

I've never seen any code like the above, which doesn't mean that it hasn't been used somewhere, but I have seen this quite often:
C:
ptr = (void *)0;
 
  • Like
Likes Jaeusm
  • #12
From N1570 C11 standards paragraph 6.2.5 page 41:
19 The void type comprises an empty set of values; it is an incomplete object type that cannot be completed.

6.3.2.1 Lvalues, arrays, and function designators
1 An lvalue is an expression (with an object type other than void) that potentially designates an object;

6.3.2.2 void
1 The (nonexistent) value of a void expression (an expression that has type void) shall not be used in any way, and implicit or explicit conversions (except to void) shall not be applied to such an expression. If an expression of any other type is evaluated as a void expression, its value or designator is discarded. (A void expression is evaluated for its side effects.)

Section 6.3.2.2 refers to what Mark44 is discussing. Not the void pointer. The side effects comment explains what happens to void functions and expressions. They act like subroutines in other languages do, modifying variables that are in scope for the function, for instance.
 
  • #13
Mark44 said:
I disagree. A void function cannot return a value. My compiler (VS 2013) flags the following as an error:
C:
void nop()
{
   int retvalue = 7;
   return retvalue;
}
That's simply because it's returning the wrong type, once you cast it no void you said it worked fine.

Mark44 said:
I've never seen any code like the above, which doesn't mean that it hasn't been used somewhere, but I have seen this quite often:
It's commonly used when you want different behavior depending on compilation options, here is an example:

Code:
#ifdef USE_LESS_MEMORY
     #define process_cstring(cstr) cstr = realloc(cstr, strlen(cstr) * sizeof(char))
#else
     #define process_cstring(cstr) (void)0
#endif
Most compilers will optimize an empty function away (some even support the inline keyword,) but strict C standard compilers won't so a no-op is used instead. I use it mostly on code that needs to work on embedded hardware with weird compilers. I don't think this process is used much anymore in normal coding, just special cases, but it's very common in legacy code.
 
  • #14
newjerseyrunner said:
That's not how I would teach it. It does return a value, it's of type void. Void is a type just like int, float, string... you can even cast to it.

Code:
(void)0;
Is commonly used to represent a no-operation, it takes the value 0 and casts it as void.
This is not doing what you think it is doing. It does very similar to the use of printf by pedantically correct programmers. printf returns a value, but hardly anyone uses it. To be pedantically correct, you should tell the compiler you are ignoring that return value:
Code:
(void)printf("%s %s\n", "hello", "world");
Now we can see what your code is doing. Reading from right to left, the semicolon terminates the statement, the zero is an expression (but then again, so is a call to printf), and void tells the compiler to not complain that that zero is being dropped onto the bit floor. Any decent compiler will optimize that away.
 
  • #15
jtbell said:
I think at the end of this sentence you meant to write something like "(that perform an operation)".
Yes. I changed my post accordingly.
 
  • #16
fog37 said:
Hello Forum,

I am trying to get clear on the return statement when defining functions in C. A function is a group of statements that together perform a certain task. A function usually receives some input arguments which it uses to produce some output arguments. In C, we must specify what type of data the output is going to be. Some functions can possibly have no input arguments and no output arguments. For example, void main(void) receives and outputs nothing...I guess it simply initiates the code that it contains which could possibly produce inputs and outputs...

In general, does the return statement represent/indicate the output of the function?

Thanks,
fog37
In mathematics, a function is something that pairs an input with an output. Given the input is the same, the output will always be the same.

In C, a "function" is a call of a routine that may or may not return a value and may return a different value given the same input.

As a programmer I tried to use honest-to-God mathematical functions. In programming this is thought of as "a subroutine with no side effects." If it has no side effects, then its only use is to return a value. This makes the program easier to managed. If everything in your program is a function then it has only one state, so it can never be in the wrong state. This eliminates that which is possibly the biggest source of error.

This can't be done if you are dealing with hardware that doesn't following the functional paradigm, or if you are dealing with a database.
 

1. What is a function?

A function is a block of code that can be called and executed multiple times to perform a specific task. It can take in inputs, known as parameters, and return an output based on the code inside the function.

2. What is the purpose of using functions?

Functions are used to organize code, make it more efficient, and reduce repetition. They can also make code more readable and easier to maintain by breaking it down into smaller, modular chunks.

3. What is the difference between a function and a method?

A function is a standalone block of code, while a method is a function that belongs to an object or class. Methods are usually used to manipulate the data within an object, while functions can operate on any data.

4. What is a return statement in a function?

A return statement is used to specify the value or result that a function should return when it is called. It is the final statement of a function and can be used to pass information back to the code that called the function.

5. Can a function have multiple return statements?

Yes, a function can have multiple return statements. However, only one return statement will be executed when the function is called. The return statement that is executed will depend on the conditions specified in the function.

Similar threads

  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
4
Views
736
  • Programming and Computer Science
Replies
2
Views
686
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
6
Views
921
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
2
Views
769
Back
Top