Write a function called add. The function should be passed two parameters. I wil
ID: 3814409 • Letter: W
Question
Write a function called add. The function should be passed two parameters. I will leave it to you to decide the types of these parameters. The function's return type should be void, but the effect of the function should be to modify the value of the variable x in the call_add function below. You must write the add function, and fill in the call_add function below to make a function call to your add function. When inserted into the following source, the call_add function should print 3.
// write the function definition here for the add function
// this is the function that you should call from main.
void call_add() {
int x = 1, y = 2;
// call the add function here
printf ("%d ", x); // should print 3
}
int main() {
call_add();
}
// 2. now write add. It is up to you to define it so
// that it has the right signature (the right
// parameter types and return type) as specified
// in the assignment write-up
// 2. (continued) now complete the call_add function.
// It should call the add function,
// and eventually it prints 3.
void call_add() {
int x = 1, y = 2;
// write the appropriate function call to add
// the statement below should print 3
printf("%d ", x);
}
Explanation / Answer
#include <stdio.h>
void add(int *x, int *y){
*x = *x + *y;
}
void call_add() {
int x = 1, y = 2;
// call the add function here
add(&x,&y);
printf ("%d ", x); // should print 3
}
int main() {
call_add();
}
Output:
sh-4.2$ gcc -o main *.c
sh-4.2$ main
3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.