-1

I'm new to programming. And I'm a bit confused about what is considered returning a value in programming.

From what I seen printf is considered as a function that takes in arguments but doesn't return value.

For example if I use code like this

 int a=10;
 printf ("a-b is,%i,%i",a-b,a++);

printf performs a calculation and return a value of a-b that wasn't there before in my computer or code but now exists, is that value returning even if I can't set variable equal to it? Is something value returning only if you can set variable equal to it if so why? Or is it value returning if it gives back something or rather returns something in main and main part of code after its done executing?

But even so it returns a++ back in main within function body so it creates and gives back new value to main within its own code. Isn't that also value returning it returned new value back to main part of code?

I have really weak understanding of what is value returning exactly, so it would be really helpful if someone could clarify. It seems to be one of those main concepts in programming?

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • 3
    Your example [involes *undefined behavior*](https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior). – MikeCAT Jul 28 '22 at 10:58
  • 3
    [`printf()`](https://man7.org/linux/man-pages/man3/printf.3.html) returns a value (the number of characters printed). – MikeCAT Jul 28 '22 at 11:00
  • @MikeCAT Wait, is that really undefined behavior? Isn't `a - b` evaluated first, followed by `a++` (comma being a sequence operator), and those values passed to `printf()`? – AKX Jul 28 '22 at 11:01
  • 3
    @AKX This case is covered by [this answer](https://stackoverflow.com/a/34536741/4062354). Commas for separating function arguments is not sequence operators. – MikeCAT Jul 28 '22 at 11:02
  • @MikeCAT Okiedoke, thanks for the link! – AKX Jul 28 '22 at 11:02
  • It is an important computing concept but I will say not really the purpose of Stack Overflow. In summary there are various meanings of Function and Procedure between and across Mathematics and Computing. In C something with side-effects that 'returns' void is a function. Though arguably that's a Procedure by some people's definition. I'd say broadly if it has side-effects it's a procedure, if it returns a value it's a function if it does neither it's the "none operation". But C doesn't have procedures. It's functions all the way down. Some of which don't return a value. – Persixty Jul 28 '22 at 11:06
  • @MikeCAT nm. I get it comma operators are not comma separators. Derp. sry. – WhozCraig Jul 28 '22 at 11:11
  • The function does not "return a-b". It passes that value to `printf` which outputs it. It then returns the number of characters output. In C `return` has a specific meaning and it is not uncommon for beginners to misuse it, such as your phrase "is that value returning". The value `a-b` is only "returned" in a statement like `return a-b;` where the `a-b` is *evaluated* and returned as the value of a function. – Weather Vane Jul 28 '22 at 11:19
  • @WeatherVane "The value a-b is only "returned" in a statement like return a-b; where the a-b is evaluated and returned as the value of a function." So even if i put in some new function body a=1,b=2,c=a+b return c but beforehand in functions body do something like add two other ints like int d=1; int f=2 and add them in new int called df=d+f within same function, only return value is int c still because i only said return c? But how come i mean couldn't i then later on set new variable like int dfg=df in main part of program. So how come it didnt return df too as a value? – 20centuryboy Jul 28 '22 at 11:46
  • Because it wasn't asked to return df. It was a local variable that is discarded at the end of the function. The `df` no longer exists. – Weather Vane Jul 28 '22 at 11:49

5 Answers5

0

If your function has a return statement, it is considered as a function that returns a value. To read a function's return statement you must use a function such as printf. Also, if a program returns 0, it means the code execution was sucessful.

SvGaming
  • 23
  • 6
  • 1
    `void foo(int a) { if (0 <= a) { printf("a = %d\n", a); return; } printf("Invalid.\n"); }` has a `return` statement but is not considered a function that returns a value. – Eric Postpischil Jul 28 '22 at 11:43
0
printf ("a-b is,%i,%i",a-b,a++);

a-b is done outside the printf function code before the function is called and parameters passed. Post increment happens after it is passed to the function. And it is not the return value.

Because the order of parameters evaluation is not determined it is undefined what will happen first post-increment or subtraction. This example invokes Undefined Behavior (UB)

The return value is something which is returned by the function return statement.

int mul(int a, int b)
{
    return a * b;  // <-- return value
}

Functions can modify outside objects if the parameter is a pointer.

int incAndSquare(int *x)
{
    (*x)++;   //integer referenced by x is being incremented.
    return *x * *x;  // return value
}

int main(void)
{
    int y = 5;
    printf("%d\n", incAndSquare(&y));
    printf("%d\n", y);
}

https://godbolt.org/z/3hjv96ern

0___________
  • 60,014
  • 4
  • 34
  • 74
  • Thanks for answering.But how come a-b being done in argument part outside of the place where printf is defined makes it not part of function. You said its because its done before function is called but how come i need to first call function and then within functions parenthesis type a-b not anywhere else before i called it? And part about undefined behavior isnt true on ide i tried this program was working maybe its ide thing? – 20centuryboy Jul 28 '22 at 11:28
  • @20centuryboy *"And part about undefined behavior isnt true"* I think you should not argue assuming your programming experience – 0___________ Jul 28 '22 at 13:44
  • @ 0___________ you seem to have misunderstood me i wasn't arguing i just said this code worked when i tried it on online ide maybe that ide allows something yours doesn't that all. And it doesn't matter to me to argue with anyone im more so interested in reason why it isnt argument – 20centuryboy Jul 29 '22 at 11:36
0

Functions in C need to have their return type defined when they are declared

int mul(int a, int b);

void print_me(char *str);

Void functions will not return any values, while other types will return a value with the type defined. But just because they return a value doesn't mean you need to "catch" it.

What you said about printf is incorrect, it does return a value (the length of the string printed). But most of the time that is irrelevant to us so we don't save that value.

return statement is used when returning a value from a function, but in void functions it can also be used to stop the function without returning any values.

Randommm
  • 410
  • 1
  • 9
0

In C, functions may be used as values in expressions. For example:

//  Return the square of x.
double square(double x)
{
    return x*x;
}

#include <stdio.h>

int main(void)
{
    printf("3 + 4**2 = %g.\n", 3. + square(4.));
}

prints “3 + 4**2 = 19.” This is called returning a value. The function contains a return statement of the form return expression;. When the return statement is executed and the calling function has called the function in an expression, the value given in the return statement is used as the value of the function in the expression.

Sometimes, you may see other methods of providing information to a calling routine called “returning a value.” For example:

//  Return the square of x in an object provided by the caller.
void square(double *y, double x)
{
    *y = x*x;
}

#include <stdio.h>

int main(void)
{
    double temporary;
    square(&temporary, 4.);
    printf("3 + 4**2 = %g.\n", 3. + temporary);
}

In this code, the main routine passes the address of its temporary object to the routine square. The routine square accepts this address as the parameter y, which is a pointer to a double. Using *y = , it puts a value in the place that the pointer y points to. This causes the value to be put into temporary, because main passed the address of temporary. After the routine square returns, the main routine uses the new value in temporary. This method does not use the return-value mechanism built into the programming language, but people may still call it returning a value since that is the purpose it is serving.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Thanks for answering.But doesn't then (going by second examples logic) printf also return value? When it increments a with a ++ it then can be used as a part of expression with its new value. So it returns a value of a+1 in a way? Also is there ever a reason to use second example of passing a value instead of first,is purpose of second only to demonstrate a point? First seems more simple and logical in all ceases that come to mind? – 20centuryboy Jul 30 '22 at 08:59
0

For starters this call of prontf

 int a=10;
 printf ("a-b is,%i,%i",a-b,a++);

invokes undefined behavior between evaluation the value of the variable a in this expression a-b and evaluation of the post-increment operator a++.

As for the function printf itself then it is declared the following way

int printf(const char * restrict format, ...);

As you can see its return type is not void but int. And according to its description (the C Standard, 7.21.6.3 The printf function)

3 The printf function returns the number of characters transmitted, or a negative value if an output or encoding error occurred.

So if a function has a return type other than void it means that is returns a value of the specified return type of the function.

That is according to the C Standard (6.8.6.4 The return statement)

1 A return statement with an expression shall not appear in a function whose return type is void. A return statement without an expression shall only appear in a function whose return type is void.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335