Bài giảng Introduction to Computer Programming (C language) - Chapter 6: Functions - Võ Thị Ngọc Châu

Ho Chi Minh City University of Technology  
Faculty of Computer Science and Engineering  
Chapter 6: Functions  
Introduction to Computer Programming  
(C language)  
TS. Võ Thị Ngọc Châu  
2017 – 2018, Semester 2  
Course Content  
C.1. Introduction to Computers and  
Programming  
C.2. C Program Structure and its  
Components  
C.3. Variables and Basic Data Types  
C.4. Selection Statements  
C.5. Repetition Statements  
C.6. Functions  
C.7. Arrays  
C.8. Pointers  
C.9. File Processing  
2
References  
[1] “C: How to Program”, 7th Ed. – Paul  
Deitel and Harvey Deitel, Prentice Hall, 2012.  
[2] “The C Programming Language”, 2nd Ed.  
– Brian W. Kernighan and Dennis M. Ritchie,  
Prentice Hall, 1988  
and others, especially those on the Internet  
3
Content  
Introduction  
Functions in the standard library  
An example of a function  
Components of a function  
Function call  
Recursion  
Summary  
4
Introduction  
In the previous chapters, we have used  
several so-called functions in the library:  
printf in stdio.h  
Those functions are modular  
scanf in stdio.h  
processing units that are:  
fflush in stdio.h  
Responsible for a  
sqrt in math.h  
certain task  
pow in math.h  
Reusable in many  
system in stdlib.h  
strcmp in string.h  
strcpy in string.h  
various programs  
5
Functions in the standard library  
<assert.h> <locale.h>  
<stddef.h>  
<stdio.h>  
<ctype.h>  
<errno.h>  
<float.h>  
<limits.h>  
<math.h>  
<setjmp.h> <stdlib.h>  
<signal.h> <string.h>  
<stdarg.h> <time.h>  
6
Functions in the standard library  
Some functions in <stdio.h>  
int printf(const char *format, ...)  
Sends formatted output to stdout  
int scanf(const char *format, ...)  
Reads formatted input from stdin  
int getchar(void)  
Gets a character (an unsigned char) from stdin  
char *gets(char *str)  
Reads a line from stdin and stores it into the string pointed  
to, by str. It stops when either the newline character („\n‟)  
is read or when the end-of-file (EOF) is reached, whichever  
comes first.  
7
Functions in the standard library  
Some functions in <math.h>  
double cos(double x)  
Returns the cosine of a radian angle x  
double pow(double x, double y)  
Returns x raised to the power of y  
double sqrt(double x)  
Returns the square root of x  
double ceil(double x)  
Returns the smallest integer value greater than or equal  
to x  
double floor(double x)  
Returns the largest integer value less than or equal to x  
8
Functions in the standard library  
Some functions in <stdlib.h>  
void *malloc(size_t size)  
Allocates the requested memory and returns a pointer to it  
void free(void *ptr)  
Deallocates the memory previously allocated by a call to  
calloc, malloc, or realloc  
int rand(void)  
Returns a pseudo-random number in the range of 0 to  
RAND_MAX (at least 32767, up to implementation)  
int system(const char *string)  
The command specified by string is passed to the host  
environment to be executed by the command processor  
. E.g. “pause”, “cls”, “date”  
9
Repeated code!!!  
Can we just code  
them once and then  
make use of them  
over the time just  
like those in the  
standard library?  
Introductions  
Let‟s define a function: getNaturalNumber()  
Declared in a header file  
C6_function_getNaturalNumber_1.h  
for multiple uses  
Source code file: C6_function_getNaturalNumber.c  
Purpose: to ask users to input a natural number  
until a valid number is input  
11  
Use of our previously defined  
function, which is declared in  
a header file:  
“C6_function_getNaturalNumber_1.h”  
Compile:  
gcc C6_starTriangle_function_1.c C6_function_getNaturalNumber.c  
–o C6_starTriangle_function_1.exe  
Introduction  
A function  
A processing unit to perform a specific task  
A means to modularize a program for  
manageable program development  
C Program 1  
C Program 2  
main()  
main()  
Divide-and-conquer  
Reusable  
function1()  
function2()  
…
function1()  
function3()  
…
Information hiding  
Abstraction  
Easy for debugging  
13  
Introduction  
Issues related to functions  
Function definition  
Function declaration  
Function call  
14  
An example of a function  
Prepare your own library for numbers  
Compute the sum of N first natural numbers  
sum = 1 + 2 + 3 + … + (N-1) + N  
Compute the factorial of N, a natural number  
factorial = 1*2*3*…*(N-1)*N  
Compute the n-th power of x, a floating-point  
number  
xn = x*x*x*…*x  
Count the number of digits in N, a natural number  
N = 123456  
=> Number of digits = 6  
Round x, a floating-point number, with two digits  
after the decimal point  
x = 1.23456  
=> x = 1.23  
15  
x = 9.87654321 => x = 9.88  
An example of a function  
Prepare your own library for numbers  
Check if a natural number is a prime number  
7 => true (0)  
8 => false (0)  
Check if a natural number is a squared number  
4 => true (0)  
8 => false (0)  
Toggle non-zero digits to „9‟ digits in an integer number to  
generate its 9-based mask  
113789 => 999999  
-10789 => -90999  
Count the number of occurrences of each digit in an integer  
number  
113789 => 0: 0; 1: 2; 2: 0; 3: 1; 4: 0; 5: 0; 6: 0; 7: 1; 8: 1; 9: 1  
-20054 => 0: 2; 1: 1; 2: 1; 3: 0; 4: 1; 5: 1; 6: 0; 7: 0; 8: 0; 9: 0  
An example of a function  
Prepare your own library for numbers  
Estimate a value of e, the natural number  
Estimate a value of ex  
Estimate a value of PI  
17  
…  
Components of a function  
Given N, a natural number, calculate the  
factorial of N: N! = 1*2*..*N = (N-1)!*N  
18  
Components of a function  
Given N, a natural number, calculate the  
factorial of N: N! = 1*2*..*N = (N-1)!*N  
Function  
body that  
includes  
declarations  
and  
statements  
performed  
for a  
Parameter list  
with comma  
separation.  
Return  
type  
which is  
a data  
type of  
the value  
returned  
Function  
name  
No default  
value for each  
parameter in C  
functions.  
return statement to return a  
value of a return type to the caller  
specific task  
19  
Components of a function  
[static] return-type function-name (argument-declarations)  
{
declarations and statements  
}
- function-name: a valid identifier  
- argument-declarations: a list of formal parameters in communication  
+ Each parameter is regarded as a local variable with a data type.  
Part  
of the  
input  
+ Each parameter can be specified with “const” for unchanged intention.  
+ Each parameter can be passed by value or by reference if it is a pointer.  
- declarations: a list of local variables  
Process  
ing in  
its  
+ Each variable can be auto or static with one single initialization.  
- statements: zero, one, or many statements of any kind  
- return-type: a valid data type or void  
body  
Part  
of the  
output  
+ Statement return [<expression>]; in the body is used to end the  
called function and return [a value] to its caller. If not, close brace of the  
body ends the called function and program control is switched to its caller.  
- [static]: optional specification to make the function available only in the file  
Charac  
teristic  
where it is defined.  
20  
Tải về để xem bản đầy đủ
pdf 50 trang yennguyen 25000 Free
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Introduction to Computer Programming (C language) - Chapter 6: Functions - Võ Thị Ngọc Châu", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pdfbai_giang_introduction_to_computer_programming_c_language_ch.pdf