Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

c Programming - Chapter 5 - Functions

This document provides an overview of functions in C programming, including their types, declaration, definition, and calling methods. It explains standard library functions, user-defined functions, and the advantages of using functions for modularity and code reusability. Additionally, it covers header files, how to create user-defined header files, and the importance of avoiding multiple inclusions.

Uploaded by

Phát Lê
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

c Programming - Chapter 5 - Functions

This document provides an overview of functions in C programming, including their types, declaration, definition, and calling methods. It explains standard library functions, user-defined functions, and the advantages of using functions for modularity and code reusability. Additionally, it covers header files, how to create user-defined header files, and the importance of avoiding multiple inclusions.

Uploaded by

Phát Lê
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

COMPUTER

PROGRAMMING 2
C/C++ LANGUAGE

M.E. LE THANH TUNG


Chapter 5: Functions
WEEK 5

M.E. LE THANH TUNG


INTRODUCTION
• 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
• User-defined functions
• The syntax of function can be divided into 3 aspects:
• Function Declaration
• Function Definition
• Function Calls
INTRODUCTION
• Standard library functions:
• The standard library functions are built-in functions in C programming.
• These functions are defined in header files.
• For example, the printf() is a standard library function to send formatted output
to the screen (display output on the screen). This function is defined in the
stdio.h header file. Hence, to use the printf()function, we need to include the
stdio.h header file using #include <stdio.h>.

• 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_type function_name (parameter_1, parameter_2);


■ return_type: the data type of the value the function returns.
■ function_name: the actual name of the function.
■ parameter_1: A parameter is like a placeholder. When a function
is invoked, you pass a value to the parameter.
5 . 1 F U N C T I O N D E C L A RAT I O N i n C
• Function Declaration:
• Example:

int sum (int a, int b);

■ Notice: A function in C must always be declared globally before calling it.


5.2 FUNCTION DEFINITION
• 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).
• 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.

return_type function_name (parameter_1, parameter_2)


{
// Body of function
}
5.2 FUNCTION DEFINITION
• Function Declaration:
• Example:

int heading (void)


{
return 0;
}

■ Notice: The code serves as both a function definition and a declaration.


5.2 FUNCTION DEFINITION
• Function return type:
• Function return type tells what type of value is returned after all function is
executed.
• When we don’t want to return a value, we can use the void data type.
• 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
(or the value in the variable represented by X) becomes the result of the
function.
■ With the void function, it is not necessary to use return statements.
• Only one value can be returned from a C function. To return multiple values,
5.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(a,b); //function call
printf("\nz = %d ",z);

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

• 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.
5 . 4 F U N C T I O N & L I B RA RY
• Header files:
• In C language, header files (.h) contain a set of predefined standard library functions.
• A header file contains:
• Function Declaration
• Macros
• Data Type Definitions
• There are 2 types of header files such as:
• Standard Library Header Files: These are pre-existing header files which are

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

preprocessor also searches in an implementation-defined manner, but one that is


normally used to include programmer-defined header files and typically
includes same directory as the file containing the directive (unless an absolute
path is given).
5 . 4 F U N C T I O N & L I B RA RY
• Create your own Header File in C:
• Instead of writing a large and complex code, we can create our own header files and include

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

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;
}
5 . 4 F U N C T I O N & L I B RA RY
#include <stdio.h>
#include “myheader.h”
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 . 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

in a conditional like below:

#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!

M.E. LE THANH TUNG

You might also like