c Programming - Chapter 5 - Functions
c Programming - Chapter 5 - Functions
PROGRAMMING 2
C/C++ LANGUAGE
• User-defined function:
• You can also create functions as per your need.
• Such functions created by the user are known as user-defined functions.
5 . 1 F U N C T I O N D E C L A RAT I O N i n C
• 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 0;
}
5.2 FUNCTION DEFINITION
#include <stdio.h>
//function definition – void function
void sum(int a, int b)
{
printf(“Ket qua: %d”, a+b);
}
int main()
{
int x,y;
printf("Nhap gia tri x,y: ");
scanf(“%d%d”, &x, &y);
sum(a,b); //function call
return 0;
}
5.3 FUNCTION CALL
• 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 required parameters along
with the function name, and if the function returns a value, then you can
store the returned value.
• When a program calls a function, the program control is transferred to the
called function. And when its function-ending closing brace is reached, it
returns the program control back to the main program.
• Note: Function call is neccessary to bring the program control to the function
definition. If not called, the function statements will not be executed.
5.3 FUNCTION CALL
• Function call:
• In the below example, the first sum function is called and 10,30 are passed to
the sum function. After the function call sum of a and b is returned and control
is also returned back to the main function of the program.
5.2 FUNCTION DEFINITION
#include <stdio.h>
int Factorial(int N)
{ int i = 1,F = 1;
while(i<=N)
{ F = F*i;
}
return F;
}
int main()
{
int x,y,z;
printf("Nhap gia tri x: ");
scanf(“%d”, &x);
y = Factorial(x); //function call
z = Factorial(y); //function call
printf("\nz = %d ",z);
return 0;
}
5.3 FUNCTION CALL
• Passing parameters to function:
• The data passed when the function is being invoked is known as the Actual
parameters.
• In the below program, 10 and 30 are known as actual parameters. Formal
Parameters are the variable and the data type as mentioned in the function
declaration; a and b are known as formal parameters.
5.3 FUNCTION CALL
• Passing parameters to function:
• We can pass parameters to the C function in two ways:
■ Pass by Value
■ Pass by Reference
• 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.
5.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;
}
5.3 FUNCTION CALL
• Passing parameters to function:
• We can pass parameters to the C function in two ways:
• Pass by Value
■ Pass by Reference
• Pass by Reference:
• This method copies the address of an argument into the formal parameter.
• Inside the function, the address is used to access the actual argument used in
the call. This means that changes made to the parameter affect the
argument.
5.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;
}
F U N C T I O N A D VA N TA G E S
available in C.
• User-Defined Header Files: The header files which are defined by the user then
they are called user-defined header files. The #define directive is used to define a
header file.
5 . 4 F U N C T I O N & L I B RA RY
• Include Header files:
• There are two ways to include a header file in your program:
• #include<headerFilename> : The header file is enclosed within angular brackets.
This is the most common way of defining a header file. The preprocessor searches in
an implementation-defined manner, normally in directories pre-designated by the
compiler/IDE. This method is normally used to include header files for the C standard
library and other header files associated with the target platform.
• #include“headerFilename” : This is enclosed within double-quotes. The
them in our program to use whenever we want. It enhances code functionality and
readability.
• Below are the steps to create our own header file:
• Step 1: Write your own C/C++ code and save that file with the “.h” extension.
• Step 2: Write the body header file (define functions, variables, …)
• Step 3: Include your header file with “#include” in your C/C++ program
5 . 4 F U N C T I O N & L I B RA RY
Create the myheader.h files and save in the same location with program file
return 0;
}
5 . 4 F U N C T I O N & L I B RA RY
• Once Only headers in C:
• If you include the header file in a program code twice then it will give you an error.
• So, to avoid this type of problem, you will have to enclose the contents of the header file
#ifndef HEADER_FILE
#define HEADER_FILE
....
....
#endif
5 . 4 F U N C T I O N & L I B RA RY
Create the myheader.h files and save in the same location with program file
#ifndef header_file
#define header_file
void swap(int *var1, int *var2)
{
int temp = *var1;
*var1 = *var2;
*var2 = temp;
}
int Factorial(int N)
{ int i = 1,F = 1;
while(i<=N)
{ F = F*i;
}
return F;
}
#endif
Thank you!