Simple calculation inside a function - c

I'm trying to do the calculation inside a function, but i'm not sure why it's not working:
int calculate(int x){
x = x + 2;
return x;
}
int main(){
int x = 0;
int i;
for(i=0;i<10;i++){
calculate(x);
}
printf("i = %d, x = %d\n", i, x);
}
I understand that x is 0 every time it passes through the function. But how do I fix it?
Supposedly i should return 10, and x should return 20.

You can actually pass the pointer of that integer you want to change, not the value itself. In that case, the new (increased) integer will be stored in the original level of scope (actually at the exact same memory spot), where it was defined, which is in this case is your main function. So your code, should look like this:
void calculate(int *x)
{
*x += 2;
}
int main(void)
{
int x = 0;
for (int i=0; i<10; i++)
{
calculate(&x);
printf("i=%d, x=%d\n", i, x);
}
return 0;
}
OUTPUT:
i=0, x=2
i=1, x=4
i=2, x=6
i=3, x=8
i=4, x=10
i=5, x=12
i=6, x=14
i=7, x=16
i=8, x=18
i=9, x=20

Variables can shadow each other. You don't have to ensure that you never, ever use i anywhere else in fear of messing with the i in your for loop, because a new scope will get a new copy of the same name (like when two different people have the same name).
To fix this, you can return the value from your calculate function. I named it x2 to clearly differentiate it from your original x:
int calculate(int x2){
x2 = x2 + 2;
return x2;
}
int main(){
int x = 0;
int i;
for(i=0;i<10;i++){
x = calculate(x);
}
printf("i = %d, x = %d\n", i, x);
}

If you want x to change, you need to pass it by reference, not by value.
void calculate(int *x){
*x = *x + 2;
}
int main(){
int x = 0;
int i;
for(i=0;i<10;i++){
calculate(&x);
}
printf("i = %d, x = %d\n", i, x);
}

You're passing x by value, so calculate only changes a local copy. When calculate returns, the result is lost. You need to return the modified value from calculate and assign it to something in main.

You're passing in x as a value (ie. it is copied). So x inside the calculate function is not the same as x outside of it. Thus, when you change its value, the change is not reflected in the x that is in main.
The following would be preferable. Note that you need to return a value from the calculate function, and then assign what it returns to some value.
int calculate(int x){
return x + 2; /* CHANGED */
}
int main(){
int x = 0;
int i;
for(i=0;i<10;i++){
x = calculate(x); /* CHANGED */
}
printf("i = %d, x = %d\n", i, x);
}

Change:
for(i=0;i<10;i++){
calculate(x);
}
to:
for(i=0;i<10;i++){
x = calculate(x);
}
Your function returns a value, thus you need to store it somewhere.

In the function you can just change one thing and iguess it will work
Int calculate(int &x)
And keep rest other things same
Basically u can use "alias"

Related

Not able to understand how a function is returning a specific value when there is no return statement but there is an int return type in C language

This recursive function 'int f(int n)' should not return anything except when 'n == 1'. If we put
n = 2,3 then y = 2. When n = 4,5 then y = 3, when n = 6,7,8 then **y = 4 **and so on. Here, variable x is static. Below is the code:
#include <stdio.h>
int f(int n){
static int x=1;
int i;
if(n==1){
return 1;
}
else {
for(i = 1; i<=2; i++){
x = x + f(n-1);
printf("%d\n",x);
}
}
}
int main() {
int n = 3;
int y =f(n);
printf("y %d",y);
}
Kindly help me understand how the value of y is assigned or how f() is returning such values.
Thank you.
I was expecting 2,3,4,5,6,7. I am able to get those values implementing some other stuff but that
is not what I want to understand. I want to understand the f() is 2, when n is 2,3. How f() is returning a value without the return statement?
The function f doesn't return a value on all code paths. This means undefined behavior. It may return anything, or y may remain uninitialized.

How to concatenate 2 numbers using bit-wise operator, leaving initial values of variables intact?

I can not use sprintf, or any other function that puts everything together in a string n, I can not really use any libc function, it's part of a challenge I'm trying to solve
Given:
int x=5;
int y=2;
Expected Output:
res = 52;
This is one posible solution:
#include <stdio.h>
int main()
{
int x= 342;
int y= 224;
int aux = y;
while( aux ) {
aux /= 10;
x*= 10;
}
x+= y;
printf("x= %d\r\n", x); // prints 342224
}

How to modify variables passed by value instead of by reference within function in C?

How to modify variables passed by value instead of by reference within function in C?
void fun_a(int a, int b) {
....
}
int main() {
int x = 5, y = 6;
printf("%d, %d", x, y); // 5, 6
fun_a(x, y); // pass by value
printf("%d, %d", x, y); // 1, 2
return 0;
}
How to use fun_a to change variables?
Is there any way to do it? Though I know it can change variables by passing references easily.
C does not know about reference. Use pointers instead.
void fun_a(int *a, int *b) {
*a = 10;
*b = 12;
}
int main() {
int x = 5, y = 6;
printf("%d, %d\n", x, y); // 5, 6
fun_a(&x, &y); // pass pointer to variable
printf("%d, %d\n", x, y); // 10, 12
return 0;
}
Simple answer : you can't.
To achieve your goal, you have to pass by pointer.

