0% found this document useful (0 votes)
16 views24 pages

Module - 4

This document covers the concepts of functions and modular programming in C, including function definition, declaration, invocation, and the differences between call by value and call by reference. It explains the advantages of using functions, provides examples of function usage, and discusses recursive and iterative functions. Additionally, it highlights the importance of parameter passing and includes code snippets to illustrate these concepts.

Uploaded by

ojsangwai17
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)
16 views24 pages

Module - 4

This document covers the concepts of functions and modular programming in C, including function definition, declaration, invocation, and the differences between call by value and call by reference. It explains the advantages of using functions, provides examples of function usage, and discusses recursive and iterative functions. Additionally, it highlights the importance of parameter passing and includes code snippets to illustrate these concepts.

Uploaded by

ojsangwai17
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/ 24

MODULE- IV: FUNCTIONS AND MODULAR PROGRAMMING

F013-4:Apply function and modular programming for solving problems in C

Content

Function definition, declaration, invocation


Return - by value & by reference,
Recursive function and iterative functions.
WHAT IS C FUNCTION?

A large C program is divided into basic building blocks called C function. C function contains
set of instructions enclosed by “{ }” which performs specific task in a C program.
Example:
1.Developing a simple calculator program.

2.Sorting customer records by name or sorting transactions by date in financial software.

3.Monthly servicing of your motorbike


Advantages of Functions :

1.By using functions, we can avoid rewriting same logic/code again and again in a program.

2.We can call C functions any number of times in a program and from any place in a program.

3.We can track a large C program easily when it is divided into multiple functions.

4.Reusability is the main achievement of C functions.


Function Declaration, Definition, Syntax

A function consist of two parts:

Declaration: the function's name, return type, and parameters (if any)

Definition: the body of the function (code to be executed)

1.Declaration syntax: return_type function_name(parameter list);

Example:
2.Definition syntax:

return_type function_name(parameter list) {

Body of the function

Example:
Function Call

When we call any function control goes to function body and execute entire code. It is called
inside a program whenever it is required to call a function. It is only called by its name in the
main() function of a program.

Syntax Example

function-name(); add();

function-name(parameter/argument); add(a,b);

return value/ variable = function-name(parameter/argument); c=fun(a,b);


Example for Function without argument and return value
#include<stdio.h>
void sum();
void main()
{
printf("\nGoing to calculate the sum of two numbers:");
sum();
}
void sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
printf("The sum is %d",a+b);
}
Output:
Going to calculate the sum of two numbers:
Enter two numbers 10 24
The sum is 34
Example for Function with argument and without return value
#include<stdio.h>
void sum(int, int);
void main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
sum(a,b);
}
void sum(int a, int b)
{
printf("\nThe sum is %d",a+b);
}
Output:
Going to calculate the sum of two numbers:
Enter two numbers 10 24
The sum is 34
Example for Function with argument and with return value

#include<stdio.h>
int sum(int, int);
void main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
result = sum(a,b);
printf("\nThe sum is : %d",result); Output:
Going to calculate the sumof two numbers:
}
Enter two numbers 10 24
int sum(int a, int b)
The sum is 34
{
return a+b;
}
Calling and Called function in c
Parameter Passing
Actual parameters are the values that are passed to a function when it is called.

Formal parameters are the variables declared in the function header that are used to receive the values of
the actual parameters passed during function calls.
Call by value and Call by reference

Call by Value:In call by value ,the copy of actual parameter values are copied to
formal parameters and these formal parameters are used in called function.The changes
made on the formal parameters does not affect the values of actual parameters.
Call by Value Example: Swapping the values of the two variables

#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and b in
main
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The value of actual parameters do
not change by changing the formal parameters in call by value, a = 10, b = 20
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a = 20, b = 10
Call by value and Call by reference
Call by Reference:In Call by Reference,the memory location address of the
actual parameters is copied to formal parameters. This address is used to access
the memory locations of the actual parameters in called function.
Call by reference Example: Swapping the values of the two variables

#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and
b in main
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The values of actual parameters
do change in call by reference, a = 10, b = 20
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b); // Formal parameters, a = 20,
b = 10
}
Difference between call by value and call by reference
Recursive Function

Any function which calls itself is called recursive function.A recursive function performs the tasks by
dividing it into the subtasks.
Factorial Program using recursion in C
#include<stdio.h>

long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}

void main()
{
int number;
long fact;
printf("Enter a number: ");
scanf("%d", &number);

fact = factorial(number);
printf("Factorial of %d is %ld\n", number, fact);
return 0;
}
Iterative functions

Iterative function is a function that repeats lines of code until a set of conditions is met.

there are three types of iteration statements: for, while, and do-while.
Difference between recursive and iterative function

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy