forked from davetang/getting_started_with_c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.c
48 lines (31 loc) · 874 Bytes
/
function.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdarg.h>
/*
In C, functions must be first defined before they are used
They can be either declared first and then implemented later on using a header file or in the beginning of the C file
C functions receive either a fixed or variable amount of arguments
Functions can only return one value, or none
*/
int multiply_by_ten(int x){
return x * 10;
}
/*
Functions with variable argument lists in C using va_list
http://www.cprogramming.com/tutorial/c/lesson17.html
*/
int multiply (int num, ... ){
va_list arguments;
int total;
va_start (arguments, num);
int x;
for (x = 0; x < num; x++){
total *= va_arg(arguments, int);
}
va_end (arguments);
return total;
}
int main (){
printf("%d\n", multiply_by_ten(10));
printf("%d\n", multiply(3, 10, 5, 2));
return 0;
}