Conflicting types error in compiler

My issue is that I keep getting the same type of error and I can not understand why. I'm fairly sure I declared and defined it before the main function.
Here is my code.
#include <stdio.h>
void functn (int x);
int functn(int x, int result){
result = (x-1)+2;
if (x <= 0){
return 0;
}
else{
return result;
}
}
int main (){
int x, y;
printf ("Enter the value of x: ");
scanf ("%d", &x);
y = f(x);
printf ("%d", y);
return 0;
}
You have a number of problems in functn. Primarily passing result. When you pass result to functn, functn receives a copy of result and the only way to get the modified value is to return the modified value. You can also pass a pointer to result and update *result in functn (similar to what you do now), that would eliminate the need to return a value, as any changes to result would be visible back in the calling function (main() here). Further, there is no need for global variables. Simply declare the variables local to main and pass as parameters, as required.
The following example declares functn (simply f below) to do both, take a pointer to result (e.g. &result) as a parameter, while also returning result, which allows you to either assign the return or not, but always have the updated value for result back in main, e.g.
#include <stdio.h>
int f (int x, int *result);
int main (void){
int x, y, result = 0;
printf ("Enter the value of x: ");
if (scanf ("%d", &x) != 1) {
fprintf (stderr, "error scanf, invalid conversion.\n");
return 1;
}
y = f (x, &result);
printf ("y = f(%d) => %d\n", x, y);
return 0;
}
int f (int x, int *result)
{
*result = (x-1)+2;
if (x <= 0)
return 0;
return *result;
}
Example Use/Output
$ ./bin/yfx
Enter the value of x: 5
y = f(5) => 6
Look things over and let me know if you have questions.
Return type of functn() different in declaration and definition. So, use
int functn(int x);
instead of
int functn(int x, int result)
Remove int result from function argument and declare inside function. like,
int functn(int x)
{
int result = 0;
result = (x-1)+2;
if (x <= 0){
return 0;
}
else{
return result;
}
}
Also, correct function call, like
y = functn(x);
instead of
y = f(x);

Both call by value and call by reference work?

I thought that calling function by value will never work, and I should always use call by reference, but trying this code...
// call by value
#include<stdio.h>
int Add(int a, int b)
{
int c = a + b ;
return c ;
}
int main()
{
int x = 2 , y = 4 ;
int z = Add(x,y);
printf("%d\n",z);
}
output will be: 6
it works fine in both ways (call by value & call by reference),
// call by reference
#include<stdio.h>
int Add(int* a, int* b)
{
int c = *a + *b ;
return c ;
}
int main()
{
int x = 2 , y = 4 ;
int z = Add(&x,&y);
printf("%d\n",z);
}
output will be: 6
not like the famous swap function example - when calling by value it doesn't swap -
// call by value
#include <stdio.h>
void swap(int a, int b)
{
int temp;
temp = b;
b = a;
a = temp;
}
int main()
{
int x = 1 , y = 2;
printf("x = %d , y = %d\n", x,y);
swap(x, y);
printf("after swapping\n");
printf("x = %d , y = %d\n", x,y);
return 0;
}
.. it only worked calling by reference
// call by reference
#include <stdio.h>
void swap(int *a, int *b)
{
int temp;
temp = *b;
*b = *a;
*a = temp;
}
int main()
{
int x = 1 , y = 2;
printf("x = %d , y = %d\n", x,y);
swap(&x, &y);
printf("after swapping\n");
printf("x = %d , y = %d\n", x,y);
return 0;
}
So How can I judge if "calling by value" going to work or not ?!
So How can I judge that call by value method is valid or not ?!
Well, it depends on what your function is about to do.
In your above example, you only need the values of (x,y) for computing, but you never plan to change their value during your function. While call-by-reference will work in this case, it is unneccessary.
In the other (indirectly given) example you obviously want to change two variable's content (that is - swap it). You can access these variables from the main-function in your Swap-function, but how can you make the change persistent? That's only possible by call-by-reference, because you have to write the changed content into a variable that survives the function.
The following will not work:
// call by value
#include<stdio.h>
void Swap(int a, int b)
{
int c = a;
a = b;
b = c;
// from here on a, b, c will be destroyed
// therefore the change cannot be seen outside the function
}
int main()
{
int x = 2 , y = 4 ;
Swap(x,y);
printf("x: %d --- y: %d\n",x,y);
}
So as a rule to keep in mind:
If you want to make a change that's supposed to survive the function's end, use call-by-reference. If you just work with some data but do not want to (or must not) change their value, use call-by-value.

Resources