목차
7.1 함수의 반환값
7.2 함수의 원형(function prototype)
7.3 순환 함수(recursive function)
7.4 매개변수
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
{
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
}
예제. #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
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
추천자료
- 소비자 행동론
- 전공실험 레이놀즈수
- 나초스 - Alarm::WaitUntil(), scheduler, synchronization 구현
- 2009년 2학기 프로그래밍언어론 기말시험 핵심체크
- 2008년 1학기 정책평가론 중간시험과제물 A형(통계적통제)
- 2011년 2학기 프로그래밍언어론 기말시험 핵심체크
- 프로그래밍언어론 교재전범위 핵심요약노트
- 2012년 2학기 프로그래밍언어론 기말시험 핵심체크
- 소리의세계 - 공명에 대한 설명 그리고 공명의 원리와 그에 대한 예
- [마케팅특강 공통] 본인이 어떤 서비스업체의 대표라고 가정하고, 서비스품질 향상, 고객충...
- 2014년 2학기 프로그래밍언어론 기말시험 핵심체크
- 2014년 2학기 프로그래밍언어론 교재 전 범위 핵심요약노트
- 2015년 2학기 프로그래밍언어론 기말시험 핵심체크
- 조사방법론 과제: 여성이 화장품 구매시 영향을 주는 요인이 무엇인가