C 언어에서 함수 선언 및 사용
함수의 선언/정의/호출(사용)
#include <stdio.h> // 함수 선언(Declaration) void sub(); int main() { // 함수 호출 sub(); return 0; } // 함수 정의(Definition) void sub() { printf("여기는 sub() 함수\n"); }
파라미터(Parameters)를 가진 함수의 선언 및 사용
#include <stdio.h> //함수 선언(Declaration) void max(int a, int b); int main() { //함수 호출 max(3, 4); return 0; } //함수 정의(Definition) void max(int a, int b) { if (a > b) printf("%d \n", a); else { printf("%d \n", b); } }
파라미터와 리턴타입을 가진 함수의 선언 및 사용 예
#include <stdio.h> //함수 선언(Declaration) int factorial(int n); int main() { //함수 호출 int res = factorial(4); printf("4!=%d \n", res); return 0; } //함수 정의(Definition) int factorial(int n) { int res = 1; while (n > 0) { res *= n; n--; } return res; }