Unit 5
User Defined Functions
5.1 User Defined Functions: Need for User-defined Functions,
5.2 A Multi-Function Program, Elements of User defined Functions,
5.3 Definition of Functions, Return Values and their Types, Function Calls, Function
Declaration,
5.4 Category of Functions: No Arguments and no Return Values, Arguments but No Return
Values, Arguments with Return values, No Arguments but Returns a Value, Functions that
Return Multiple Values
5.5Nesting of Functions, Recursion
5.6 Structures: What is a Structure? Structure Type Declarations
5.7 Structure Declarations, Referencing Structure Members
5.8 Referencing Whole Structures, Initialization of Structures.
A user-defined function is a type of function in C language that is defined by the user
himself to perform some specific task. It provides code reusability and modularity to our
program. User-defined functions are different from built-in functions as their working is
specified by the user and no header file is required for their usage.
5.1 User Defined Functions: Need for User-defined Functions
Advantages of User-Defined Functions
The advantages of using functions in the program are as follows:
One can avoid duplication of code in the programs by using functions. Code can be
written more quickly and be more readable as a result.
Code can be divided and conquered using functions. This process is known as Divide
and Conquer. It is difficult to write large amounts of code within the main function, as
well as testing and debugging. Our one task can be divided into several smaller sub-
tasks by using functions, thus reducing the overall complexity.
For example, when using pow, sqrt, etc. in C without knowing how it is implemented,
one can hide implementation details with functions.
With little to no modifications, functions developed in one program can be used in
another, reducing the development time.
Benefits of User-Defined Functions
There are several benefits to using user-defined functions in C programming, including:
Reusability: One of the main benefits of user-defined functions is that they can be reused in
different parts of a program. Instead of writing the same code multiple times, you can define
, a function and call it whenever you need to perform a specific task. It makes the code more
efficient and easier to maintain.
Modularity: User-defined functions promote modularity in a program by breaking it down
into smaller, manageable parts. Each function can perform a specific task, and the program as
a whole can be built by combining these functions. It makes it easier to understand the code
and debug any issues that may arise.
Simplified code: User-defined functions can simplify the code by abstracting away complex
logic into a single function. It makes the code easier to read and understand, reducing the
likelihood of errors and improving the overall quality of the program.
Better testing: By breaking down a program into smaller functions, it becomes easier to test
each function individually. It makes it easier to identify and isolate issues within the code,
making it easier to fix any bugs and improve the overall quality of the program.
Improved collaboration: User-defined functions can improve collaboration by making it
easier for multiple developers to work on different parts of a program simultaneously. By
breaking the program down into smaller functions, each developer can focus on their specific
task and work independently, making it easier to combine their work later.
5.2 A Multi-Function Program, Elements of User defined Functions
1.Function declaration.
2.Function definition.
3.Function call.
1. Function Declarations
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
return_type name_of_the_function (parameter_1, parameter_2);
The parameter name is not mandatory while declaring functions. We can also declare the
function without using the name of the data variables.
Example
int sum(int a, int b); // Function declaration with parameter names
int sum(int , int); // Function declaration without parameter names
User Defined Functions
5.1 User Defined Functions: Need for User-defined Functions,
5.2 A Multi-Function Program, Elements of User defined Functions,
5.3 Definition of Functions, Return Values and their Types, Function Calls, Function
Declaration,
5.4 Category of Functions: No Arguments and no Return Values, Arguments but No Return
Values, Arguments with Return values, No Arguments but Returns a Value, Functions that
Return Multiple Values
5.5Nesting of Functions, Recursion
5.6 Structures: What is a Structure? Structure Type Declarations
5.7 Structure Declarations, Referencing Structure Members
5.8 Referencing Whole Structures, Initialization of Structures.
A user-defined function is a type of function in C language that is defined by the user
himself to perform some specific task. It provides code reusability and modularity to our
program. User-defined functions are different from built-in functions as their working is
specified by the user and no header file is required for their usage.
5.1 User Defined Functions: Need for User-defined Functions
Advantages of User-Defined Functions
The advantages of using functions in the program are as follows:
One can avoid duplication of code in the programs by using functions. Code can be
written more quickly and be more readable as a result.
Code can be divided and conquered using functions. This process is known as Divide
and Conquer. It is difficult to write large amounts of code within the main function, as
well as testing and debugging. Our one task can be divided into several smaller sub-
tasks by using functions, thus reducing the overall complexity.
For example, when using pow, sqrt, etc. in C without knowing how it is implemented,
one can hide implementation details with functions.
With little to no modifications, functions developed in one program can be used in
another, reducing the development time.
Benefits of User-Defined Functions
There are several benefits to using user-defined functions in C programming, including:
Reusability: One of the main benefits of user-defined functions is that they can be reused in
different parts of a program. Instead of writing the same code multiple times, you can define
, a function and call it whenever you need to perform a specific task. It makes the code more
efficient and easier to maintain.
Modularity: User-defined functions promote modularity in a program by breaking it down
into smaller, manageable parts. Each function can perform a specific task, and the program as
a whole can be built by combining these functions. It makes it easier to understand the code
and debug any issues that may arise.
Simplified code: User-defined functions can simplify the code by abstracting away complex
logic into a single function. It makes the code easier to read and understand, reducing the
likelihood of errors and improving the overall quality of the program.
Better testing: By breaking down a program into smaller functions, it becomes easier to test
each function individually. It makes it easier to identify and isolate issues within the code,
making it easier to fix any bugs and improve the overall quality of the program.
Improved collaboration: User-defined functions can improve collaboration by making it
easier for multiple developers to work on different parts of a program simultaneously. By
breaking the program down into smaller functions, each developer can focus on their specific
task and work independently, making it easier to combine their work later.
5.2 A Multi-Function Program, Elements of User defined Functions
1.Function declaration.
2.Function definition.
3.Function call.
1. Function Declarations
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
return_type name_of_the_function (parameter_1, parameter_2);
The parameter name is not mandatory while declaring functions. We can also declare the
function without using the name of the data variables.
Example
int sum(int a, int b); // Function declaration with parameter names
int sum(int , int); // Function declaration without parameter names