KEMBAR78
L03 - Functions | PDF | Parameter (Computer Programming) | Control Flow
0% found this document useful (0 votes)
19 views26 pages

L03 - Functions

The document provides an overview of functions in C programming, including their role, types (predefined and user-defined), and methods for passing data (call by value and call by reference). It also discusses recursion, its types (direct and indirect), and the advantages and disadvantages of using recursive functions. Additionally, it covers function declaration, definition, and calling, along with memory allocation for recursive functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views26 pages

L03 - Functions

The document provides an overview of functions in C programming, including their role, types (predefined and user-defined), and methods for passing data (call by value and call by reference). It also discusses recursion, its types (direct and indirect), and the advantages and disadvantages of using recursive functions. Additionally, it covers function declaration, definition, and calling, along with memory allocation for recursive functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

C Programming

Functions

"Defined path for a Stable career in Core Technologies“


hello@etechprowess.com
INDEX
❑ Functions ❑Types of recursion
❑ Role of Functions in C ❑Direct Recursion
❑Indirect Recursion
❑ Types of Functions
❑Predefined/Library Functions
❑ Memory allocation of Recursive Functions
❑User-defined Functions ❑ Advantages & Disadvantages
❑ User-Defined Functions
❑ Passing data to Functions
❑ Call by value
❑ Call by Reference
❑ Recursion

🌐 etechprowess.com
Functions
• A function is a block of code that performs a specific task.
• Every C program has at least one function, which is main(), and all the
most trivial programs can define additional functions
• Suppose, you need to create a program to create a circle and color it. You
can create two functions to solve this problem:
• create a circle function
• create a color function
• Dividing a complex problem into smaller chunks makes our program easy
to understand and reuse.
Role of functions in C
• Functions are used because of following reasons –
• To improve the readability of code.
• Improves the reusability of the code, same function can be used in any program
rather than writing the same code from scratch.
• Debugging of the code would be easier if you use functions, as errors are easy to
be traced.
• Reduces the size of the code, duplicate set of statements are replaced by function
calls.
Types of Functions
• Depending on whether a function is defined by the user or already
included in C compilers, there are two types of functions in C
programming
Types of Functions
• Types of functions
• Predefined standard library functions:
• Standard library functions are also known as built-in functions.
• Functions such as puts(), gets(), printf(), scanf(), etc are standard library
functions.
• These functions are already defined in header files (files with .h extensions are
called header files such as stdio.h)
• User Defined functions:
• A function created by user is known as user defined function.
• It has the functionality/logic as required by a programmer.
Limitations of Library function
• All predefined function are contained limited task only that is for what
purpose function is designed for same purpose it should be used.
• As a programmer we do not having any controls on predefined function
implementation part is there in machine readable format.
• In implementation whenever a predefined function is not supporting user
requirement then go for user defined function.
Function Declaration
• Syntax of a function
• return_type function_name (argument_list) ;
• A function declaration tells the compiler about a function's name, return
type, and parameters.
• The actual body of the function can be defined separately.
• If function is defined before main(), then function declaration can be
avoided.
Function Definition
• Syntax of a function
• return_type function_name (argument_list)
{
Set of statements – Block of code
}
• return_type: Return type can be of any data type such as int, double, char, void,
short etc.
• function_name: It can be anything, however it is advised to have a meaningful
name for the functions so that it would be easy to understand the purpose of
function just by seeing it’s name.
• argument list: Argument list contains variables names along with their data types.
These arguments are kind of inputs for the function.
• Block of code: Set of C statements, which will be executed whenever a call will be
made to the function
Function Calling
• When any function is called, control goes to function body and executes
entire code.
• For calling any function just write name of function and if any parameter
is required then pass parameter.
• Syntax of a function
• function_name (argument_list) ;
• Function calling is also known as Invoking a Function
• After defining a function, it needs to be invoked for execution.

• Note: At the time of function calling function must be terminated with ';'.
Passing data to Functions
• There are two methods to pass the data into the function in C language,
i.e., call by value and call by reference
Call by Value
• In call by value method, the actual value of an argument is passed to the
formal parameter of the function.
• In this case, changes made to the parameter inside the function have no
effect on the argument.
• By default, C programming uses call by value to pass arguments.
Call by Value

Program to swap to numbers using Call by Value


• #include <stdio.h>
// Function Definition
void swap(int x, int y);// Function Declaration void swap(int x, int y) {
int temp;
int main () { temp = x; // save the value of x
int a = 100; x = y; // put y into x
int b = 200; y = temp; // put temp into y
printf("Before swap, value of a : %d\n", a ); return;
printf("Before swap, value of b : %d\n", b ); }
/* calling a function to swap the values */
swap(a, b);
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
return 0;
}
Call by Reference
• In call by reference method, the address of an argument is passed to 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.
• To pass a value by reference, argument pointers are passed to the
functions just like any other value
Call by Reference

Program to swap to numbers using Call by Reference


• #include <stdio.h>
// Function Definition
void swap(int x, int y);// Function Declaration void swap(int *x, int *y) {
int *temp;
int main () { *temp = *x; // save the value of x
int a = 100; *x = *y; // put y into x
int b = 200; *y = *temp; // put temp into y
printf("Before swap, value of a : %d\n", a );
printf("Before swap, value of b : %d\n", b ); }
/* calling a function to swap the values */
swap(&a, &b);
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
return 0;
}
return Statement
• A function in C can be called either with arguments or without arguments.
• These function may or may not return values to the calling functions.
• All C functions can be called either with arguments or without arguments.
• Also, they may or may not return any values
Different ways to create User-defined Functions
• Functions can be categorized as:
• Function with no argument and no return value
• Function with arguments but no return value
• Function with no arguments but returns a value
• Function with arguments and return value
Recursion
• Recursion is the process of repeating items in a self-similar way.
Recursive Functions
• In programming languages, if a program allows you to call a function
inside the same function, then it is called a recursive call of the function.
• Syntax
• void recursion() {
recursion(); /* function calls itself */
}

int main() {
recursion();
}
• Recursive functions are very useful to solve many mathematical problems,
such as calculating the factorial of a number, generating Fibonacci series,
etc.
• Note : But while using recursion, programmers need to be careful to define an exit condition from the
function, otherwise it will go into an infinite loop.
Types of Recursion
• There are two types of recursion:
• Direct Recursion
• Indirect Recursion
Direct Recursion
• If a function calls itself directly then the function is known as direct
recursive function.
• Example:- Direct Recursive function
• int fib(int num)
{
if (num==1 || num==2)
return 1;
else
return (fib(num-1)+fib(num-2));
}
• fib() function is direct recursive, because the statements inside the fib() function
calls the fib() function directly.
Indirect Recursion
• A function that does not call itself directly then function is known as an
indirect recursive function.
• Example:- Indirect Recursive function
• int first_function(int num) {
if (num<=2)
return 2;
else
return new_function(num);
}
int new_function(int num) {
return first_function(num);
}
• Here, first_function() calls the new_function(), which is indeed a new function.
Then this new_function() calls the first_function(). So, it is an indirect recursive
function.
Recursion
• Program to find factorial of a given int factorial(int num)
{
number if (num==0)
• #include <stdio.h> {
int factorial(int); return 0;
int main() }
else if (num==1)
{ {
int num=5,fact; return 1;
printf("Factorial of a number using }
recursion!\n\n"); else
fact = factorial(num); {
printf("Factorial of %d is: return num*factorial(num-1);
%d\n",num,fact); }
} }
Memory allocation of Recursive Functions
• Each recursive function call creates a copy of it in memory.
• When some data is returned from the recursive call then the memory releases the
copy.
• All variables, arguments and other things of function get stored in the stack. And for
each recursive call, a separate stack is maintained.
• int print(int num)
{
if(num == 0)
return 0;
else
{
printf("Value is: %d",num);
return print(num-1); // recursive call
}
}
• Let num=4.
• At first, all stacks are maintained.
• If the value of num reaches 0 then the stacks will be deleted one by one by returning 0 to its calling
stack.
Advantages & Disadvantages of Recursion
• Advantages of Recursion:
• Makes the program elegant.
• It adds clarity to the program code and also reduces the time to write the code.
• Reduces time complexity.
• Disadvantages of Recursion:
• It is slower than non recursive programs due to the overhead of maintaining the
stack.
• It requires more memory for the stack.
• For better performance, use loops instead of recursion. Because recursion is
slower.
THANK YOU

eTECH Prowess

Phone:+91 7676651416, 080 4890 919

Email:hello@etechprowess.com

http://etechprowess.com

You might also like