함수에 대하여
본 자료는 2페이지 의 미리보기를 제공합니다. 이미지를 클릭하여 주세요.
닫기
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
해당 자료는 2페이지 까지만 미리보기를 제공합니다.
2페이지 이후부터 다운로드 후 확인할 수 있습니다.

목차

7.1 함수의 반환값

7.2 함수의 원형(function prototype)

7.3 순환 함수(recursive function)

7.4 매개변수

본문내용

*/
{
int i;
for (i=0; i<5; i++) printf("%d ", num[i]);
}
** 위의 3가지 함수 모두 동일하게 하나의 포인터 매개변수를 생성한다.
3번째 방법이 가장 널리 사용된다.
예제. #include "stdio.h"
void prompt(char *msg, int *num);
void main(void)
{
int i;
prompt("Enter a number: ", &i);
printf("Your number is : %d\n", i);
}
void prompt(char *msg, int *num)
{
printf("%s", msg);
scanf("%d", num); /* num 앞에 &가 붙지 않는다. num 자체가 주소 */
}
7.5 main() 함수의 인수
명령어-라인(command-line) 인수를 전달받기 위해 사용된다.
(TC 통합환경에서는 실행하기 전에 Options - Arguments에 인수를 적어준다.)
형식: void main(int argc, char *argv[]);
argc: 명령어-라인에 있는 인수들의 수 (실행 프로그램의 이름을 포함)
argv: 문자열 포인터들의 배열
** 일반적인 사용: argv[0] = "실행 프로그램의 이름"
argv[1] = "첫번째 인수"
: :
예. #include "stdio.h"
void main(int argc, char *argv[])
{
int i;
for (i=1; i printf("%s ", argv[i]);
}
예제. #include "stdio.h"
#include "stdlib.h"
void main(int agrc, char *argv[])
{
int i;
double d;
long l;
i = atoi(argv[1]);
l = atol(argv[2]);
d = atof(argv[3]);
printf("%d, %ld, %lf\n", i, l, d);
}
예제. #include "stdio.h"
#include "stdlib.h"
void main(int agrc, char *argv[])
{
double pounds;
if (argc != 2) {
printf("Usage: CONVERT \n");
printf("Try again!");
exit(1); /* 프로그램을 종료하기 위한 표준 함수 */
}
pounds = atof(argv[1]) / 16.0;
printf("%lf pounds\n", pounds);
}
7.6 매개변수 선언상의 차이
지금까지 사용한 것과 달리 초기에는 구형식이 사용되었다.
구 형식(classic form): 초기 선언 방법
type function-name(parameter1, parameter2, ..., parameterN)
type parameter1;
type parameter2;
...
type parameterN;
{
function-code
}
예. float f(ch, size, max)
char ch;
long size;
double max;
{

}
신 형식(modern form): ANSI 표준
type function-name(type parameter1, type parameter2, ..., type parameterN);
{
function-code
}
예. float f(char ch, long size, double max)
{

}
** ANSI 표준에서는 어느 형식을 사용해도 괜찮다.
하지만 신형식을 사용하는 것이 바람직하다.
프로그래밍 연습
7.1절 연습문제 1
7.3절 연습문제 1
7.5절 연습문제 1, 2
종합문제 2-1, 2, 3

키워드

  • 가격1,300
  • 페이지수8페이지
  • 등록일2003.02.10
  • 저작시기2003.02
  • 파일형식한글(hwp)
  • 자료번호#221068
본 자료는 최근 2주간 다운받은 회원이 없습니다.
청소해
다운로드 장바구니