# 引數與傳回值

``````int main(void) {
int x = 10;
....
printf("%d\n", increment(x));
printf("%d\n", x);

return 0;
}

int increment(int n) {
n = n + 1;
return n;
}
``````

``````int main(void) {
int x = 10;
....
printf("%d\n", increment(&x));
printf("%d\n", x);

return 0;
}

int increment(int *n) {
*n = *n + 1;
return *n;
}
``````

``````#include <stdio.h>
#include <stdlib.h>

int* ints(int, int);

int main(void) {
int size = 0;
int init = 0;

printf("陣列大小：");
scanf("%d", &size);
printf("元素初值：");
scanf("%d", &init);

int *arr = ints(size, init);
for(int i = 0; i < size; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}

free(arr);

return 0;
}

int* ints(int size, int init) {
int *arr = malloc(size * sizeof(int));
for(int i = 0; i < size; i++) {
arr[i] = init;
}
return arr;
}
``````

``````陣列大小：5

arr[0] = 3
arr[1] = 3
arr[2] = 3
arr[3] = 3
arr[4] = 3
``````

``````#include <stdio.h>
#include <stdlib.h>

int* ints(int, int);

int main(void) {
int size = 0;
int init = 0;

printf("陣列大小：");
scanf("%d", &size);
printf("元素初值：");
scanf("%d", &init);

int *arr = ints(size, init);
for(int i = 0; i < size; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}

free(arr);

return 0;
}

int* ints(int size, int init) {
int arr[size];
for(int i = 0; i < size; i++) {
arr[i] = init;
}
return arr; // warning: function returns address of local variable
}
``````

``````#include <stdio.h>
#include <stdlib.h>

void printInts(int arr[5]) {
for(int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
}

int main(void) {
int arr[] = {1, 2, 3, 4, 5};
printInts(arr);

return 0;
}
``````

``````#include <stdio.h>
#include <stdlib.h>

void printInts(int len, int arr[len]) {
for(int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
}

int main(void) {
int arr[] = {1, 2, 3, 4, 5};
printInts(5, arr);

return 0;
}
``````

``````#include <stdio.h>
#include <stdlib.h>

void printInts(int len, int *arr) {
for(int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
}

int main(void) {
int arr[] = {1, 2, 3, 4, 5};
printInts(5, arr);

return 0;
}
``````

``````#include <stdio.h>
#include <stdlib.h>

void printInts(int len, int *arr) {
for(int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
}

int main(void) {
printInts(5, (int[]) {1, 2, 3, 4, 5});

return 0;
}
``````

`(int[]) {1, 2, 3, 4, 5}``int[]` 轉型是必要的，編譯器需要這項資訊知道這是個 `int` 陣列。