0% found this document useful (0 votes)
11 views

Functions

The document discusses modular programming, emphasizing the importance of breaking complex programs into smaller, manageable functions to enhance reusability and ease of debugging. It outlines the structure of functions, including their definition, parameters, and return types, while also providing examples of various types of functions and how to pass arguments. Additionally, it covers the concepts of passing arrays to functions and the rules governing such operations.

Uploaded by

akul.kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Functions

The document discusses modular programming, emphasizing the importance of breaking complex programs into smaller, manageable functions to enhance reusability and ease of debugging. It outlines the structure of functions, including their definition, parameters, and return types, while also providing examples of various types of functions and how to pass arguments. Additionally, it covers the concepts of passing arrays to functions and the rules governing such operations.

Uploaded by

akul.kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

FUNCTIONS

Apr 6, 2025 Dept of I&CT 1


Modular Programming
Lengthier programs
• Prone to errors
• Tedious to locate and correct the errors

To overcome this
Programs are broken into a number of smaller logical components, each of
which serves a specific task.

Apr 6, 2025 Dept of I&CT


2
Modularization
Process of splitting the lengthier and complex programs into a number of
smaller units is called Modularization.

Programming with such an approach is called Modular programming

3
Apr 6, 2025 Dept of I&CT
Advantages of modularization
• Reusability

• Debugging is easier

• Build library

Apr 6, 2025 Dept of I&CT


4
Functions

A function is a set of instructions to carryout a particular task.

Using functions we can structure our programs in a more modular way.

Apr 6, 2025 Dept of I&CT 5


Functions

Standard functions
(library functions or built-in functions)

User-defined functions
Written by the user(programmer)

Apr 6, 2025 Dept of I&CT


6
Defining a Function
Name
• You should give functions descriptive names
• Same rules as variable names, generally
Return type
– Data type of the value returned to the part of the program that activated (called) the
function.

Apr 6, 2025 Dept of I&CT 7


Functions
Parameter list
• A list of variables that hold the values being passed to the function
Body
• Statements enclosed in curly braces that perform the function’s operations(tasks)

Apr 6, 2025 Dept of I&CT 8


The general form of a function definition

return_type
function_name(parameter_definition)
{
variable declaration;
statement1;
statement2;
.
.
.
return(value_computed);
}

Apr 6, 2025 Dept of I&CT


9
Functions: understanding

Return type Function


name Parameter List

