SlideShare a Scribd company logo
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
Ad

More Related Content

What's hot (20)

Computer networks
Computer networksComputer networks
Computer networks
Tej Kiran
 
IT infrastructure and Network technologies for Midterm
IT infrastructure and Network technologies for MidtermIT infrastructure and Network technologies for Midterm
IT infrastructure and Network technologies for Midterm
Mark John Lado, MIT
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
baabtra.com - No. 1 supplier of quality freshers
 
Data types
Data typesData types
Data types
Dr. Rupinder Singh
 
Bit Torrent Protocol
Bit Torrent ProtocolBit Torrent Protocol
Bit Torrent Protocol
Ali Habeeb
 
File Management in C
File Management in CFile Management in C
File Management in C
Paurav Shah
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
Hajar Len
 
Preprocessors
Preprocessors Preprocessors
Preprocessors
Gourav Arora
 
Error detection and correction
Error detection and correctionError detection and correction
Error detection and correction
Maria Akther
 
Np unit2
Np unit2Np unit2
Np unit2
vamsitricks
 
Structure of a Switch
Structure of a SwitchStructure of a Switch
Structure of a Switch
Jyothishmathi Institute of Technology and Science Karimnagar
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 
Programming in C Presentation upto FILE
Programming in C Presentation upto FILEProgramming in C Presentation upto FILE
Programming in C Presentation upto FILE
Dipta Saha
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
Tasnima Hamid
 
Computer Networks.pptx
Computer Networks.pptxComputer Networks.pptx
Computer Networks.pptx
PrasanthKumar403552
 
Function C++
Function C++ Function C++
Function C++
Shahzad Afridi
 
history of c.ppt
history of c.ppthistory of c.ppt
history of c.ppt
arpanabharani
 
1 introduction-to-computer-networking
1 introduction-to-computer-networking1 introduction-to-computer-networking
1 introduction-to-computer-networking
Mayank Jain
 
Topologies
TopologiesTopologies
Topologies
Ahmed Elnaggar
 
Decision Making and Looping
Decision Making and LoopingDecision Making and Looping
Decision Making and Looping
Munazza-Mah-Jabeen
 

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

Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
Amarjith C K
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde3
 
Functions.pptx, programming language in c
Functions.pptx, programming language in cFunctions.pptx, programming language in c
Functions.pptx, programming language in c
floraaluoch3
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap
 
Detailed concept of function in c programming
Detailed concept of function  in c programmingDetailed concept of function  in c programming
Detailed concept of function in c programming
anjanasharma77573
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
LidetAdmassu
 
C function
C functionC function
C function
thirumalaikumar3
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
Sampath Kumar
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
Dr. Chandrakant Divate
 
Principals of Programming in CModule -5.pdfModule-3.pdf
Principals of Programming in CModule -5.pdfModule-3.pdfPrincipals of Programming in CModule -5.pdfModule-3.pdf
Principals of Programming in CModule -5.pdfModule-3.pdf
anilcsbs
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
JVenkateshGoud
 
Function in c
Function in cFunction in c
Function in c
CGC Technical campus,Mohali
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
Venkatesh Goud
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
kushwahashivam413
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
Sowri Rajan
 
Function in c
Function in cFunction in c
Function in c
Raj Tandukar
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
JAVVAJI VENKATA RAO
 
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdfModule 1_Chapter 2_PPT (1)sasaddsdsds.pdf
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
anilcsbs
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde3
 
Functions.pptx, programming language in c
Functions.pptx, programming language in cFunctions.pptx, programming language in c
Functions.pptx, programming language in c
floraaluoch3
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap
 
Detailed concept of function in c programming
Detailed concept of function  in c programmingDetailed concept of function  in c programming
Detailed concept of function in c programming
anjanasharma77573
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
LidetAdmassu
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
Dr. Chandrakant Divate
 
Principals of Programming in CModule -5.pdfModule-3.pdf
Principals of Programming in CModule -5.pdfModule-3.pdfPrincipals of Programming in CModule -5.pdfModule-3.pdf
Principals of Programming in CModule -5.pdfModule-3.pdf
anilcsbs
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
kushwahashivam413
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
JAVVAJI VENKATA RAO
 
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdfModule 1_Chapter 2_PPT (1)sasaddsdsds.pdf
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
anilcsbs
 
Ad

More from Sitamarhi Institute of Technology (20)

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

Recently uploaded (20)

Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
How to use nRF24L01 module with Arduino
How to use nRF24L01 module with ArduinoHow to use nRF24L01 module with Arduino
How to use nRF24L01 module with Arduino
CircuitDigest
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
Resistance measurement and cfd test on darpa subboff model
Resistance measurement and cfd test on darpa subboff modelResistance measurement and cfd test on darpa subboff model
Resistance measurement and cfd test on darpa subboff model
INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Data Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptxData Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptx
RushaliDeshmukh2
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
lecture5.pptxJHKGJFHDGTFGYIUOIUIPIOIPUOHIYGUYFGIH
lecture5.pptxJHKGJFHDGTFGYIUOIUIPIOIPUOHIYGUYFGIHlecture5.pptxJHKGJFHDGTFGYIUOIUIPIOIPUOHIYGUYFGIH
lecture5.pptxJHKGJFHDGTFGYIUOIUIPIOIPUOHIYGUYFGIH
Abodahab
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
How to use nRF24L01 module with Arduino
How to use nRF24L01 module with ArduinoHow to use nRF24L01 module with Arduino
How to use nRF24L01 module with Arduino
CircuitDigest
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Data Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptxData Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptx
RushaliDeshmukh2
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
lecture5.pptxJHKGJFHDGTFGYIUOIUIPIOIPUOHIYGUYFGIH
lecture5.pptxJHKGJFHDGTFGYIUOIUIPIOIPUOHIYGUYFGIHlecture5.pptxJHKGJFHDGTFGYIUOIUIPIOIPUOHIYGUYFGIH
lecture5.pptxJHKGJFHDGTFGYIUOIUIPIOIPUOHIYGUYFGIH
Abodahab
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 

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