0% found this document useful (0 votes)
3 views20 pages

Module 4 Chapter 1

The document discusses user-defined functions and recursion in programming, emphasizing their importance in modular programming to simplify complex tasks. It outlines the structure of functions, including function definition, declaration, and calls, along with examples such as calculating factorials and sums. Additionally, it highlights the benefits of breaking down programs into smaller, manageable modules for better debugging and maintenance.

Uploaded by

Shakthi Raj
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)
3 views20 pages

Module 4 Chapter 1

The document discusses user-defined functions and recursion in programming, emphasizing their importance in modular programming to simplify complex tasks. It outlines the structure of functions, including function definition, declaration, and calls, along with examples such as calculating factorials and sums. Additionally, it highlights the benefits of breaking down programs into smaller, manageable modules for better debugging and maintenance.

Uploaded by

Shakthi Raj
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

Functions

Department of
Computer Science & Engineering

[Link]
Module 4

User Defined Functions and Recursion.


Example Programs Finding factorial of positive
integers and Fibonacci series.

Department of Computer Science & Engineering [Link]


User Defined Functions

Functions
Thanks!

The functions that The functions


are not required that are to be
to be written by Library User Defined developed by the
us. Functions Functions user at the time
eg: printf(), of writing a
scanf(), sqrt()…. program. eg:
main().

Department of Computer Science & Engineering


[Link]
Need for User Defined Function

Every program must have a main() function to indicate where the program has to begin
its execution.
•If a program has only main(), it becomes too large and complex. As a result, the task
of debugging , testing and maintaining becomes difficult.
•If a program is divided into functional parts ,then each part may be independently
coded and later combined into a single unit. These independently coded programs are
called functions or subprograms.
•There are times when certain type of operations or calculations are repeated at many
points throughout a program. eg-factorial().
•Another requirement is to design a function that can be called and whenever used.
This saves both time and space.

Department of Computer Science & Engineering [Link]


Modular Programming
Modular programming is a “divide and conquer” approach applied to the design and
development of software systems.
It is defined as organizing a large program into small, independent program segments called
modules that are separately named and individually callable program units.
Characteristics:
•Each module should do only one thing.
•Communication between modules is allowed only by a calling module.
•A module can be called by one and only one higher module.
•No communication can take place directly between modules that do not have calling-called
relationship.
•All modules are designed as single-entry, single-exit systems using control structures.
Department of Computer Science & Engineering [Link]
Modular Programming
Top down modular programming using functions

Department of Computer Science & Engineering [Link]


Function

A function is a self contained block of code


that performs a particular task.
•Any function can call any other function.
• It can call itself.
•A ‘called function’ can also call another
function.
•A function can be called more than once.
This is called multi-function program.

Department of Computer Science & Engineering [Link]


User Defined Function

void add()
Function call
void main() {
{ int a,b, sum ;
add(); a=70;
} b=20;
sum=a+b;
printf(“Addition of two
numbers=%d”, sum);
}

Department of Computer Science & Engineering [Link]


Elements of User defined function
Function declaration Function definition
•Function definition
•Function call void add()
#include<stdio.h> {
•Function declaration void add(); int a,b, sum ;
void main() a=70;
{ b=20;
add(); sum=a+b;
} printf(“Addition of two
numbers =%d”, sum);
Function call }

Department of Computer Science & Engineering [Link]


Function Definition
A function definition, also known as function implementation shall include the
following elements:

1. Function name All the six elements are grouped into two parts

2. Function type Function header (first three


3. List of parameters elements)
4. Local variable
declarations Function body (second three
5. Function statements elements)
6. Return statement

Department of Computer Science & Engineering [Link]


General format of Function Definition

function_type function_name (parameter list)


Function Header
{ (3 parts)
local variable declaration;
executable statement1;
executable statement2;
… Function Body


return statement;
}

Department of Computer Science & Engineering [Link]


Function Header The parameter list declares the
variables that will receive the data sent
by the calling program.
The function name is They are often referred to as formal
any valid C identifier parameters.
The parameter list contains declaration
of variables separated by commas and
function_type function_name (parameter list) surrounded by parenthesis.
Ex:
int sum(inta,int b)
{
specifies the type of value(like int ,char, float or … Formal parameters
double) that the function is expected to return to }
the program calling the function.

Department of Computer Science & Engineering [Link]


Function Body

The function body enclosed in braces contains 3 parts:


1. Local declarations that specify the variables needed by the function.
2. Function statements that perform the task of the function.
3. A return statement that returns the value evaluated by the function.
int mul(int x,int y)
{
int p; /*local variable*/
p=x*y; /*computes product*/
return p; /*returns the result*/
}

Department of Computer Science & Engineering [Link]


Function calls

A function can be called by simply using the function name followed by a list of actual
parameters(arguments), if any, enclosed in parentheses.

main() Function call


{ int mul(int x, int y)
Int m,n,y; {
y=mul(m,n); return(x*y);
printf(“%d\n”,y); }
}

Department of Computer Science & Engineering [Link]


Function Declaration/ Function Prototype
Like variables, all functions in a C program must be declared, before they are invoked.
It Consists of four parts.
1. Function type(return type)
2. Function name
3. Parameter list
4. Terminating semicolon
Syntax: function-type function-name(parameter list);
It is similar to function header except the terminating semicolon.
Example:
int mul(int x,int y);
Department of Computer Science & Engineering [Link]
Program to add two numbers
#include<stdio.h> int add(int a,int b)
int add(inta,int b); {
void main() int c;
{ c=a+b;
int a,b,c; return c;
printf("Enter the value of a and b:"); }
scanf("%d%d",&a,&b);
c=add(a,b);
printf("\n Addition of %d and %d is
%d",a,b,c);
}

Department of Computer Science & Engineering [Link]


Locating Function Prototype
Function definition can be written after main(). In Function definition can be written before main(). In this
this function declaration is compulsory. function declaration is not compulsory.
#include<stdio.h> #include<stdio.h>
int add(inta,int b); int add(inta,int b)
void main() {

{


}

void main()
}
{
int add(inta,int b) …
{ …
… }

}

Department of Computer Science & Engineering [Link]


Factorial of a given number

#include<stdio.h> int factorial(int n)


int factorial(int n); {
void main() inti,f=1;
{ if(n==0)
int n; return 1;
printf("Enter the value of n:"); for(i=1;i<=n;i++)
scanf("%d",&n); f=f*i;
printf("factorial of %d is return f;
%d",n,factorial(n)); }
}
Department of Computer Science & Engineering [Link]
Output

Enter the value of n 4


Factorial of 4 is 24

Department of Computer Science & Engineering [Link]


Thank you

Department of Computer Science & Engineering [Link]

You might also like