C语言 函数ppt
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Header Files
– Contain function prototypes for library functions包含了库 函数的函数原型 – <stdlib.h> , <math.h> , etc – Load with #include <filename>
#include <math.h>
int maximum( int, int, int ); • Takes in 3 ints • Returns an int
• Promotion rules and conversions
– Converting to lower types can lead to errors
4.3
• Header files
• Variables can be declared inside blocks (can be nested) • Functions can not be defined inside other functions
– Returning control
• If nothing returned – return; – or, until reaches right brace • If something returned – return expression;
– Arguments may be constants, variables, or expressions – Math library functions 数学库函数
• perform common mathematical calculations • #include <math.h>
4.2
Function Prototypes 函数原型
• Function prototype
– Function name – Parameters – what the function takes in – Return type – data type function returns (default int) – Used to validate functions – Prototype only needed if function definition comes after use in program – The function with the prototype
– Copy of argument passed to function – Changes in function do not effect original – Use when function does not need to modify argument
2000 Prentice Hall, Inc.
All rights reserved. Program Output
1 /* Fig. 4.4: fig05_04_2.c Outline 2 Finding the maximum of three integers */ Outline 3 #include <stdio.h> 4 1. Function 5 int maximum( int, int, int ); /* function prototype */ 6 prototype (3 7 int main() parameters) 8 { 9 int a, b, c; 10 2. Input values 11 printf( "Enter three integers: " ); 12 scanf( "%d%d%d", &a, &b, &c ); 13 printf( "Maximum is: %d\n", maximum( a, b, c ) ); 2.1 Call function 14 15 return 0; 16} 17 3.Function 18/* Function maximum definition */ definition 19int maximum( int x, int y, int z ) 20{ 21 int max = x; 22 23 if ( y > max ) 24 max = y; 25 26 if ( z > max ) 27 max = z; 28 29 return max; 2000 Prentice Hall, Inc. 30} All rights reserved.
• void – indicates that the function returns nothing
– Parameter-list参数表: comma separated list, declares parameters
• A type must be listed explicitly for each parameter unless, the parameter is of type int
• Communicate information between functions • Local variables局部变量
• Benefits of functions
– Divide and conquer分而治之
• Manageable program development – Software reusability软件重用 • Use existing functions as building blocks for new programs • Abstraction - hide internal details (library functions库函数) – Avoid code repetition避免代码重复
• Boss asks worker to complete task
– Worker gets information, does task, returns result – Information hiding: boss does not know details
• 从函数声明的角度看: 标准函数(库函数):由C++语言的集成 开发环境提供,存放在库(Library) 函数 中,用户在链接相应的库函数头文 件后便可以直接使用。 用户自定义的函数 • 从函数的形式看: 无参函数:调用该函数时,主调函数不需 函数 要将数据传递给被调用函数,只执 行指定的一组操作 有参函数:主调需传递数据给被调函数
Example
• Format for calling functions
– FunctionName( argument );
• If multiple arguments, use comma-separated list
– printf( "%.2f", sqrt( 900.0 ) );
• Calls function sqrt, which returns the square root of its argument • All math functions return data type double
Function Definitions
• Function definition format
return-value-type function-name( parameter-list ) { declarations and statements }
– Function-name: any valid identifier – Return-value-type: data type of the result (default int)
1 /* Fig. 4.4: fig05_04_1.c Outline 2 Finding the maximum of three integers */ Outline 3 #include <stdio.h> 4 5 /* Function maximum definition */ Function 6 int maximum( int x, int y, int z ) definition 7 { 8 int max = x; 9 10 if ( y > max ) 11 max = y; 12 13 if ( z > max ) 14 max = z; 15 16 return max; 17} 18 19int main() 20{ 1. Input values 21 int a, b, c; 22 23 printf( "Enter three integers: " ); 24 scanf( "%d%d%d", &a, &b, &c ); 25 printf( "Maximum is: %d\n", maximum( a, b, c ) ); 2.1 Call function 26 27 return 0; 28} 29 Enter three integers: 22 85 17 Maximum is: 85
Function Definitions
• Function definition format (continued)
return-value-type function-name( parameter-list ) { declarations and statements }
– Declarations and statements: function body (block)函数体
4.3 4.4 4.5 4.6 4.7 4.8 4.9
4.1
• Functions
Program Modules in C
– Modules in C – Programs combine user-defined functions with library functions
• C standard library 标准库has a wide variety of functions
• Custom header files
– Create file with functions – Save as filename.h – Load in other files with #include "filename.h" – Reuse functions
Calling Functions: Call by Value and Call by Reference • Used when invoking functions • Call by value按值调用
• Functห้องสมุดไป่ตู้on calls 函数调用
– Invoking functions • Provide function name and arguments (data)参数 (数据) • Function performs operations or manipulations • Function returns results – Function call analogy:
Chapter 4 - Functions
Outline
4.1 4.2 Program Modules in C Functions Function Definitions Function Prototypes Header Files Calling Functions: Call by Value and Call by Reference Storage Classes Scope Rules Recursion Example Using Recursion: The Fibonacci Series Recursion vs. Iteration
• Functions
Functions
– Modularize a program程序模块化 – All variables declared inside functions are local variables局部变量
• Known only in function defined
– Parameters参数