Fundamentals of
Programming Languages
Presented By :
Prof. Supriya Jadhav-Deshmukh
User Defined Functions: Need for User-defined
Functions, A Multi-Function Program, Elements of
UNIT-V User defined Functions, Definition of Functions, Return
Values and their Types, Function Calls, Function
Declaration,
Category of Functions: No Arguments and no Return
User Defined Values, Arguments but No Return Values, Arguments
with Return values, No Arguments but Returns a Value,
Functions Functions that Return Multiple Values, Nesting of
Functions, Recursion
Structures : What is a Structure? Structure Type
Declarations, Structure Declarations, Referencing
Structure Members, Referencing Whole Structures,
Initialization of Structures
• We have used function in every program. However they
have been primarily limited to three functions, namely,
main, printf and scanf.
• In this unit we shall consider in detail :
Introduction
to Function o How a function is designed?
o How a function is integrated into program?
o How two or more functions are put together?
o How they communicate to one another.
• C Function can be classified into two
categories :
Introduction 1. Library Functions : printf and scanf belongs to the
to Function category library functions.
2. User Define Function : main is belons to user
defined function.
• A function is a block of code which only
runs when it is called.
• You can pass data, known as parameters, into a
function.
What is
• Functions are used to perform certain actions,
Function? and they are important for reusing code: Define
the code once, and use it many times.
• A user-defined function is a function that the
user develops and writes themselves, as
opposed to a library function, which does not
need to be written by the user.
NEED FOR USER-DEFINED FUNCTIONS
• User-defined functions (UDFs) are a key tool in programming
languages that allow developers to create custom functions that
can be reused throughout a program.
• Simplify complex tasks: UDFs can break down complex tasks into
smaller, more manageable components.
• Automate repetitive operations: UDFs can automate routines that
require the same set of operations to be performed repeatedly.
• Improve code maintainability: UDFs can be reused throughout a
program, making code easier to maintain and debug.
NEED FOR USER-DEFINED FUNCTIONS
Cont...
• Improve code readability: UDFs can help developers avoid
repetition and make their code more readable.
• Improve code efficiency: UDFs can help developers make
their code more efficient.
• Extend built-in functions: UDFs can be used to extend built-
in functions.
• Encapsulate calculations: UDFs can be used to encapsulate
calculations that are standard for an organization.
MULTI FUNCTION PROGRAM
• a program that uses multiple functions, which are blocks of code that perform specific
tasks in a program.
• Functions are the basic building blocks of a C program, and they can be called multiple
times to perform the same task with different arguments.
• :
ELEMENTS OF USER-DEFINED FUNCTIONS
• Function declaration: As like variable
declaration, program should declare a function so that is
to be used later in the program is known as function
declaration.
• Function definition: Function definition is an
independent program module that is specially written to
implement the requirements of the function.
• Function call: In order to use this function we need to
invoke it at a required place in the program. This is
known as function call.
FUNCTION DECLARATION
• Like variable, all functions in the C program must be declare, before they are
invoked.
• A function declaration is also known as function prototype.
• Function declaration consist of Four Parts :
o Function Type(Return type)
o Function Name
o Parameter List
o Terminating Semicolon.
FUNCTION DECLARATION Cont..
• General Format :
Function-type Function-name (parameter list);
For Example : - Multiplication Function is Declare as ….
int mul (int m, int n); /* Function prototype */
DEFINITION OF FUNCTIONS
• A function definition, is also known as function implementation
o Function Name
o Function Type
o List of Parameters
o Local variable declarations
o Function statements
o A return statement.
DEFINITION OF FUNCTIONS Cont...
• All above six elements are grouped into two parts :
o Function Header(First three elements)
o Function Body(Second three elements)
o General Format
DEFINITION OF FUNCTIONS Cont...
o Function Header(First three elements)
Function Name
Function Type
List of Parameters
Function Header
o Name and type :
The function specifies the type of value (Float or Double). If
the return type not explicitly specified the by default it is
integer.
If the function not returning anything, the we need to specify
return type as void.
Function Header Cont...
o Formal Parameter list
Formal parameter are input data to the function to carry out the specified task.
Since they represent actual input values, they are often referred to as formal
parameter.
Function Header Cont...
DEFINITION OF FUNCTIONS Cont...
o Function Body(Second three elements)
Local variable declarations – that specifies the variable
needed by the function.
Function statements – that perform task of the function.
A return statement – returns the value evaluated by the
function.
Function Body Cont...
Example :
Function Body Cont...
Return values and their types :
o The return statement can take one of the following forms:
return;
or
return (expression);
The first 'plain' return does not return any value it acts much as
closing brace({) of the function.
The second form of the return with an expression returns the
value of expression.
Function Body Cont...
Return values and their types Cont...:
o For Example : -
int mul(intx, int y)
int p;
P = x * y;
return(p);
It will returns the value of p which is product of the values x and y.
Last two statements can be combined as
return (x * y)
Function Calls
A function can be called by simply using the function name
followed by list of actual parameters(or arguments), if any
enclosed in parenthesis.
o For Example : -
main ()
int y;
y = mul(10,5);
Printf("%d\n", y);
}
Function Calls
A function can be called by simply using the function
name followed by list of actual parameters(or
arguments), if any enclosed in parenthesis.
CATEGORY OF FUNCTIONS
A function, depending on whether arguments are present or
not and whether a value is returned or not, may belong to
one of the following categories.
Category 1: Function with no arguments and no return
Values.
Category 2: Function with arguments and return values.
Category 3: Function with arguments and no return values.
Category 4: Function with no arguments and return values.
Category 5: Function that return multiple values
Function with no arguments and no return Values
The function takes no inputs (arguments) and does not return any
value. It typically performs an action like printing something.
Function with arguments and return values
The function takes inputs (arguments) and returns a value after
processing them.
Function with arguments and no return values
This function accepts arguments but does not return a value.
Function with no arguments and return values
The function takes no inputs but returns a value.
Function that returns multiple values
C does not support directly returning multiple values.
However, we can use pointers or structures to achieve this.
• int main() {
• #include <stdio.h> • int x = 5, y = 6;
• struct Results res = calculate(x, y); // Calling the function
• printf("Sum: %d, Product: %d\n", res.sum, res.product);
• // Structure declaration • return 0;
• struct Results { • }
• int sum;
• // Function definition
• int product;
• struct Results calculate(int a, int b) {
• }; • struct Results res;
• res.sum = a + b;
• res.product = a * b;
• // Function declaration
• return res;
• struct Results calculate(int a, int b);
• }
NESTING OF FUNCTIONS
A called function can call another function.
A function can call another function is called nesting of
function
RECURSION OF FUNCTIONS
When a called function in turn calls another function a
process of “chaining” occurs.
Recursion is a special case of this process, where a
function calls itself.
STRUCTURES
Arrays can be used to represent a group of data items that are
belongs to the same type.
Arrays are used to store large set of data and manipulate them.
disadvantage of arrays is that all the elements stored in an array are
to be of the same data type.
If we need to use a collection of different data type items it is not
possible using an array.
When we require using a collection of different data items of
different data types we can use a structure
STRUCTURES
A structure in C is a user-defined data type that groups related variables of different data types
under one name. It is used to represent a record.
When we require using a collection of different data items of different data types we can use a
structure
Structure is a method of packing data of different types.
A structure is a convenient method of handling a group of related data items of different data
types.
Syntax :
struct tag_name
{
data type member1;
data type member2;
…
…
};
STRUCTURES Cont...
struct Person is the structure type.
p1 is a structure variable.
Structure Type Declarations
A structure type declaration defines a new data type using
the struct keyword. The structure defines a blueprint, but no
memory is allocated until a structure variable is created.
Syntax Example
Referencing Whole Structures
You can copy an entire structure into another of the same type.
The entire p1 structure is copied to p2.
Initialization of Structures
You can initialize structure members at the time of declaration.
Structure members are initialized directly while declaring s1.