> For the complete documentation index, see [llms.txt](https://krjaeh0.gitbook.io/j-log/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://krjaeh0.gitbook.io/j-log/programming/languages/c-and-c++/pointers-and-memory/function-arguments-by-reference.md).

# function arguments by reference

This page demonstrates passing function arguments by reference in C with several examples.

{% stepper %}
{% step %}

### EX1

Passing by value (no effect on caller's variable):

```c
/*EX1*/
void addone(int n) {
    // n is local variable which only exists within the function scope
    n++; // therefore incrementing it has no effect
}

int n;
printf("Before: %d\n", n);
addone(n);
printf("After: %d\n", n);
```

{% endstep %}

{% step %}

### EX2

Passing a pointer so the function can modify the caller's variable:

```c
/*EX2*/
void addone(int *n) {
    // n is a pointer here which points to a memory-address outside the function scope
    (*n)++; // this will effectively increment the value of n
}

int n;
printf("Before: %d\n", n);
addone(&n);
printf("After: %d\n", n);
```

{% endstep %}

{% step %}

### EX3

Modifying fields of a struct via pointer:

```c
/*EX3*/
void move(point * p) {
    (*p).x++;   // p->x++;
    (*p).y++;   // p->y++;
}
```

{% endstep %}
{% endstepper %}

## Exercise

Given the `person` struct and a `birthday` function that should increment the person's age by reference.

```c
typedef struct {
  char * name;
  int age;
} person;

/* function declaration */
void birthday(person * p);

/* write your function here */
void birthday(person * p)
{
    p -> age++;
}

int main() {
  person john;
  john.name = "John";
  john.age = 27;

  printf("%s is %d years old.\n", john.name, john.age);
  birthday(&john);
  printf("Happy birthday! %s is now %d years old.\n", john.name, john.age);

  return 0;
}
```

This program demonstrates modifying a struct's field by passing a pointer to the struct into the function.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://krjaeh0.gitbook.io/j-log/programming/languages/c-and-c++/pointers-and-memory/function-arguments-by-reference.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
