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?
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
Can a C function modify the value of its input arguments in the calling function. Explain...
(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 read two integers and calculate the total product of only even numbers between them. Return the answer to the calling function. c++ program
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...
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...
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++
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...
9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in the main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file named...
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...
(a) Write a Ruby function mean that computes the mean value of an arbitrary number of arguments. Function calls mean (1,2,3,4,5) mean 3 ,1 ,and "No arguments". (1) and mean should respectively return tion sigma that uses your function mean and computes the standard deviation of an arbitrary of arguments. Function calls sigma (1,2,1,2)sigma (1) and sigma should respectively return1, o, and "No arguments" (Hint: the standard deviation is the square root of (b) Write a Ruby func variance, and...
Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...