SlideShare a Scribd company logo
2
Most read
4
Most read
7
Most read
PPS
Unit – 6
Function
6.1 Introduction & Writing Functions
Similar to other languages C language also provides the facility of function. Function is
the block of code which is used to perform a specific task. In c language the complete
program is composed of function.
Functions are useful to divide c programs into smaller modules. Programmer can
invoked these modules anywhere inside c program for any number of times.
Functions are used to increase readability of the code. Size of program can be reduce
by using functions. By using function, programmer can divide complex tasks into
smaller manageable tasks and test them independently before using them together.
Functions of C language are defined with the type of function. The type of functions
indicates the data type of value which will return by function. In order to use function in
the program, initially programmer have to inform compiler about the function. This is
also called as defining a function.
In C programme all the function definition present outside the main function. All function
need to be declared and defined before use. Function declaration requires function
name, argument list, and return type.
Return Type Function name (Argument list)
{
Statement 1;
Statement 2;
…………...
Statement n;
}
6.2 Scope Of Variables Functions (Including Using Built In Libraries)
1. Library Functions
A function which is predefined in c language is called library function. Library function
is also called as built in function of C language. The definition of library function is
stored in respective header file. Library functions are used to perform dedicated
operation like taking input from user, displaying output, string handling operation, etc.
Library functions are readily available and programmer can directly use it without
writing any extra code. For example, printf () and scanf () are library function and their
definition is stored in stdio header file.
2. User Defined Functions
User define function is the block of code written by programmer to perform a particular
task. As compiler doesn’t have any idea about the user define function so programmer
has to define and declare these functions inside the program body. Programmer can
define these function outside the main function but declaration of user define function
should present in main function only. Whenever compiler executes function call
(function declaration) then compiler shift the flow of program execution to the definition
part of user define function.
Example
#include <stdio.h>
#include<conio.h>
int add (int x, int y)
{
int sum;
sum = x + y;
return (sum);
}
main ()
{
inta,b,c;
a = 15;
b = 25;
c = add(a,b);
printf ("n Addition is %d ", c);
}
Output:
Addition is 40
There are two ways to pass the parameters to the function
1. Parameter Passing by value
In this mechanism, the value of the parameter is passed while calling the function.
2. Parameter Passing by reference
In this mechanism, the address of the parameter is passed while calling the function.
6.3 Parameter Passing In Functions, Call By Value
Parameter Passing by Value
This is the default way of passing the parameters to the function. This is achieved by
passing the copy of data to the function. This mechanism is also called as call by value.
In case of parameter passing by value, the changes made to the formal arguments in the
called function have no effect on the values of actual arguments in the calling function.
This mechanism is used when programmer don't want to change the value of passed
parameters. When parameters are passed by value then functions in C create copies of
the passed in variables and do required processing on these copied variables.
Pass-by-value is implemented by actual data transfer so additional storage is required to
maintain the copies of passed parameters.
Example:
#include <stdio.h>
#include<conio.h>
/* function declaration goes here.*/
void swap( int p1, int p2 );
int main()
{
int a = 10;
int b = 20;
printf("Before: Value of a = %d and value of b = %dn", a, b );
swap( a, b );
printf("After: Value of a = %d and value of b = %dn", a, b );
getch();
}
void swap( int p1, int p2 )
{
int t;
t = p2;
p2 = p1;
p1 = t;
printf("Value of a (p1) = %d and value of b(p2) = %dn", p1, p2 );
}
Output :
Before: Value of a = 10 and value of b = 20
Value of a (p1) = 20 and value of b (p2) = 10
After: Value of a = 10 and value of b = 20
Note: In the above example the values of “a” and “b” remain unchanged before calling
swap function and after calling swap function.
Parameter Passing by Reference
This mechanism is used when programmer want a function to do the changes in passed
parameters and reflect those changes back to the calling function. This mechanism is
also called as call by reference. This is achieved by passing the address of variable to
the function and function body can directly work over the addresses. Advantage of pass
by reference is efficiency in both time and space. Whereas disadvantages are access to
formal parameters is slow and inadvertent and erroneous changes may be made to the
actual parameter.
Example:
#include <stdio.h>
#include<conio.h>
void swap( int *p1, int *p2 );
int main()
{
int a = 10;
int b = 20;
printf("Before: Value of a = %d and value of b = %dn", a, b );
swap(&a, &b );
printf("After: Value of a = %d and value of b = %dn", a, b );
}
void swap( int *p1, int *p2 )
{
int t;
t = *p2;
*p2 = *p1;
*p1 = t;
printf("Value of a (p1) = %d and value of b(p2) = %dn", *p1, *p2 );
}
Output :
Before: Value of a = 10 and value of b = 20
Value of a (p1) = 20 and value of b(p2) = 10
After: Value of a = 20 and value of b = 10
Note: In the above example the values of “a” and “b” are changes after calling swap
function.
6.4 Passing Arrays To Functions: Idea Of Call By Reference
Array is a data structure which stores the collection of similar types of element in
consecutive memory locations. Indexing of array always start with ‘0’ where as non-
graphical variable ‘0’ indicates the end of array. Syntax for declaring array is
data type array name[Maximum size];
 Data types are used to define type of element in an array. Data types are also
