Example 1 : Build a function to calculate n!
long gtref(int n)
{
Maybe you are interested!
-
Programming Foundation - University of Commerce - 1 -
Practice database programming with VB.net - 39 -
Java programming Web design profession - Dalat College of Technology - 8 -
Mitsubishi PLC Programming Guide - 3 -
Java programming Web design profession - Dalat College of Technology - 10
long gt=1;
for (int i=2;i<=n;i++)

gt=gt*i;
return gt;
}
7/2020 Programming Fundamentals 145
Example 2 : Build a function to calculate the k-combination of n. int tohop(int n, int k)
{
int th; th=gthua(n)/(gthua(k)*gthua(nk)); return th;
}
7/2020 Programming Fundamentals 146
Meaning : return value to function
Syntax :
return (value);
Function type : functions with types other than void
Location : Usually placed on the command line
end of the body
Note :
A function can have multiple return statements.
7/2020 Programming Fundamentals 147
Example 1: calculate n!
long gtref(int n)
{
long gt=1;
for (int i=2;i<=n;i++) gt=gt*i;
return gt;
}
7/2020 Programming Fundamentals 148
Example 2 : Check if a positive integer is
is a prime number
int ktnto(int n)
{ if (n==1 || n==2) return 1;
else
{ for (int i=2;i<n;i++)
if (n%i==0) return 0; return 1;
}
}
7/2020 Programming Fundamentals 149
Execution process when encountering function call
Allocate memory for arguments and local variables
Assign the values of real parameters to the objects
corresponding
Execute statements in the function body
When encountering the return statement or the last } of the function body, the machine will delete the arguments, local variables and leave the function.
7/2020 Programming Basics 150
Note:
Function arguments and local variables need to have different names.
Cannot take the value of an argument out of a function
Arguments cannot be used to change the values of quantities outside the function.
Arguments can be changed in the function body but actual parameters are not changed (arguments are just copies of actual parameters)
7/2020 Programming Fundamentals 151
Location of appearance: function call
Meaning : The return value of the function is a specific value of the declared type.
Role : Can be used as a constant value in assignments, arithmetic operations, input/output statements.
For example
printf(“factorial value is: %ld”,gthua(n));
th=gthua(n)/(gthua(k)*gthua(nk));
7/2020 Programming Fundamentals 152
Note : If the function has no return value → answer
function calls will stand alone as a command;
For example:
void import(int a[],int n)
{
for (int i=0;i<n;i++) scanf(“%d”,&a[i]);
}
int x[10], d; enter(x,d);
7/2020 Programming Fundamentals 153
4.2.1 Global variables and local variables
4.2.2 Formal parameters of functions
4.2.3 Passing parameters
7/2020 Programming Fundamentals 154
Meaning : the type of variable will determine the life cycle and
its scope of operation.
Global variable : is a variable that has the scope of the entire program.
Location : declared outside every function, under declaration
pre-treatment report
Life cycle : from the position it is declared to the end of the program.
7/2020 Programming Fundamentals 155
Local variable : is a variable that operates within a function or a block of code.
Position : function parameter or declared variable
report in function body
Lifecycle : from the position it is declared to the end of the scope. (block/function)
Note :
Should use local variables to reduce storage space
storage and easy control
All variables declared in the body of functions are local variables (including the main function).
7/2020 Programming Fundamentals 156
For example:
#include <stdio.h>
#include <conio.h>
int x=1 ; // global variable int test ();
int main(int argc, char *argv[])
{ int y = 5; //local variable
printf("%d", test()) ;
printf("%d",y); getch();
return 0;
}
int test()
{ return x *2; }
7/2020 Programming Fundamentals 157
Formal parameters of a function: are parameters written in the function definition.
Request :
Parameters must have both a type and a name.
The type, order and number of parameters passed must be exactly as declared in the prototype, the parameter names may differ.
Parameters can have default values, which are
This must be written as close to the right as possible.
7/2020 Programming Fundamentals 158
For example
void import(int a[],int n=10)
{
for (int i=0;i<n;i++) scanf(“%d”,&a[i]);
}
7/2020 Programming Fundamentals 159
Classify:
Input parameter: contains known value
Output parameter: contains the newly received results.
Note:
There are parameters that act as both input and output parameters.
For example:
7/2020 Programming Basics 160





