You are on page 1of 24

CHARUTAR VIDYA MANDAL UNIVERSITY

A D PATEL I NSTITUTE O F T ECHNOLOGY

FUNCTIONS & RECURSION

P REPARED BY:
P ROF. A XIT K ACHHIA
CP D EPARTMENT
Concepts of User Defined Functions
➢Overview:
➢A function is a block of code that can be used to perform a specific action.
➢C allows programmers to write their own functions, also known as user-
defined functions.
➢A user-defined function has three main components that are function
declarations, function definition and function call.
➢Further functions can be called by call by value or call by reference.
➢Functions need to be written once and can be called as many times as
required inside the program, which increases reusability in code and makes
code more readable and easy to test, debug, and maintain the code.

2
Concepts of User Defined Functions
➢User-defined functions in C language are defined by the programmer to
perform specific operations.
➢They are also known as “tailor-made functions” which are built only to
satisfy the given condition.
➢We can classify functions into call by value or call by reference.
➢A function is a block of code that can be used to perform a specific action. C
allows users to create their own functions called user-defined functions.
➢A user-defined function can perform specific actions defined by users based
on given inputs and deliver the required output.

3
Elements of User-defined Functions
➢Function divides our program into several independent sub-tasks, making
our code easier to test and debug than one extensive program.
➢The function also helps us avoid duplication of efforts in our code as we
don't have to write the same code again, reducing the time to write code.

4
Elements of User-defined Functions
➢Functions in the C language have three parts.
➢1. Function declaration
➢A function declaration is simply a prototype of our function. Function
declaration contains function name, return type, and parameters but does
not contain the body of the function.
➢Function declaration gives information to the compiler about the user-
defined function that may be used in the later part of the code.

5
Elements of User-defined Functions
➢Syntax of function declaration
➢A function declaration has three main components: return type, function
name, and parameters. The function name is used to identify the function
uniquely in code. Function parameters are included in the declaration to
identify the number and types of inputs that the function accepts.
returnType functionName(type1 parameterName1, type2parameterName2,
...);

➢It is not compulsory to mention parameter name in declaration hence we


can also use
returnType functionName(type1 , type2, ...);

6
Elements of User-defined Functions
➢Return type: The type of data returned from the function is called return
type. A function may not return any output, in that case, we use void as the
return type. In function declaration return type is mentioned before the
name of the function.
➢Function name: Function name is a unique name that can be used to
identify our function in the program.
➢ Function names are used to create function calls, which is why they are
unique identifiers for compilers. A valid function name in C can contain
letters, underscore, and digits; the first letter must not be a digit.

7
Elements of User-defined Functions
➢For Example,
thisIsAfunction(); // valid
_getMaximum(); // valid
!getMinimum(); // invalid, symbols except underscore are not
allowed
getPowerOf2(); // valid
2Root(); // invalid function name, must not start with a number

8
Elements of User-defined Functions
➢Parameter list: Parameters required by the function are also defined inside
the declaration to tell the compiler number of arguments required by the
function along with their data types.
➢Semicolon: Semicolon indicates the termination of a function declaration.

9
Elements of User-defined Functions
➢Functions in the C language have three parts.
➢2. Function Definition
➢Function definition contains the actual block of code that is executed once
the function is called. A function definition has four components:
1.Return type
2.Function name
3.Function parameters
4.Body of function

10
Elements of User-defined Functions
➢Function body contains a collection of instructions that define what a
function does. If the function returns any value, we use the keyword return to
return the value from the function.
➢Syntax of function definition
returnType functionName(functionParameters...)
{
// function body
}

11
Elements of User-defined Functions
➢We can also give default values to function parameters
int getRectangleArea(int length = 10, int breadth = 5)
{
return length * breadth;
}

➢If getRectangleArea() is called, default values will be assigned to function


parameters and 50 will be returned as function output.

12
Elements of User-defined Functions
➢3. Calling User-defined Functions
➢To transfer the control to a user-defined function, we need to call the
function. A function can be called using a function name followed by round
brackets. We can pass arguments to function inside brackets, if any.

13
Elements of User-defined Functions
➢Syntax for function call
➢A function call includes two things that are function name and function
arguments. The function name is used to identify the function getting called
and arguments are passed inside parentheses that act as input for the called
function. Arguments must be passed in the exact order in which they are
present in the function declaration.

functionName(functionArgument1, functionArgument2, ...);

14
Elements of User-defined Functions
Creating a Function Call
➢To call a function and calculate its output, we need to create a function call.
Whenever a function is called, control of the program is transferred to called
function, and the function body of the called function is executed.
➢For example, to call a function with name getReactangleArea accepting two
arguments length and breadth, the syntax will be..
➢int area = getReactangleArea(l, b);
➢here getReactangleArea(l, b) is function call and the output of the function
is returned in variable area.

15
Elements of User-defined Functions
There are two types of function calls.
1. Call by value
2. Call by reference

16
Elements of User-defined Functions
1. Call by value
➢In call by value method, we can not modify the value of the actual
parameter by the formal parameter.
➢In call by value, different memory is allocated for actual and formal
parameters since the value of the actual parameter is copied into the formal
parameter.
➢In call by value function, argument values are copied to function parameters
and we cannot modify the actual value of parameters.
➢Actual and formal arguments are created at the different memory location

17
Elements of User-defined Functions
2. Call by reference
•The value of the actual parameters can be modified by changing the formal
parameters since the address of the actual parameters is passed.
•An address of value is passed into the function.
•Changes made inside the function validate outside of the function also. The
values of the actual parameters do change by changing the formal
parameters.
•Actual and formal arguments are created at the same memory location

18
Recursion
➢Recursion is the technique of making a function call itself. This technique
provides a way to break complicated problems down into simple problems
which are easier to solve.
➢Recursion may be a bit difficult to understand. The best way to figure out
how it works is to experiment with it.
➢Recursion is a process in which a function calls itself directly or indirectly.
➢For Example:
int fun()
{
-------------
fun();
} 19
Recursion
➢Any function which calls itself is called recursive function, and such function
calls are called recursive calls.

➢For Example:
main()
{
printf(“This is an Example of Recursion\n”);
fun();
}

20
Recursion
➢When executed, this program will produce an o/p something like…….
➢This is an Example of Recursion
➢This is an Example of Recursion
➢This is an Example of Recursion
➢This is an Example of Recursion
➢This is an Ex

➢Execution is terminated abruptly; otherwise the execution will continue


indefinitely.

21
Recursion
➢Another useful example of a recursion is the evaluation of factorial of a
given number.
➢The factorial of a number n is expressed as a series of repetitive
multiplication:
➢Factorial of n = n(n-1)(n-2)………1

➢For example
➢Factorial of 4 = 4*3*2*1 = 24

22
Recursion
➢A function to evaluate factorial of n is as follows:
factorial (int n)
{
int fact;
if (n==1)
return 1;
else
fact = n*factorial(n-1)
return(fact);
}
23
Recursion

24

You might also like