void main (void)

}
{
printf("hello world\n”); Body

Apr 6, 2025 Dept of I&CT


10
Functions
Return type Function Parameter List
name
void DisplayMessage(void)

}
{ printf(" Hello from the function display
message” \n”);
} Body

void main()
{ printf(“Hello from main”;
DisplayMessage(); // FUNCTION CALL
printf(“Back in function main again.\n”;
}

Apr 6, 2025 Dept of I&CT 11


Arguments and parameters
Both arguments and parameters are variables used in a program &
function.
Variables used in the function reference or function call are called
arguments. These are written within the parenthesis followed by the
name of the function. They are also called actual parameters.
Variables used in function definition are called parameters, They are also
referred to as formal parameters.

Apr 6, 2025 Dept12


of I&CT
To be solved …Functions
Write appropriate functions to
1. Find the factorial of a number ‘n’.
2. Reverse a number ‘n’.
3. Check whether the number ‘n’ is a palindrome.
4. Generate the Fibonacci series for given limit ‘n’.
5. Check whether the number ‘n’ is prime.
6. Generate the prime series using the function written for prime check,
for a given limit.

Apr 6, 2025 Dept of I&CT 13


To be solved … Functions
Factorial of a given number ‘n’
long factFn(int); long factFn(int num) //factorial
//prototype calculation
{
void main() { int i, fact=1;
int n;
long f; for (i=1; i<=num; i++)
printf("Enter the number to fact=fact * i;
evaluate its factorial:";
Scanf(“%d”, &n); return (fact); //returning the
f =factFn(n); //function call factorial
printf("\nFact of %l is %l”, }
n, f);
}
Apr 6, 2025 Dept of I&CT 14
To be solved … Functions
Fibonacci series generation for limit ‘n’
void fibFn(int lim) { //fib
void fibFn(int); //prototype
generation
int i, two;
void main() { if (lim<=0)
int n; printf("<<"The limit should
printf("Enter the limit for be +ive.\n";
Fibonacci numbers to be else {
computed?) "; printf("\nFibonacci nos\n“);
scanf(“%d”, &n); int next =1, current = 0;
fibFn(n); //function call for (i=1; i<=lim; i++) {
} two = current + next;
current = next;
next = two;
}
}
}
Apr 6, 2025 Dept of I&CT 15
Functions- points to note
1. The parameter list must be separated by commas.
dispChar( int n, char c);
2. The parameter names need to be the same in the prototype declaration and the function
definition.
3. The types must match the types of parameters in the function definition, in number
and order.
4. Use of parameter names in the declaration is optional.
void dispChar ( int , char);//proto-type

Apr 6, 2025 Dept of I&CT 16


Functions- points to note

5. If the function has no formal parameters, the list is written as (void) but it is
optional..
6. The return type is optional, when the function returns int type data.
7. The return type must be void if no value is returned.
8. When the declared types do not match with the types in the function definition,
compiler will produce error.

Apr 6, 2025 Dept of I&CT 17


Functions- Categories

1. Functions with no arguments and no return values.

2. Functions with arguments and no return values.

3. Functions with arguments and one return value.

4. Functions with no arguments but return a value.

5. Functions that return multiple values (later).

Apr 6, 2025 Dept of I&CT 18


Funtion with No Arguments/parameters & No return values

void dispPattern(void )
{ int i;
for (i=1;i<=20 ; i++)
printf("*”;}

void dispPattern(void); // prototype


void main()
{ printf("\nfn to display a line of stars\n”);
dispPattern();
}

Apr 6, 2025 Dept of I&CT 19


Function with Arguments/parameters & No return values

void dispPattern(char c )
{ int i;
for (i=1;i<=20 ; i++)
printf(“%c”, c);
printf("\n”;}

void dispPattern(char ch); // prototype


void main()
{ printf("“\nfn to display a line of patterns\n”;
dispPattern(‘#’);
dispPattern(‘*’);
dispPattern(‘@’); }

Apr 6, 2025 Dept of I&CT 20


Function with Arguments/parameters & One return value

Addition program

Apr 6, 2025 Dept of I&CT 21


Function with No Arguments but A return value
Read a number from function and return it

Apr 6, 2025 Dept of I&CT 22


Function that return multiple values

Apr 6, 2025 Dept of I&CT 23


Pass by value
swap

04/06/2025 Department of I&CT 24


Pass by Reference

04/06/2025 Department of I&CT 25


Pass by Reference
• We use two operators to achieve parameter passing by reference.
 address operator ‘&’ and
 indirection operator ‘*’
• Here
- The actual arguments (arguments in function call) are input arguments and
- The formal arguments (those in function definition) are output arguments.
How its working!
 In the function call when we pass the actual values to function, we pass the address of
locations where the values are stored in the memory (thus the & is called address
operator).
 The operator * is known as indirection operator because it gives an indirect reference to
a variable through its address stored in it.

04/06/2025 Department of I&CT 26


Passing 1D-Array to Function
Rules to pass a 1D- array to a function
 The function must be called by passing only the array name.
 In the function definition, we must indicate that the array has one-dimension by
including one set of brackets.

 The size of the dimension is not mandatory.


 The prototype declaration should be like function header.

Apr 6, 2025 Dept of I&CT 27


Passing 1D-Array to Function
Program to pass an array and find sum of
elements

04/06/2025 Department of I&CT 28


Passing 2D-Array to Function
Rules to pass a 2D- array to a function
 The function must be called by passing only the array name.
 In the function definition, we must indicate that the array has two-dimensions by
including two set of brackets.

 The size of the second dimension must be specified.


 The prototype declaration should be like function header.

Apr 6, 2025 Dept of I&CT 29


Passing 2D-Array to Function
int fn2d(int x[ ][10], int m, int n)
{ int i,j,sum=0;
for(i=0; i<m; i++)
for(j=0; j<n; j++)
sum+=x[i][j];
return(sum);
}
void main() {
int i, j, n, a[10][10], m;
int fn2d(int [][], int, int);
//read dimensions & matrix
printf("Sum of elements of 2D array is %d”, fn2d(a,
m, n));
04/06/2025 } Department of I&CT 30

You might also like