Comprehending some C code (simple function calls)

In summary, the conversation discusses a piece of code and questions about how it works. The main function calls the FindSum function twice, with different parameters, and the FindSum function modifies the values of the parameters and outputs them. The conversation also includes questions about the values of variables and how they are modified in the code.
  • #1
Arnoldjavs3
191
3

Homework Statement


Hi, I have a piece of code that I'm trying to comprehend but I don't understand some aspects. Here is that code:

Code:
#include <stdio.h> 

/* function prototype declaration for FindSum */

void FindSum(int, int, int *);

int main(void)
{

int a=2, b=5, c=1, x=3, y=4, z=7; 

/* body of main */

 FindSum (a, b, &c); /* a first call to FindSum */ 
 printf(“first call in main %d %d %d %d %d %d \n”, a, b, c, x, y, z); 
 FindSum (x, y, &z); /* a second call to FindSum */
 printf(“second call in main %d %d %d %d %d %d \n”, a, b, c, x, y, z); 

return 0;

}/* definition of FindSum */

void FindSum (int a, int b, int *c)
{

 a += (b * 2);
 b += (b * 2);
*c += (b * 2);

 printf(“in FindSum: %d %d %d \n”, a, b, *c); 

}

Homework Equations

The Attempt at a Solution



The first things I'm trying to get a grasp on:
In the main function, where it first calls FindSum it has a b c as its parameters, and in the second call it has x y and z. Is this initial call to the function only going to edit the values of the integers for a b and c? Would it simply then shout out the values of x y and z? If this is the case, I'm assuming it would do the same for the second call to the function, but for x y and z. (And this is where it begins to confuse me)

In the function of FindSum, it only modifies the value of a, b, and c. During the second call to the function FindSum, would it simply echo the values of x y and z? They won't be changed?

And in the line "b += (b*2)" would it be

a += (5 *2)
b += (5 * 2)
*c += (10 * 2)

Sorry for all the questions. I'll try compiling the code when I'm at home, but for now I just want to try acting like the computer. Thanks alot!
 
Physics news on Phys.org
  • #2
Arnoldjavs3 said:
Is this initial call to the function only going to edit the values of the integers for a b and c?
No. The first two parameters (here a and b) are called by value, which means that just a copy is sent to FindSum. The third parameter (here c) is called by reference, which means that it may be changed by FindSum.
Arnoldjavs3 said:
Would it simply then shout out the values of x y and z?
What exactly do you mean by that?
Arnoldjavs3 said:
In the function of FindSum, it only modifies the value of a, b, and c
Oops - inside FindSum, a, b and c refers to the parameters to the function, not to the variables with the same name in main. As I explained above, only the third parameter may be assigned to - and it is.
Arnoldjavs3 said:
During the second call to the function FindSum, would it simply echo the values of x y and z? They won't be changed?
Again: A copy of x and y are sent to FindSum as parameters. FindSum modifies the variable referenced by the third parameter, so z will be changed.
Arnoldjavs3 said:
a += (5 *2)
b += (5 * 2)
*c += (10 * 2)
Almost correct - but since b was 5 to begin with, when you add (5*2), it becomes 15 before the line with *c. Thus *c += (15*2).
 

Related to Comprehending some C code (simple function calls)

What is C code?

C code is a programming language used for creating software and computer programs. It is a high-level language that is often used for system and application software, and is known for its efficiency and flexibility.

How do you comprehend C code?

To comprehend C code, you need to have a basic understanding of the language's syntax and structure. You also need to have knowledge of the specific libraries and functions used in the code, as well as the logic and purpose behind each line of code. Practice and experience are also important in comprehending C code.

What are function calls in C code?

Function calls in C code are used to execute a specific task or operation. They are used to break down complex tasks into smaller, more manageable units of code. Function calls can also pass parameters or arguments, which are values or variables used by the function to perform its task.

How do you read a function call in C code?

To read a function call in C code, you need to understand the function's name, return type, and any parameters that are being passed. The function name is usually followed by parentheses, and any parameters are placed inside the parentheses. The return type indicates the type of value that the function will return after it has been executed.

What is the purpose of function calls in C code?

The purpose of function calls in C code is to make the code more organized, modular, and reusable. By breaking down complex tasks into smaller functions, it becomes easier to understand and maintain the code. Function calls also help to avoid repetition of code and improve efficiency.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
788
  • Engineering and Comp Sci Homework Help
Replies
3
Views
790
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
710
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
929
  • Engineering and Comp Sci Homework Help
Replies
4
Views
985
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top