useful in finding the size of total memory locations allocated for the array.
 All the rules of defining name of variable are also applicable for the array name.
 Maximum size indicates the total number of maximum elements that array can
hold.
 Total memory allocated for the array is equal to the memory required to store one
element of the array multiply by the total element in the array.
 In array, memory allocation is done at the time of declaration of an array.
Example:
Sr. No. Instructions Description
1. #include<stdio.h> Header file included
2. #include<conio.h> Header file included
3. void main() Execution of program begins
4. { Memory is allocated for variable i,n and
array a
5. inti,n,a[10];
6. clrscr(); Clear the output of previous screen
7. printf("enter a
number");
Print “enter a number”
8. scanf("%d",&n); Input value is stored at the addres of
variable n
9. for(i=0;i<=10;i++) For loop started from value of i=0 to i=10
10. { Compound statement(scope of for loop
starts)
11. a[i]=n*i; Result of multiplication of n and I is stored
at the ith location of array ieasi=0 so it is
stores at first location.
12. printf("n %d",a[i]); Value of ith location of the array is printed
13. } Compound statement(scope of for loop
ends)
14. printf("n
first element in
array is %d",a[0]);
Value of first element of the array
is printed
15. printf("n fifth
element in array is
%d",a[4]);
Value of fifth element of the array is
printed
16. printf("n tenth
element in array is
Value of tenth element of the array is
printed
%d",a[9]);
17. getch(); Used to hold the output screen
18. } Indicates end of scope of main function
This is the default way of passing the parameters to the function. This is achieved by
passing the copy of data to the function. This mechanism is also called as call by value.
In case of parameter passing by value, the changes made to the formal arguments in the
called function have no effect on the values of actual arguments in the calling function.
This mechanism is used when programmer don't want to change the value of passed
parameters. When parameters are passed by value then functions in C create copies of
the passed in variables and do required processing on these copied variables.
Pass-by-value is implemented by actual data transfer so additional storage is required to
maintain the copies of passed parameters.
Example:
#include <stdio.h>
#include<conio.h>
/* function declaration goes here.*/
void swap( int p1, int p2 );
int main()
{
int a = 10;
int b = 20;
printf("Before: Value of a = %d and value of b = %dn", a, b );
swap( a, b );
printf("After: Value of a = %d and value of b = %dn", a, b );
getch();
}
void swap( int p1, int p2 )
{
int t;
t = p2;
p2 = p1;
p1 = t;
printf("Value of a (p1) = %d and value of b(p2) = %dn", p1, p2 );
}
Output :
Before: Value of a = 10 and value of b = 20
Value of a (p1) = 20 and value of b (p2) = 10
After: Value of a = 10 and value of b = 20
Note: In the above example the values of “a” and “b” remain unchanged before calling
swap function and after calling swap function.
Reference
 Brian W. Kernighan And Dennis M. Ritchie, The C Programming Language, Prentice
Hall Of India
 YashwantKanetkar, Let Us C, Bpb Publication
PPS 6.6.FUNCTION INTRODUCTION & WRITING FUNCTIONS, SCOPE OF VARIABLES FUNCTIONS

More Related Content

What's hot (20)

PPTX
Issues in design_of_code_generator
vinithapanneer
 
PPTX
Computer architecture page replacement algorithms
Mazin Alwaaly
 
PPSX
Object oriented programming 2
Aadil Ansari
 
PPTX
minimization the number of states of DFA
Archana Gopinath
 
PPT
Binary Search
kunj desai
 
PPTX
Input-Buffering
Dattatray Gandhmal
 
PPTX
VIRTUAL MEMORY
Kamran Ashraf
 
PPT
Naive String Matching Algorithm | Computer Science
Transweb Global Inc
 
PPTX
Linux Memory Management with CMA (Contiguous Memory Allocator)
Pankaj Suryawanshi
 
PPTX
KMP String Matching Algorithm
kalpanasatishkumar
 
PDF
Top 100 PHP Interview Questions and Answers
Vineet Kumar Saini
 
DOCX
Hangman Game Programming in C (coding)
hasan0812
 
PPT
Chapter 8 : Memory
Amin Omi
 
PDF
M.c.a. (sem ii) operating systems
Tushar Rajput
 
PPTX
Sum of subset problem.pptx
V.V.Vanniaperumal College for Women
 
PDF
Cs6503 theory of computation book notes
appasami
 
PDF
Advanced Apache Spark Meetup Project Tungsten Nov 12 2015
Chris Fregly
 
PPTX
Virtual memory management in Operating System
Rashmi Bhat
 
PPTX
Quadratic probing
rajshreemuthiah
 
PPTX
Steps for c program execution
Rumman Ansari
 
Issues in design_of_code_generator
vinithapanneer
 
Computer architecture page replacement algorithms
Mazin Alwaaly
 
Object oriented programming 2
Aadil Ansari
 
minimization the number of states of DFA
Archana Gopinath
 
Binary Search
kunj desai
 
Input-Buffering
Dattatray Gandhmal
 
VIRTUAL MEMORY
Kamran Ashraf
 
Naive String Matching Algorithm | Computer Science
Transweb Global Inc
 
Linux Memory Management with CMA (Contiguous Memory Allocator)
Pankaj Suryawanshi
 
KMP String Matching Algorithm
kalpanasatishkumar
 
Top 100 PHP Interview Questions and Answers
Vineet Kumar Saini
 
Hangman Game Programming in C (coding)
hasan0812
 
Chapter 8 : Memory
Amin Omi
 
M.c.a. (sem ii) operating systems
Tushar Rajput
 
Sum of subset problem.pptx
V.V.Vanniaperumal College for Women
 
Cs6503 theory of computation book notes
appasami
 
Advanced Apache Spark Meetup Project Tungsten Nov 12 2015
Chris Fregly
 
Virtual memory management in Operating System
Rashmi Bhat
 
Quadratic probing
rajshreemuthiah
 
Steps for c program execution
Rumman Ansari
 

Similar to PPS 6.6.FUNCTION INTRODUCTION & WRITING FUNCTIONS, SCOPE OF VARIABLES FUNCTIONS (20)

PPTX
3 Function & Storage Class.pptx
aarockiaabinsAPIICSE
 
PPTX
C concepts and programming examples for beginners
SHAAMILIRAJAKUMAR1
 
PDF
Principals of Programming in CModule -5.pdfModule-3.pdf
anilcsbs
 
PPTX
2-Concept of Pointers in c programming.pptx
naushigrdcs
 
PPTX
Function (rule in programming)
Unviersity of balochistan quetta
 
PPTX
Function in C program
Nurul Zakiah Zamri Tan
 
PPT
User Defined Functions in C
RAJ KUMAR
 
PPT
Functions and pointers_unit_4
MKalpanaDevi
 
PDF
USER DEFINED FUNCTIONS IN C.pdf
BoomBoomers
 
PDF
VIT351 Software Development VI Unit1
YOGESH SINGH
 
PPTX
UNIT3.pptx
NagasaiT
 
PPT
Unit iv functions
indra Kishor
 
PPT
16717 functions in C++
LPU
 
PPT
Fucntions & Pointers in C
Janani Satheshkumar
 
PPTX
chapter-7 slide.pptx
cricketreview
 
PDF
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 3.pdf
amanpathak160605
 
PDF
Unit 3 (1)
Sowri Rajan
 
PDF
cp Module4(1)
Amarjith C K
 
PPTX
unit_2.pptx
Venkatesh Goud
 
PPTX
Functions
Golda Margret Sheeba J
 
3 Function & Storage Class.pptx
aarockiaabinsAPIICSE
 
C concepts and programming examples for beginners
SHAAMILIRAJAKUMAR1
 
Principals of Programming in CModule -5.pdfModule-3.pdf
anilcsbs
 
2-Concept of Pointers in c programming.pptx
naushigrdcs
 
Function (rule in programming)
Unviersity of balochistan quetta
 
Function in C program
Nurul Zakiah Zamri Tan
 
User Defined Functions in C
RAJ KUMAR
 
Functions and pointers_unit_4
MKalpanaDevi
 
USER DEFINED FUNCTIONS IN C.pdf
BoomBoomers
 
VIT351 Software Development VI Unit1
YOGESH SINGH
 
UNIT3.pptx
NagasaiT
 
Unit iv functions
indra Kishor
 
16717 functions in C++
LPU
 
Fucntions & Pointers in C
Janani Satheshkumar
 
chapter-7 slide.pptx
cricketreview
 
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 3.pdf
amanpathak160605
 
Unit 3 (1)
Sowri Rajan
 
cp Module4(1)
Amarjith C K
 
unit_2.pptx
Venkatesh Goud
 
Ad

More from Sitamarhi Institute of Technology (20)

PDF
STET 2025 900+ Computer MCQs in English PDF (studynotes.online).pdf
Sitamarhi Institute of Technology
 
PDF
DeepSeek vs. ChatGPT - The Battle of AI Titans.pdf
Sitamarhi Institute of Technology
 
DOCX
METHODS OF CUTTING COPYING HTML BASIC NOTES
Sitamarhi Institute of Technology
 
PDF
introduction Printer basic notes Hindi and English
Sitamarhi Institute of Technology
 
PDF
Beginners Guide to Microsoft OneDrive 2024–2025.pdf
Sitamarhi Institute of Technology
 
PDF
ChatGPT Foundations rompts given for each topic in both personal and business...
Sitamarhi Institute of Technology
 
PDF
Google Drive Mastery Guide for Beginners.pdf
Sitamarhi Institute of Technology
 
PDF
Chat GPT 1000+ Prompts - Chat GPT Prompts .pdf
Sitamarhi Institute of Technology
 
PDF
Smart Phone Film Making.filmmaking but feel limited by the constraints of exp...
Sitamarhi Institute of Technology
 
PDF
WhatsApp Tricks and Tips - 20th Edition 2024.pdf
Sitamarhi Institute of Technology
 
PDF
Mastering ChatGPT for Creative Ideas Generation.pdf
Sitamarhi Institute of Technology
 
PDF
BASIC COMPUTER CONCEPTSMADE BY: SIR SAROJ KUMAR
Sitamarhi Institute of Technology
 
PDF
MS Word tutorial provides basic and advanced concepts of Word.
Sitamarhi Institute of Technology
 
PPTX
BELTRON_PROGRAMMER 2018 and 2019 previous papers
Sitamarhi Institute of Technology
 
PDF
CORPORATE SOCIAL RESPONSIBILITY CSR) through a presentation by R.K. Sahoo
Sitamarhi Institute of Technology
 
PDF
Enhancing-digital-engagement-integrating-storytelling-
Sitamarhi Institute of Technology
 
PDF
business-with-innovative email-marketing-solution-
Sitamarhi Institute of Technology
 
PDF
MS Excel Notes PDF in Hindi माइोसॉट एसेल
Sitamarhi Institute of Technology
 
PDF
beltron-programmer-2023-previous-year-question.pdf
Sitamarhi Institute of Technology
 
PDF
Beltron Programmer IGNOU-MCA-NEW-Syllabus.pdf
Sitamarhi Institute of Technology
 
STET 2025 900+ Computer MCQs in English PDF (studynotes.online).pdf
Sitamarhi Institute of Technology
 
DeepSeek vs. ChatGPT - The Battle of AI Titans.pdf
Sitamarhi Institute of Technology
 
METHODS OF CUTTING COPYING HTML BASIC NOTES
Sitamarhi Institute of Technology
 
introduction Printer basic notes Hindi and English
Sitamarhi Institute of Technology
 
Beginners Guide to Microsoft OneDrive 2024–2025.pdf
Sitamarhi Institute of Technology
 
ChatGPT Foundations rompts given for each topic in both personal and business...
Sitamarhi Institute of Technology
 
Google Drive Mastery Guide for Beginners.pdf
Sitamarhi Institute of Technology
 
Chat GPT 1000+ Prompts - Chat GPT Prompts .pdf
Sitamarhi Institute of Technology
 
Smart Phone Film Making.filmmaking but feel limited by the constraints of exp...
Sitamarhi Institute of Technology
 
WhatsApp Tricks and Tips - 20th Edition 2024.pdf
Sitamarhi Institute of Technology
 
Mastering ChatGPT for Creative Ideas Generation.pdf
Sitamarhi Institute of Technology
 
BASIC COMPUTER CONCEPTSMADE BY: SIR SAROJ KUMAR
Sitamarhi Institute of Technology
 
MS Word tutorial provides basic and advanced concepts of Word.
Sitamarhi Institute of Technology
 
BELTRON_PROGRAMMER 2018 and 2019 previous papers
Sitamarhi Institute of Technology
 
CORPORATE SOCIAL RESPONSIBILITY CSR) through a presentation by R.K. Sahoo
Sitamarhi Institute of Technology
 
Enhancing-digital-engagement-integrating-storytelling-
Sitamarhi Institute of Technology
 
business-with-innovative email-marketing-solution-
Sitamarhi Institute of Technology
 
MS Excel Notes PDF in Hindi माइोसॉट एसेल
Sitamarhi Institute of Technology
 
beltron-programmer-2023-previous-year-question.pdf
Sitamarhi Institute of Technology
 
Beltron Programmer IGNOU-MCA-NEW-Syllabus.pdf
Sitamarhi Institute of Technology
 
Ad

Recently uploaded (20)

PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPTX
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PPTX
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
Design Thinking basics for Engineers.pdf
CMR University
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 

PPS 6.6.FUNCTION INTRODUCTION & WRITING FUNCTIONS, SCOPE OF VARIABLES FUNCTIONS

  • 1. PPS Unit – 6 Function 6.1 Introduction & Writing Functions Similar to other languages C language also provides the facility of function. Function is the block of code which is used to perform a specific task. In c language the complete program is composed of function. Functions are useful to divide c programs into smaller modules. Programmer can invoked these modules anywhere inside c program for any number of times. Functions are used to increase readability of the code. Size of program can be reduce by using functions. By using function, programmer can divide complex tasks into smaller manageable tasks and test them independently before using them together. Functions of C language are defined with the type of function. The type of functions indicates the data type of value which will return by function. In order to use function in the program, initially programmer have to inform compiler about the function. This is also called as defining a function. In C programme all the function definition present outside the main function. All function need to be declared and defined before use. Function declaration requires function name, argument list, and return type. Return Type Function name (Argument list) { Statement 1; Statement 2; …………... Statement n;
  • 2. } 6.2 Scope Of Variables Functions (Including Using Built In Libraries) 1. Library Functions A function which is predefined in c language is called library function. Library function is also called as built in function of C language. The definition of library function is stored in respective header file. Library functions are used to perform dedicated operation like taking input from user, displaying output, string handling operation, etc. Library functions are readily available and programmer can directly use it without writing any extra code. For example, printf () and scanf () are library function and their definition is stored in stdio header file. 2. User Defined Functions User define function is the block of code written by programmer to perform a particular task. As compiler doesn’t have any idea about the user define function so programmer has to define and declare these functions inside the program body. Programmer can define these function outside the main function but declaration of user define function should present in main function only. Whenever compiler executes function call (function declaration) then compiler shift the flow of program execution to the definition part of user define function. Example #include <stdio.h> #include<conio.h> int add (int x, int y) { int sum; sum = x + y; return (sum); } main ()
  • 3. { inta,b,c; a = 15; b = 25; c = add(a,b); printf ("n Addition is %d ", c); } Output: Addition is 40 There are two ways to pass the parameters to the function 1. Parameter Passing by value In this mechanism, the value of the parameter is passed while calling the function. 2. Parameter Passing by reference In this mechanism, the address of the parameter is passed while calling the function. 6.3 Parameter Passing In Functions, Call By Value Parameter Passing by Value This is the default way of passing the parameters to the function. This is achieved by passing the copy of data to the function. This mechanism is also called as call by value. In case of parameter passing by value, the changes made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function. This mechanism is used when programmer don't want to change the value of passed parameters. When parameters are passed by value then functions in C create copies of the passed in variables and do required processing on these copied variables.
  • 4. Pass-by-value is implemented by actual data transfer so additional storage is required to maintain the copies of passed parameters. Example: #include <stdio.h> #include<conio.h> /* function declaration goes here.*/ void swap( int p1, int p2 ); int main() { int a = 10; int b = 20; printf("Before: Value of a = %d and value of b = %dn", a, b ); swap( a, b ); printf("After: Value of a = %d and value of b = %dn", a, b ); getch(); } void swap( int p1, int p2 ) { int t; t = p2; p2 = p1; p1 = t;
  • 5. printf("Value of a (p1) = %d and value of b(p2) = %dn", p1, p2 ); } Output : Before: Value of a = 10 and value of b = 20 Value of a (p1) = 20 and value of b (p2) = 10 After: Value of a = 10 and value of b = 20 Note: In the above example the values of “a” and “b” remain unchanged before calling swap function and after calling swap function. Parameter Passing by Reference This mechanism is used when programmer want a function to do the changes in passed parameters and reflect those changes back to the calling function. This mechanism is also called as call by reference. This is achieved by passing the address of variable to the function and function body can directly work over the addresses. Advantage of pass by reference is efficiency in both time and space. Whereas disadvantages are access to formal parameters is slow and inadvertent and erroneous changes may be made to the actual parameter. Example: #include <stdio.h> #include<conio.h> void swap( int *p1, int *p2 ); int main() { int a = 10; int b = 20; printf("Before: Value of a = %d and value of b = %dn", a, b );
  • 6. swap(&a, &b ); printf("After: Value of a = %d and value of b = %dn", a, b ); } void swap( int *p1, int *p2 ) { int t; t = *p2; *p2 = *p1; *p1 = t; printf("Value of a (p1) = %d and value of b(p2) = %dn", *p1, *p2 ); } Output : Before: Value of a = 10 and value of b = 20 Value of a (p1) = 20 and value of b(p2) = 10 After: Value of a = 20 and value of b = 10 Note: In the above example the values of “a” and “b” are changes after calling swap function. 6.4 Passing Arrays To Functions: Idea Of Call By Reference Array is a data structure which stores the collection of similar types of element in consecutive memory locations. Indexing of array always start with ‘0’ where as non- graphical variable ‘0’ indicates the end of array. Syntax for declaring array is data type array name[Maximum size];
  • 7.  Data types are used to define type of element in an array. Data types are also useful in finding the size of total memory locations allocated for the array.  All the rules of defining name of variable are also applicable for the array name.  Maximum size indicates the total number of maximum elements that array can hold.  Total memory allocated for the array is equal to the memory required to store one element of the array multiply by the total element in the array.  In array, memory allocation is done at the time of declaration of an array. Example: Sr. No. Instructions Description 1. #include<stdio.h> Header file included 2. #include<conio.h> Header file included 3. void main() Execution of program begins 4. { Memory is allocated for variable i,n and array a 5. inti,n,a[10]; 6. clrscr(); Clear the output of previous screen 7. printf("enter a number"); Print “enter a number” 8. scanf("%d",&n); Input value is stored at the addres of variable n 9. for(i=0;i<=10;i++) For loop started from value of i=0 to i=10 10. { Compound statement(scope of for loop starts) 11. a[i]=n*i; Result of multiplication of n and I is stored at the ith location of array ieasi=0 so it is stores at first location. 12. printf("n %d",a[i]); Value of ith location of the array is printed 13. } Compound statement(scope of for loop ends) 14. printf("n first element in array is %d",a[0]); Value of first element of the array is printed 15. printf("n fifth element in array is %d",a[4]); Value of fifth element of the array is printed 16. printf("n tenth element in array is Value of tenth element of the array is printed
  • 8. %d",a[9]); 17. getch(); Used to hold the output screen 18. } Indicates end of scope of main function This is the default way of passing the parameters to the function. This is achieved by passing the copy of data to the function. This mechanism is also called as call by value. In case of parameter passing by value, the changes made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function. This mechanism is used when programmer don't want to change the value of passed parameters. When parameters are passed by value then functions in C create copies of the passed in variables and do required processing on these copied variables. Pass-by-value is implemented by actual data transfer so additional storage is required to maintain the copies of passed parameters. Example: #include <stdio.h> #include<conio.h> /* function declaration goes here.*/ void swap( int p1, int p2 ); int main() { int a = 10; int b = 20; printf("Before: Value of a = %d and value of b = %dn", a, b ); swap( a, b ); printf("After: Value of a = %d and value of b = %dn", a, b ); getch();
  • 9. } void swap( int p1, int p2 ) { int t; t = p2; p2 = p1; p1 = t; printf("Value of a (p1) = %d and value of b(p2) = %dn", p1, p2 ); } Output : Before: Value of a = 10 and value of b = 20 Value of a (p1) = 20 and value of b (p2) = 10 After: Value of a = 10 and value of b = 20 Note: In the above example the values of “a” and “b” remain unchanged before calling swap function and after calling swap function. Reference  Brian W. Kernighan And Dennis M. Ritchie, The C Programming Language, Prentice Hall Of India  YashwantKanetkar, Let Us C, Bpb Publication