CHAPTER 2: BASIC OF C/C++
D ATA S T R U C T U R E a n d A L G O R I T H M S
M.E. LE THANH TUNG
2.4 FUNCTION:
⚬ A function in C is a set of statements that when called perform
some specific task. It is the basic building block of a C program that
provides modularity and code reusability.
⚬ There are two types of function in C programming: Standard library
functions and User-defined functions
⚬ The syntax of function in C can be divided into 3 aspects:
⚬ Function Declaration.
⚬ Function Definition.
⚬ Function Calls.
2.4 FUNCTION:
• 2.4.1 Function declaration:
⚬ In a function declaration, we must provide the function name, its
return type, and the number and type of its parameters.
⚬ A function declaration tells the compiler that there is a function
with the given name defined somewhere else in the program.
⚬ Syntax of function declaration:
return-type Function_name (parameters);
2.4 FUNCTION:
• 2.4.1 Function declaration:
⚬ Example:
■ Notice: A function in C must always be declared globally
before calling it.
2.4 FUNCTION:
• 2.4.1 Function declaration:
⚬ return-type: the data type of the value the function returns. If the
function doesn’t return a value, using the void keyword for return-
type
⚬ Function_name: the actual name of the function. The
⚬ parameters: A parameter is like a placeholder. When a function is
invoked, you pass a value to the parameter. In C, parameter
includes the data type and the name of the parameter.
2.4 FUNCTION:
• 2.4.2 Function definition:
⚬ The function definition consists of actual statements which are
executed when the function is called (i.e. when the program control
comes to the function).
⚬ In C, the function is only defined after declaration.
⚬ Syntax of function definition:
return-type Function_name (parameters)
{
statements;
}
2.4 FUNCTION:
• 2.4.2 Function definition:
⚬ Example:
2.4 FUNCTION:
• 2.4.2 Function definition:
⚬ Notice:
⚬ A C function is generally defined and declared in a single
step because the function definition always starts with the
function declaration so we do not need to declare it
explicitly.
2.4 FUNCTION:
• 2.4.2 Function definition:
⚬ Return statement:
⚬ Function return type tells what type of value is returned after
all function is executed. In the return statement:
⚬ C return statement ends the execution of a function and
returns the control to the function from where it was called.
⚬ When a line of code in a function that says: "return X;" is
executed, the function "ends" and no more code in the
function is executed. The value of X becomes the result of
the function.
⚬ Only one value can be returned from a C function. To return
2.4 FUNCTION:
• 2.4.2 Function definition:
#include <stdio.h>
//function definition – return-type function
int sum(int a, int b)
{
return a+b;
}
int main()
{
int x,y,z;
printf("Nhap gia tri x,y: ");
scanf(“%d%d”, &x, &y);
z = sum(x,y); //function call
printf("\nz = %d ",z);
return 0;
}
2.4 FUNCTION:
• 2.4.2 Function definition:
⚬ Void function:
⚬ When we don’t want to return a value, we can use the void data
type.
⚬ With the void function, it is not necessary to use return
void Function_name (parameters)
statements.
{
statements;
}
2.4 FUNCTION:
• 2.4.2 Function definition:
#include <stdio.h>
//function definition – return-type function
int sum(int a, int b)
{
printf(“Ket qua: %d”, a+b);
}
int main()
{
int x,y,z;
printf("Nhap gia tri x,y: ");
scanf(“%d%d”, &x, &y);
sum(x,y); //function call
return 0;
}
2.4 FUNCTION:
• 2.4.3 Function call:
⚬ A function call is a statement that instructs the compiler to execute
the function.
⚬ To call a function, you simply need to pass the values for the
required parameters along with the function name.
Function_name (value1, value2,…);
⚬ And if the return-function, then you can store the returned value
with a variable.
variable = Function_name (value1, value2,…);
2.4 FUNCTION:
• 2.4.3 Function call:
⚬ In the below example, the
sum function is called and
10,30 are passed to the
parameters.
⚬ And the statements of function
will be executed (calculate the
sum of a and b and returned
it).
⚬ After the function call, the
program is also returned back
2.4 FUNCTION:
• 2.4.3 Function call:
⚬ Note:
⚬ Function call is neccessary to bring the program control to the
function definition. If not called, the function statements will not be
executed.
2.4 FUNCTION:
• 2.4.3 Function call:
#include <stdio.h>
int Factorial(int N)
{ int F = 1;
for(int i = 1;i<=N;i++)
{
F = F*i;
}
return F;
}
int main()
{
int x,y;
printf("Nhap gia tri x: ");
scanf(“%d”, &x);
y = Factorial(x); //function call
printf("\ny = %d ",y);
return 0;
}
2.4 FUNCTION:
• 2.4.3 Function call:
⚬ Passing parameters to
function:
⚬ Formal Parameters are the
variable and the data type as
mentioned in the function
declaration.
⚬ The data passed when the
function is being invoked
(Function call) is known as the
Actual parameters.
2.4 FUNCTION:
• 2.4.3 Function call:
⚬ Passing parameters to function:
⚬ We can pass parameters to the C function in two ways:
⚬ Pass by Value: Parameter passing in this method copies values
from actual parameters into formal function parameters. As a
result, any changes made inside the functions do not reflect in
the caller’s parameters.
⚬ Pass by Reference: This method copies the address of an
argument into the formal parameter. This means that changes
made to the parameter affect the argument.
2.4 FUNCTION:
• 2.4.3 Function call:
#include <stdio.h>
void swap(int var1, int var2)
{
int temp = var1;
var1 = var2;
var2 = temp;
}
int main()
{
int x = 3, y = 2;
printf("Before swap Value of var1 and var2 is: %d, %d\n" , x, y);
swap(x, y);
printf("After swap Value of var1 and var2 is: %d, %d" , x, y);
return 0;
}
2.4 FUNCTION:
• 2.4.3 Function call:
#include <stdio.h>
void swap(int *var1, int *var2)
{
int temp = *var1;
*var1 = *var2;
*var2 = temp;
}
int main()
{
int x = 3, y = 2;
printf("Before swap Value of var1 and var2 is: %d, %d\n" , x, y);
swap(&x, &y);
printf("After swap Value of var1 and var2 is: %d, %d" , x, y);
return 0;
}
2.4 FUNCTION:
• Functions in C is a highly useful feature of C with many
advantages as mentioned below:
• The function can reduce the repetition of the same statements
in the program.
• The function makes code readable by providing modularity to our
program.
• There is no fixed number of calling functions it can be called as
many times as you want.
• The function reduces the size of the program.
• Once the function is declared you can just use it without thinking
about the internal working of the function.
DATA STRUCTURE &
ALGORITHMS
THANKS YO U