Passing parameter to function by value

Thread Starter

Parth786

Joined Jun 19, 2017
642
I wrote one program to understand basic of call by value in c programming. I am passing copy of parameter by value to function

C:
#include <stdio.h>

void function(int x);

int main(void)
{
  int a = 5;
 
  printf("value of a in main function before calling: %d\n", a);
 
  function(a);
 
  printf("value of a in main function after calling: %d\n", a);

  return 0;
}

void function(int x)
{
  printf("value of x in function : %d\n", x);
  x = 10;
  printf("value of x in function : %d\n", x);
}
value of a in main function before calling: 5
value of x in function : 5
value of x in function : 10
value of a in main function after calling: 5

If you can see the value of variable doesn't change in main function before calling and so when we call function from main function the value will be same a = 5, After calling the function the value of variable doesn't change it will still same a = 5

Does it meaningful program, can it be write in better way ?
 

spinnaker

Joined Oct 29, 2009
7,830
When you pass by value, the compiler places the value on the stack. That value is then retrieved from the stack and used in the function. So it is a copy that is being used. Changing the copy does not change the original value.

You need to pass by pointer in C.

Code:
void  foo(int *x)
{
   *x=10;

}


void main()
{
      int y = 0;

     // foo converts the value of y to 10  0
      printf("x= %d",foo(&y);

}
C++ allows pass by reference operator but that is a whole other story
 

spinnaker

Joined Oct 29, 2009
7,830
P.S. If you use pass by value or pointer or whatever the stack is still used to pass the "values". What is pushed depends on the method used.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Call by address
C:
#include <stdio.h>

void function(int *x);

int main(void)
{
  int a = 5;

  printf("value of a in main function before calling: %d\n", a);

  function(&a);

  printf("value of a in main function after calling: %d\n", a);

  return 0;
}

void function(int *x)
{
  printf("value of x in function : %d\n", *x);
  *x = 10;
  printf("value of x in function: %d\n", *x);
}
value of a in main function before calling: 5
value of x in function : 5
value of x in function: 10
value of a in main function after calling: 10

so value of a in main function before calling is 5 and value of a in main function after calling is 10

Does it suitable example to understand about the basic of call by address ?
 

spinnaker

Joined Oct 29, 2009
7,830
Probably a bad idea to call your function name function. If in an example situation it could be confusing. foo is usually used for an example function name.

And yes you have the basics of pointers.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Probably a bad idea to call your function name function. If in an example situation it could be confusing. foo is usually used for an example function name.

And yes you have the basics of pointers.
Does this program suitable for function with argument and return type ?

whenever we call function it give return of addition
C:
#include<stdio.h>

int add(int num1, int num2);

int main (void)
{
  int addValue;
  int value1 = 40;
  int value2 = 22;

  addValue = add(value1, value2);

  printf("%d\n",addValue);
  
     return 0;

}

int add(int num1, int num2)
{
    return num1 + num2;
}
compiler output = 62
 

WBahn

Joined Mar 31, 2012
32,836
I wrote one program to understand basic of call by value in c programming. I am passing copy of parameter by value to function

value of a in main function before calling: 5
value of x in function : 5
value of x in function : 10
value of a in main function after calling: 5

If you can see the value of variable doesn't change in main function before calling and so when we call function from main function the value will be same a = 5, After calling the function the value of variable doesn't change it will still same a = 5

Does it meaningful program, can it be write in better way ?
What do you mean by "meaningful program"?

Whether it can be written in a "better way" depends on what it is you are trying to accomplish with the program.

If all you are trying to accomplish is to show that when a variable is passed by value to a function that the function can't make any changes to the value of the variable in the caller, then this program demonstrates this just fine.
 
Top