- 함수 : 프로그램의 여러가지 기능 중에 한가지 기능을 하는 것이 함수.
printf -> 출력 함수, scanf -> 입력 함수
- 표준함수 : 구현에 필요한 기능을 미리 제공되는 함수
- 사용자 정의 함수 : 프로그래머가 개발시에 필요에 따라 직접 만든 기능
[리턴 타입] [함수명](매개변수){
수행할 코드
return 값;
}
int plus(int a, int b){
return a+b;
}
- return : 함수 수행 결과를 호출한 쪽으로 보내줌
- 리턴 타입 : 함수 수행 결과의 데이터 타입을 알려줌
- 함수명 : 호출명
- 매개변수 : 호출한 쪽에서 함수가 수행되기 위한 데 이터를 보내 주는 부분
- void : 리턴할 결과 없을 때 쓰는 것
예제> 사칙연산, 절대값, 큰값, 작은값 구하는 함수
#include<stdio.h>
int plus(int a,int b){
return a+b;
}
int minus(int a,int b){
return a-b;
}
int mul(int a,int b){
return a*b;
}
int div(int a,int b){
return a/b;
}
int mod(int a,int b){
return a%b;
}
int max(int a,int b){
if(a>b)
return a;
else
return b;
}
int min(int a,int b){
/*
if(a<b)
return a;
else
return b;
*/
return a < b ? a:b;
}
int abs(int a){
/*
if(a < 0)
return -a;
return a;
*/
return a<0 ? -a:a;
}
double plus(double a, double b){
return a+b;
}
void main(){
int a=10,b=20;
printf("%d+%d=%d\n",a,b,plus(a,b));
printf("%d-%d=%d\n",a,b,minus(a,b));
printf("%d*%d=%d\n",a,b,mul(a,b));
printf("%d/%d=%d\n",a,b,div(a,b));
printf("%d%%%d=%d\n",a,b,mod(a,b));
printf("max=%d\n",max(a,b));
printf("min=%d\n",min(a,b));
printf("abs=%d\n",abs(-3));
}
'Code Archive > C언어' 카테고리의 다른 글
| Step8. 포인터 (0) | 2014.09.06 |
|---|---|
| Step7. 배열 (0) | 2014.09.05 |
| Step5. 반복문(for,while,do-while) (0) | 2014.09.05 |
| Step3. 연산자 (0) | 2014.09.05 |
| Step4. 조건문(if,switch) (0) | 2014.09.05 |