Home > Software engineering >  If function updates a variable is it considered as a function that returns value?
If function updates a variable is it considered as a function that returns value?

Time:07-29

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?

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

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.

CodePudding user response:

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.

  •  Tags:  
  • c
  • Related