0% found this document useful (0 votes)
13 views14 pages

Lesson 9

Uploaded by

soumyapanda0078
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)
13 views14 pages

Lesson 9

Uploaded by

soumyapanda0078
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/ 14

Function

Monolithic Vs Modular Programming:


1. Monolithic Programming indicates the program which contains a single function for the
large program.
2. Modular programming help the programmer to divide the whole program into different
modules and each module is separately developed and tested. Then the linker will link all
these modules to form the complete program.
3. On the other hand monolithic programming will not divide the program and it is a
single thread of execution. When the program size increases it leads inconvenience and
difficult to maintain.
Disadvantages of monolithic programming: 1. Difficult to check error on large programs.
2. Difficult to maintain. 3. Code can be specific to a particular problem. i.e. it can not be
reused.
Advantage of modular programming: 1. Modular program are easier to code and debug.
2. Reduces the programming size. 3. Code can be reused in other programs. 4. Problem
can be isolated to specific module so easier to find the error and correct it.
User Define Functions Vs Standard Function
User Define Function:
A function that is declared, called and defined by the user is called user
define function. Every user define function has three parts as:
1. Prototype or Declaration
2. Calling
3. Definition
Standard Function:
The C standard library is a standardized collection of header files and library
routines used to implement common operations, such as input/output and
character string handling. Unlike other languages (such as COBOL,
FORTRAN, and PL/I) C does not include built in keywords for these tasks, so
nearly all C programs rely on the standard library to function.
FUNCTION
A function is a group of statements that together performs a task. Every C
program has at least one function, which is main(), and all the most trivial
programs can define additional functions.
Function Declaration OR Function Prototype
1. It is also known as function prototype .
2. It informs the computer about the three things
a) Name of the function
b) Number and type of arguments received by the function.
c) Type of value return by the function
Syntax :
return_type function_name (type1 arg1 , type2 arg2,…);
OR
return_type function_name (type1, type2,…);
3. Calling function need information about called function .If called function is place
before calling function then the declaration is not needed.
Function Definition:
1. It consists of code description and code of a function .
It consists of two parts
a) Function header
b) Function body
Function definition tells what are the I/O function and what is going to do.

Syntax:
return_type function_name (type1 arg1 , type2 arg2)
{
local variable; statements ; return (expression);
}
2. Function definition can be placed any where in the program but
generally placed after the main function .
3. Local variable declared inside the function is local to that function. It
cannot be used anywhere in the program and its existence is only
within the function.
4. Function definition cannot be nested.
5. Return type denote the type of value that function will return and
return type is optional if omitted it is assumed to be integer by default.
Write a program to add two numbers using function
#include <stdio.h>
#include <conio.h>
int sum(int , int); //function declaration or prototype
int main()
{ int num1,num2,total; //variable declaration
clrscr();
printf("Enter the two number ");
scanf("%d %d",&num1,&num2); //getting two number as input from user
total=sum(num1,num2); //calling the function //The total value (returned by the function) is stored in total
variable.
Output
printf("The sum of these numbers :%d", total); //display the total value Enter the two numbers 45
getch(); 75
return 0; The sum of these numbers :120
}
int sum(int a, int b) //defining function based in declaration
{ int result=a+b; //find sum of two numbers //and result of sum stored in result variable
return result; //returning result
ACTUAL ARGUMENTS AND FORMAL
ARGUMENTS
Actual Arguments
1. Arguments which are mentioned in the function call are known as actual arguments.
2. These are the values which are actually sent to the function.
It can be written as constant, variable or an expression which return a value .
Example: funct (6,9) , funct ( a,b )

Formal Arguments
1. Arguments which are mentioned in function definition are called dummy or formal
arguments.
2. These arguments are used to just hold the value that are sent by calling function.
3. Formal arguments are like other local variables of the function which are created
when function call starts and destroyed when end function.
Example: return_type func(int x,int y)
Write a program to find GCD (Greatest Common Divisor)
of two numbers
#include<stdio.h>
int GCD(int,int);
void main()
{
int a,b,gcd;
printf(“Enter two numbers”);
scanf(“%d%d”,&a,&b);
gcd=GCD(a,b); // a,b are actual arguments
printf(“GCD of %d and %d is %d”,a,b,gcd);
}
int GCD(int x, int y) // x and y are formal arguments
{ Output:
Enter two numbers 21
if(y==0) return x; 35
else GCD of 21 and 35 is 7
return GCD(y,x%y);
}
PARAMETER PASSING TECHNIQUES
In C programming parameters can be passed in two ways:
1. call by value
2. call by reference
Call by value:
Here values of actual arguments are passed to the formal arguments
and operation is done in the formal arguments.
Since formal argument is photo copy of actual argument, any changes
made with the formal arguments do not affect the actual argument.
Changes made to the formal argument are local to the block of called
function, so when control comes back to calling function changes
made, vanish.
Program to explain a function with call by value.
#include<stdio.h>
void change(int num)
{
printf("Before adding value inside function num=%d \
n",num);
num=num+100;
printf("After adding value inside function num=%d \n", num); Output:
Before function call x=100
} Before adding value inside function
int main() { num=100
int x=100; After adding value inside function
printf("Before function call x=%d \n", x); num=200
change(x); //passing value in function After function call x=100
printf("After function call x=%d \n", x);
return 0; Note: Value of actual argument is not
} changed i.e. 100
Call by reference
• In call by reference, the address of the variable is passed into the function
call as the actual parameter. So formal arguments are the pointers to the
actual arguments.
• The value of the actual parameters can be modified by changing the
formal parameters since the address of the actual parameters is passed.
• In call by reference, the memory allocation is similar for both formal
parameters and actual parameters. All the operations in the function are
performed on the value stored at the address of the actual parameters,
and the modified value gets stored at the same address.

i.e. changes made to the formal argument reflects the actual argument.
Program to understand the concept of call by reference.

#include<stdio.h>
void change(int *num)
{
printf("Before adding value inside function num=%d \n",*num);

(*num) += 100;
printf("After adding value inside function num=%d \n", *num);
} Output:
int main() { Before function call x=100
int x=100;
Before adding value inside function
printf("Before function call x=%d \n", x);
num=100
change(&x); //passing reference in function
printf("After function call x=%d \n", x); After adding value inside function
return 0; num=200
} After function call x=200
int main()
{ while(true)
{ printf("RABBIT"); break; }
return 0; }
A) RABBIT
B) RABBIT is printed unlimited number of times.
C) No output
D) Compiler error.

int main()
{ int a=5;
while(a >= 3);
{ printf("RABBIT\n"); break; }
printf("GREEN");
return 0; }
A) GREEN
B) RABBIT GREEN
C) RABBIT is printed infinite times
D) None of the above

int main()
{ int a=32;
do
{ printf("%d ", a);
a++; }
while(a <= 30);
return 0; }
A) 32
B) 33
C) 30
D) No Output
#include <stdio.h> int main()
int main() {
{
int i = 1024; int n;
for (; i; i >>= 1) for (n = 9; n!=0; n--)
printf("GeeksQuiz"); printf("n = %d", n--);
return 0;
} return 0;
}
int main() {
int i;
if (printf("0"))
i = 3;
else
i = 5;
printf("%d", i);
return 0;
}

You might also like