Question

Can a C function modify the value of its input arguments in the calling function. Explain...

Can a C function modify the value of its input arguments in the calling function. Explain your answer and write a small program to demonstrate that it is correct?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Yes. A 'C' function can modify the value of its input arguments when the arguments are passed to the function by 'call by reference' method, where, address of the argument is copied to the formal parameter. That is, argument pointers are passed to the function.

Since the address of the actual parameters is passed, values of these parameters will be modified on modifying the formal parameters. (formal parameters - pointing to the same address as the actual parameters).

Following code is an example of passing arguments by reference to a function. Here, address of actual arguments a and b are passed by reference to the swap function. Modifying the formal parameters (num1 and num2) in swap function gets reflected in the calling function as well. We can see that the values of a and b are swapped as well.

#include <stdio.h>

//swap the values of 2 variables - also reflected in the calling function variables

//passing arguments by reference
void swap(int *num1, int *num2) {

int temp;
temp = *num1; //the value at num1 is saved in temp
*num1 = *num2; //num2 value to num1
*num2 = temp; //copy temp value to num2
return;
}

int main () {


int a = 10;
int b = 20;

printf("Before swap, value of a : %d\n", a );
printf("Before swap, value of b : %d\n", b );

//&a - address of variable a (or pointer to a)
//&b - address of variable b (or pointer to b)

swap(&a, &b);

printf("value of a after swapping : %d\n", a );
printf("value of b after swapping : %d\n", b );

return 0;
}

Output

: Before swap, value of a 10 Before swap, value of b: 20 value of a after swapping : 20 value of b after swapping : 10

Add a comment
Know the answer?
Add Answer to:
Can a C function modify the value of its input arguments in the calling function. Explain...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PYTHON PROGRAMMING: DO NOT USE INPUT FUNCTION, use sys.argv (PLEASE TYPE OUT ANSWER) Modify this program:...

    PYTHON PROGRAMMING: DO NOT USE INPUT FUNCTION, use sys.argv (PLEASE TYPE OUT ANSWER) Modify this program: Write a program to calculate the bmi getcategory(): takes the bmi as a parameter and returns the bmi category. You are not allowed to change the two functions and you will use the same two functions as previously. Just modify the program so that it calculates the bmi and category for any number of pairs of height and weights. You must loop through the...

  • (C++) Write a function that accepts an int array and the array’s size as arguments. The function...

    (C++)Write a function that accepts an int array and the array’s size as arguments.The function should create a new array that is twice the size of the argument array.The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0.The function should return a pointer to the new array.Demonstrate the function by using it in a main program that reads an integer N (that is not more...

  • Write a function called productEven that takes as its parameter an input file. The function should...

    Write a function called productEven that takes as its parameter an input file. The function should read two integers and calculate the total product of only even numbers between them. Return the answer to the calling function. c++ program

  • Experiment with the arguments in the max() function in the program to determine if the function must have four arguments.

    1.   Experiment with the arguments in the max() function in the program to determine if the function must have four arguments. Provide an example for your answer.2. Write code that prompts the user for a floating point number and prints the smallest integer that is larger than the number the user entered.                           3.         Write the code to print a random number between one and six.     4.         Assume that a user enters any number and that the number is stored in the...

  • Create a function that takes in an integer array called “input” and size. The function should...

    Create a function that takes in an integer array called “input” and size. The function should return a pointer to a dynamically created array the has two locations. The first is the minimum number in the input array and the second is the maximum number in input. Demonstrate your work by calling this function in main. The program should be c++

  • please write program in C++ Write a function called productEven, that takes as its parameter an...

    please write program in C++ Write a function called productEven, that takes as its parameter an input file. The function should read two integers and calculate the total product of only even numbers between them. Return the answer to the calling function. please write the program in C++

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • a. Ask the user for the input of a letter (char type). Write a function that...

    a. Ask the user for the input of a letter (char type). Write a function that takes a char as the argument (parameter) and returns the ASCII value of that char back to the caller. Call the function using the char variable and taking input from the user (no validation required). Display the value in ASCII. b. Write a program that takes a char as the argument (parameter) and uses a loop to interrogate the char, calling a function that...

  • Command line input In C++ it is possible to accept command line arguments. Command-line arguments are...

    Command line input In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in the program, it must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line...

  • Question: In C++ Write a correct and complete C++ program th... Bookmark In C++ Write a...

    Question: In C++ Write a correct and complete C++ program th... Bookmark In C++ Write a correct and complete C++ program that inputs 20 integer values from the user and performs three calculations. In the main function, input the values from the user. As part of your answer, write a sub-function, named calcAvg (), which takes two arguments and returns the average of all the integers. Also write another sub-function, named reverseArray (), which takes two arguments and reverses the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT