0% found this document useful (0 votes)
12 views18 pages

SPC - Module Iv - I

Uploaded by

riddhi.jain0414
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)
12 views18 pages

SPC - Module Iv - I

Uploaded by

riddhi.jain0414
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/ 18

MODULE IV: FUNCTIONS

By: Dim ple Bohra


Functions
❖ What is Function ?
❖ Advantages of Functions
❖ Types of functions
❖ How function works ?
❖ Parameter Passing
❖ Return Statement
❖ Call by Value
❖ Call by Reference
❖ Recursive function
❖ How recursion works ?
❖ How to write recursive function ?
❖ Recursive Function Call
WHAT IS FUNCTION ?

❖ A function is a block of code that performs a specific task


❖ Functions are building blocks of any programming language
❖ It is a reusable block of code that makes a program easier to understand, test and can be easily
modified without changing the calling program
ADVANTAGES OF FUNCTIONS

❖ It provides modularity to your program's structure


❖ It makes your code reusable. You just have to call the function by its name to use it,
wherever required
❖ In case of large programs with thousands of code lines, debugging and editing becomes
easier if you use functions
❖ It makes the program more readable and easy to understand
TYPES OF FUNCTIONS
TYPES OF FUNCTIONS

Library functions
❖ Library functions are those functions which are already defined in C library
❖ Example: printf(), scanf(), strcat() etc.
❖ You just need to include appropriate header files to use these functions

User-defined functions:
❖ User Defined Functions are those functions which are defined by the user at the time of writing
program
❖ These functions are made for code reusability and for saving time and space
HOW FUNCTION WORKS

return_type function_name (argument list)


{
function body;
return_type function_name (argument list); }

Function Function
Declaration Definition

Function Call

function_name (argument list);


HOW FUNCTION WORKS
HOW FUNCTION WORKS

Function declaration: (Function Prototype)


❖ A function must be declared globally to tell the compiler about the function name, function
parameters, and return type
Function call:
❖ Function can be called from anywhere in the program
❖ The parameter list must not differ in function calling and function declaration
Function definition:
❖ It contains the actual statements which are to be executed
❖ The program control comes to function definition when the function is called
❖ Only one value can be returned from the function
HOW FUNCTION WORKS

void add(); // Function Declaration


int main()
{
add(); // Function Call
Example: Write a C Program return 0;
to perform addition of two }
numbers using function.
void add() // Function Definition
{
int n1,n2;
printf("\n Enter two numbers:");
scanf("%d %d", &n1, &n2);
printf("\n Addition of %d and %d = %d", n1, n2, n1+n2);
}
PARAMETER (ARGUMENT) PASSING

❖ The mechanism used to convey information to the


function is the argument
❖ A function's arguments are used to receive the
necessary values by the function call

Actual arguments:
The values that are passed to the called function from the
main function are known as Actual arguments
Formal arguments:
The variables declared in the function prototype or
definition are known as Formal arguments
PARAMETER (ARGUMENT) PASSING

void add(int a, int b); // Function Declaration


int main()
Formal
{ Arguments
int n1,n2;
Example: Write a C Program printf("\n Enter two numbers:");
to perform addition of two scanf("%d %d", &n1, &n2);
numbers using function.
add(n1, n2); // Function Call
return 0; Actual
} Arguments
void add(int n1, int n2) // Function Definition
{
Formal
printf("\n Addition of %d and %d = %d", a, b, a+b);
Arguments
}
RETURN STATEMENT

❖ The return statement is used to terminate the


execution of a function and transfer program
control back to the calling function
❖ A function may contain one or more return
statements
❖ A function can return only one value at a time
❖ Return type of a function and return value data
type should be same
PARAMETER (ARGUMENT) PASSING

Parameter passing can be done in two ways: (Types of function calls)


1. Call by value
2. Call by reference
CALL BY VALUE

❖ The value of the actual parameters is copied into the formal parameters
❖ The arguments in the function call (Actual arguments) are not modified by the change
in parameters of the called function (Formal arguments)
❖ Different memory is allocated for actual and formal parameters

a b a b
Actual Argument 10 20 10 20 Formal Argument

100 150 200 250

Local to main Local to function


CALL BY VALUE

Example: Write a C Program to swap two numbers

void swap(int , int); void swap(int a, int b)


int main() {
{ int temp;
int a = 10; temp = a;
int b = 20; a = b;
printf(“\n Before swapping the values in main”); b = temp;
printf(“ a = %d, b = %d ",a, b); printf(“\n After swapping values in function”);
swap(a, b); printf(“a = %d, b = %d“, a, b);
printf(“\n After swapping values in main); }
printf(“a = %d, b = %d ",a, b);
}
CALL BY REFERENCE

❖ The address of the variable is passed into the function call as the actual parameter
❖ The value of the actual parameters can be modified by changing the formal parameters since
the address of the actual parameters is passed
❖ Same memory is allocated for both formal parameters and actual parameters

a a
Actual Argument 10 100 Formal Argument

100 200
Normal Variable Pointer that stores the address
of actual argument
CALL BY REFERENCE

Example: Write a C Program to swap two numbers

void swap(int *, int *); void swap(int *a, int *b)


int main() {
{ int temp;
int a = 10; temp = *a;
int b = 20; *a = *b;
printf(“\n Before swapping the values in main”); *b = temp;
printf(“ a = %d, b = %d ",a, b); printf(“\n After swapping values in function”);
swap(&a, &b); printf(“a = %d, b = %d“, *a, *b);
printf(“\n After swapping values in main); }
printf(“a = %d, b = %d ",a, b);
}

You might also like