L9 C Function
L9 C Function
1
MSc. Nguyen Van Tien
Contents
2. Function definition
3. Function prototype
5. Recursive function
2
Concept
3
Concept
4
Standard function
5
Standard function
6
Standard function
8
User-defined function
createCircle() function
color() function
9
User-defined function
10
User-defined function
Enter Calculate
Print S
a, b, c > 0 S = a! + b! + c!
11
User-defined function
do {
printf(“Enter a positive integer: ”);
scanf(“%d”, &a);
} while (a <= 0);
do {
printf(“Enter a positive integer: ”);
scanf(“%d”, &b);
} while (b <= 0);
do {
printf(“Enter a positive integer: ”);
scanf(“%d”, &c);
} while (c <= 0);
12
User-defined function
{ Calculate s1 = a! = 1 * 2 * … * a }
s1 = 1;
for (i = 2; i <= a ; i++)
s1 = s1 * i;
{ Calculate s2 = b! = 1 * 2 * … * b }
s2 = 1;
for (i = 2; i <= b ; i++)
s2 = s2 * i;
{ Calculate s3 = c! = 1 * 2 * … * c }
s3 = 1;
for (i = 2; i <= c ; i++)
s3 = s3 * i;
13
User-defined function
14
Syntax
16
How to write a function
Example 1
• Function name: PrintSum
• Job: calculate and output the sum of 2 integers
• Input: two integers x and y
• Output: none
void PrintSum(int x, int y) {
int s;
s = x + y;
printf(“Sum of %d and %d is %d”, x, y, s);
}
17
How to write a function
Example 2
• Function name: GetSum
• Job: calculate and return the sum of 2 integers
• Input: two integers x and y
• Output: an integer with the value x + y
int GetSum(int x, int y) {
int s;
s = x + y;
return s;
}
18
How to write a function
Example 3
• Function name: ReadPrintSum
• Job: input and output the sum of 2 integers
• Input: none
• Output: none
void ReadPrintSum(int x, int y) {
printf(“Enter two integers: ”);
scanf(“%d%d”, &x, &y);
printf(“Sum of %d and %d is %d”, x, y, x + y);
}
19
The scope of the function
Concept
Is the effective range of variables and functions.
Variable:
Global: declare inside and outside all functions (including main
function) and affect the entire program.
Local: declared in a function or block { } and valid only within
the function or block itself (including its sub-blocks). The local
variable will be deleted from memory at the end of the block
declaring it.
20
The scope of the function
24
Parameters and function calls
Example: Write a program to swap 2 elements
#include<stdio.h>
int main()
// Pass by value
1 void Swap1 (int x, int y) { int m=12; n=28;
{
2
3
int temp = x; Swap1(m,n);
x = y;
4
y = temp; printf(“m=%d n=%d\n”,m,n”);
5
6
}
// Pass by address (pointer)
Swap2(&m,&n);
7
void Swap2 (int *x, int *y)
{
printf(“m=%d n=%d\n”,m,n”);
8
9
int temp = *x; Swap3(m,n);
*x = *y;
10 *y = temp; printf(“m=%d n=%d\n”,m,n”);
11 }
// Pass by reference return 0;
12 void Swap3 (int &x, int &y)
13 { }
14 int temp = x;
15 x = y;
16 y = temp;
17 }
18
25
Parameters and function calls
Pass by value
• Pass arguments to the function as values.
• Constants, variables, and expressions can be passed, but the
function will only receive a value.
• Used when there is no need to change the value of the
parameter after executing the function.
Pass by address
• Pass arguments to the function in the form of an address
(pointer).
• Do not pass a value to this parameter.
• Used when there is a need to change the value of the
parameter after executing the function.
void PassByAddress (int *x)
{
…
*x++;
}
27
Parameters and function calls
Pass by reference
• Pass arguments to the function in the form of an address
(pointer). Begins with & in the declaration.
• Do not pass a value to this parameter.
• Used when there is a need to change the value of the
parameter after executing the function.
Note
Within a function, parameters can be passed in many ways.
29
Parameters and function calls
Note
Using a reference is a way to return a value to a program.
int GetSum(int x, int y) {
return x + y;
}
void CalSum(int x, int y, int &sum) {
sum = x + y;
}
void CalSumDiff(int x, int y, int &sum, int &diff) {
sum = x + y; diff = x – y;
}
30
Principles when writing a function
31
Recursive function
Concept
• A subroutine can call another subroutine.
• Calling itself is called recursion.
• This number of calls must have a limit (breakpoint)
For example
Calculate S(n) = n! = 1*2*…*(n-1)*n
We see S(n) = S(n-1)*n
So instead of calculating S(n) we will go to calculate S(n-1)
Similarly calculate S(n-2), …, S(2), S(1), S(0) = 1
32
Recursive function
Example
int factorial(int n)
{
if (n == 0)
return 1;
else
return factorial(n – 1) * n;
}
33
Recursive function
35