---
# System prepended metadata

title: Function

---

# Function
Function is a tool that defined by users .Each function can has diffrent type , like int ,char , float ....etc .

In nowadays , we use "functional programming " to write a program .We defined diffrent functions for each process instead of writing one main function to include all process .

Example :
```clike=
#include <stdio.h>
int main(){
    int a[10] = {1,2,3,4,5,6,7,8,9,10} ;
    printf("a[0] is %d\n" , a[0]) ;
    printf("a[1] is %d\n" , a[1]) ;
    printf("a[2] is %d\n" , a[2]) ;
    printf("a[3] is %d\n" , a[3]) ;
    printf("a[4] is %d\n" , a[4]) ;
    printf("a[5] is %d\n" , a[5]) ;
    printf("a[6] is %d\n" , a[6]) ;
    printf("a[7] is %d\n" , a[7]) ;
    printf("a[8] is %d\n" , a[8]) ;
    printf("a[9] is %d\n" , a[9]) ;
    return 0 ;
}
```
```clike=
#include <stdio.h>
void print_array(int * a){
    printf("a[0] is %d\n" , a[0]) ;
    printf("a[1] is %d\n" , a[1]) ;
    printf("a[2] is %d\n" , a[2]) ;
    printf("a[3] is %d\n" , a[3]) ;
    printf("a[4] is %d\n" , a[4]) ;
    printf("a[5] is %d\n" , a[5]) ;
    printf("a[6] is %d\n" , a[6]) ;
    printf("a[7] is %d\n" , a[7]) ;
    printf("a[8] is %d\n" , a[8]) ;
    printf("a[9] is %d\n" , a[9]) ;   
}
int main(){
    int a[10] = {1,2,3,4,5,6,7,8,9,10} ;
    print_array(a) ;
    return 0 ;
}
```

## Format
```clike
type function_name( arguments ){} ;
```

* **type** 
    The type of the value that the function return
* **function_name**
    The name defined by users , can be any ,but can have space or strange sign in function name

* **arguments**
    The variables that we pass into the function , when we call it .
    
Example :　
```clike=
void print_array(int * a){
    printf("a[0] is %d\n" , a[0]) ;
    printf("a[1] is %d\n" , a[1]) ;
    printf("a[2] is %d\n" , a[2]) ;
    printf("a[3] is %d\n" , a[3]) ;
    printf("a[4] is %d\n" , a[4]) ;
    printf("a[5] is %d\n" , a[5]) ;
    printf("a[6] is %d\n" , a[6]) ;
    printf("a[7] is %d\n" , a[7]) ;
    printf("a[8] is %d\n" , a[8]) ;
    printf("a[9] is %d\n" , a[9]) ;   
}
```
* name : `print_array`
* type : `void`
* arguments :　`int *a`

Type `void` means we don't need to return any value  , and `*a` is a way to pass a array into the function .

## IO funtion
IO = input / output
### printf()

```clike
printf("string you want to out put %d%d%d.....\n" ,a,b,c,....) ;
```
If you have n % , you have to input n variables . % symbol can be plugged in the string. 
* name : `printf`
* arguments : "....." , a ,b ,c ,.....


[printf source code](https://code.woboq.org/userspace/glibc/stdio-common/printf.c.html)
[argument](https://docs.microsoft.com/zh-tw/cpp/c-runtime-library/format-specification-syntax-printf-and-wprintf-functions?view=msvc-160)

Example of `printf`

`\n` is the special character that will 換行 .... Characters that with `\` begin may have special function .
```clike=
#include <stdio.h>

int main(void) {
    printf("顯示字元 %c\n", 'A');
    printf("顯示字元編碼 %d\n", 'A');
    printf("顯示字元編碼 %c\n", 65);    
    printf("顯示十進位整數 %d\n", 15);
    printf("顯示八進位整數 %o\n", 15);
    printf("顯示十六進位整數 %X\n", 15);
    printf("顯示十六進位整數 %x\n", 15);    
    printf("顯示科學記號 %E\n", 0.001234);    
    printf("顯示科學記號 %e\n", 0.001234);    
    return 0;
}
```

### scanf()

* **scan `int`**
```clike
int a ;
scanf("%d" , &a) ;
```

* **scan `char`**
```clike
char c ;
scanf("%c" , &c) ;
```

* **scan string**
```clike
char a[100] ;
scanf("%s" , a) ;
```

Example : 
```clike=
#include <stdio.h>

int main(int argc, char const *argv[])
{
	char c ;
	printf("Enter a character : ");
	scanf("%c" , &c) ;
	printf("the character is %c\n", c);
	
	int a ;
	printf("Enter a number : ");
	scanf("%d"  , &a) ;
	printf("the number is %d\n", a);

	char s[100] ;
	printf("Enter a word : ");
	scanf("%s"  , s) ;
	printf("the word is %s\n", s);

	return 0;
}
```
```clike
$ gcc io.c -o io
$./io
```

```clike
Enter a character : a
the character is a

Enter a number : 5
the number is 5

Enter a word : hi
the word is hi


```