영어로 읽는 코딩

08 [C] 함수(function)

#include <stdio.h>

int power(int m, int n); //function prototype

/* test power function*/

int main() {
    int i;

	for (i = 0; i < 10; ++i)
		printf("%d %d %d\n", i, power(2, i), power(-3, i));
	return 0;
}

/*power: raise base to n-th power: n >= 0 */
int power(int base, int n)
{
	int i, p;

	p = 1;
	for (i = 1; i <= n; ++i)
		p = p * base;
	return p;
}

 

* 함수의 형태

A function definition has this form:

return-type function-name (parameter declarations, if any)
{
           declarations
           statements
}

 

* 함수의 호출과 인수의 처리

The function power is called twice by main, in the line

printf( ”%d %d %d\n” , i , power( 2 , I ), power( -3 , I ));

Each call passes two arguments to power, which each time returns an integer to be formatted and printed. In an expression, power ( 2 , i ) is an integer just as 2 and i are.

The first line of power itself,

int power(int base, int n)

declares the parameter types and names, and the type of the result that the function returns. The names used by power for its parameters are local to power, and are not visible to any other function: other routines can use the same names without conflict. This is also true of the variables i and p: the i in power is unrelated to the i in main.

We will generally use parameter for a variable named in the parenthesized list in a function definition, and argument for the value used in a call of the function. The terms formal argument and actual argument are sometimes used for the same distinction.

 

* 리턴 (함수와 메인)

The value that power computes is returned to main by the return statement. Any expression may follow return:

return expression;

A function need not return a value; a return statement with no expression causes control, but no useful value, to be returned to the caller, as does “falling off the end" of a function by reaching the terminating right brace. And the calling function can ignore a value returned by a function.

You may have noticed that there is a return statement at the end of main. Since main is a function like any other, it may return a value to its caller, which is in effect the environment in which the program was executed. Typically, a return value of zero implies normal termination; non-zero values signal unusual or erroneous termination conditions. In the interests of simplicity, we have omitted return statements from our main functions up to this point, but we will include them hereafter, as a reminder that programs should return status to their environment.

 

* 함수 프로토타입 설명

The declaration

int power(int m, int n);

just before main says that power is a function that expects two int arguments and returns an int. This declaration, which is called a function prototype, has to agree with the definition and uses of power. It is an error if the definition of a function or any uses of it do not agree with its prototype.

Parameter names need not agree. Indeed, parameter names are optional in a function prototype, so for the prototype we could have written

int power(int, int);

A Well-chosen names are good documentation, however, so we will often use them.

 

[The C Programming Language (second edition, Prentice Hall) by Brian W. Kernighan and Dennis M. Ritchie]

댓글

댓글 본문
버전 관리
Yoo Moon Il
현재 버전
선택 버전
graphittie 자세히 보기