C语言程序设计(双语)课件Chapter5Functions
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
C语⾔程序设计(双语)课件Chapter5Functions
Chapter 5
Functions
Related Sections:Roberts,Chapter 5,P137
Today we start our study of functions in C. This is one of the most crucial parts of the language—something you will use in every program that you write. Even if you don’t define any new functions by yourself, main() is a function that has to be present in any complete C program.
Objectives
To understand the concept of calling a function and the reason for supplying arguments as part of the call.
To understand function prototypes and how to write them.
To be able to implement simple functions containing statements used in the previous chapters.
To be able to use the return statements to specify the result of a function
To understand the concept of a predicate functions and how to use them effectively.
To understand the relationship between formal parameters in a function and arguments in its caller.
Definition
A function is a set of statements that have been collected together and given a name.
A function body is always a block and therefore consists of statement enclosed in curly braces.
Functions
Functions from mathematics Functions from C libraries Writing your own functions
Library Functions
stdio.h
printf()
scanf()
string.h
strcat()
strcmp()
strlen()
Library Functions math.h
abs()
fabs()
log()
pow()
sin()
cos()
Consider the library function What do the function do when you use it?
Writing your own function
see P.140
Step 1:Function declaration before calling function should include following three parts:
(also called Function Prototype)
The name of the function
The type of each argument to the function and ,in most cases ,a description name for the argument.
The type of the value the functions return
double CelsiusToFahrenheit(double c);
name result type arguments specifiers
Writing your own function Step 2:Function definition
double CelsiusToFahrenheit(double c)
{
return (9.0/5.0*c+32);
}
Writing your own function
The Return Statement in function
If a function returns a result ,the statement in the function body must include at least one return statement,which specifies the value to be returned.
return(expression);
Writing your own function-Example 1
P.144 Figure 5-1
Generating a table of Celsius to
Fahrenheit conversions
Example 2
See Roberts p.147
To define a function called Factorial that takes an integer n and returns the
product of the integers between 1 and n. Factorial(0) = 1
Factorial(1) = 1 = 1*1
Factorial(2) = 2 = 1*2
Factorial(3) = 6 = 1*2*3
Factorial(4) = 24 = 1*2*3*4
Factorial(5) =120 = 1*2*3*4*5
Factorial function
int Factorial(int n)
{
int product,I;
product=1;
for(I=1;I<=n;I++){
product*=I;
}
return (product);
}
Functions that return nonumeric
values
Functions in C can return values of any data type.
For example,if you were writing a program to convert a numeric month (1-12) into the string that indicates the corresponding month name between January and December.
Example P147 Figure5-3 MonthName
Predicate functions
See Roberts 148
Functions that return values 0 or 1 ,which stands for true or false,are called predicate functions.
Predicate Function-Example1 int IsEven(int n);
main()
{ int I;
for(I=1;I<=10;I++)
if (IsEven(i))
printf(“%2d\n”,I);
}
int IsEven(int n)
{ return(n%2==0);
}
Predicate Function-Example2 Exercise in class:
Write a program to determine whether year is
a leap year.#include
int IsLeapYear(int n);
main()
{ int year;
scanf("%d",&year);
if(IsLeapYear(year)) printf("This is a leap year!\n");
else printf("This isn't a leap year!\n");
}
int IsLeapYear(int n)
{ return( (n%4==0)&&(n%100!=0)||(n%400==0) );}
Mechanics of the function-calling process
see P150
#include
#define LowerLimit 0
#define UpperLimit 7
int Factorial(int n);
main()
{
int i;
for (i = LowerLimit; i <= UpperLimit; i++) { printf("%d! = %5d\n", i, Factorial(i)); }
}
int Factorial(int n)
{
int product, i;
product = 1;
for (i = 1; i <= n; i++) {
product *= i;
}
return (product); }
Factorial
n product i
i
main
i
main Factorial
n
01
product i
1。