SlideShare a Scribd company logo
Functions
08/23/151
08/23/152
Advantages :
 Program writing becomes easy
 Program becomes easy to understand
 Modification in large program becomes easy
 Modularity comes in program when we use function
08/23/153
Calling to a function.
message();
main( )
{
message( ) ;
printf ( "nHello!" ) ;
}
message( )
{
printf ( "nJUET..." ) ;
}
And here’s the output...
JUET
Hello! 08/23/154
Calling to a function(Cont…)
When main function calls message() the control passes to the
function message( ). The activity of main( ) is temporarily
suspended; it falls asleep while the message( ) function wakes up and
goes to work. When the message( ) function runs out of statements
to execute, the control returns to main( ), which comes to life again
and begins executing its code at the exact point where it left off.
Thus, main( ) becomes the ‘calling’ function, whereas message( )
becomes the ‘called’ function.
08/23/155
Cont…
main( )
{
printf ( "nI am in main" ) ;
italy( ) ;
brazil( ) ;
argentina( ) ;
}
italy( ){
printf ( "nI am in italy" ) ;
}
brazil( ){
printf ( "nI am in brazil" ) ;
}
argentina( ){
printf ( "nI am in argentina" ) ;
}
08/23/156
I am in main
I am in italy
I am in brazil
I am in argentina
Function Declaration
Declaration Syntax:
Return_Type Function_Name(argument_list);
 Return_Type can be any of data type like char, int, float, double,
array, pointer etc.
 argument_list can also be any of data type like char, int, float,
double, array, pointer etc.
 Declaration must be before the call of function in main
function
Example: int add(int a, int b);
08/23/157
Function Definition
08/23/158
Function Call
Call Syntax:
Function_Name(argument_list);
08/23/159
Sample Example
#include<stdio.h>
int sum (int,int); //function declaration
void main(){
int p;
p=sum(3,4); //function call
printf(“%d”,p);
}
int sum( int a,int b){
int s; //function body
s=a+b;
return s; //function returning a value
} 08/23/1510
Passing arguments to functions
In programming, argument(parameter) refers to data that is
passed to function(function definition) while calling function.
In following example two variable, num1 and num2 are passed to
function during function call and these arguments are accepted by
arguments a and b in function definition. 
08/23/1511
Cont…
 Formal Parameter :Parameter written in Function Definition is Called “Formal
Parameter”.
 In last example a and b are formal parameters.
 There are two methods of declaring the formal arguments.
1. Kernighan and Ritchie (or just K & R) method.
calsum ( x, y, z )
int x, y, z ;
2. ANSI method
calsum ( int x, int y, int z )
 This method is called ANSI method and is more commonlyused these days.
 Actual Parameter :Parameter written in Function Call is Called “Actual
Parameter”.
 In last example num1 and num2 were actual parameters. 08/23/1512
Example: parameter passing
/* Sending and receiving values between functions */
main( ){
int a, b, c, sum ;
printf ( "nEnter any three numbers " ) ;
scanf ( "%d %d %d", &a, &b, &c ) ;
sum = calsum ( a, b, c ) ;
printf ( "nSum = %d", sum ) ;
}
calsum ( x, y, z )
int x, y, z ;
{
int d ;
d = x + y + z ;
return ( d ) ;
}
And here is the output...
Enter any three numbers 10 20 30
Sum = 60 08/23/1513
Return statement
In the message() function of previous example the moment closing
brace ( } ) of the called function was encountered the control returned
to the calling function. No separate return statement was necessary to
send back the control.
This approach is fine if the called function is not going to return any
meaningful value to the calling function.
In the above program, however, we want to return the sum of x, y
and z. Therefore, it is necessary to use the return statement.
 The return statement serves two purposes:
(1) On executing the return statement it immediately transfers the control back to the
calling program.
(2) It returns the value present in the parentheses after return, to the calling function.
In the above program the value of sum of three numbers is being returned.
08/23/1514
Return statement(Cont…)
 There is no restriction on the number of return statements that may be present in a
function. Also, the return statement need not always be present at the end of the
called function.
 The following program illustrates these facts.
fun( ){
char ch ;
printf ( "nEnter any alphabet " ) ;
scanf ( "%c", &ch ) ;
if ( ch >= 65 && ch <= 90 )
return ( ch ) ;
else
return ( ch + 32 ) ;
}
 In this function different return statements will be executed depending on whether
ch is capital or not.
08/23/1515
Return statement(Cont…)
 If a meaningful value is returned then it should be accepted in the calling program by
equating the called function to some variable. For example,
 sum = calsum ( a, b, c ) ;
 All the following are valid return statements.
 return ( a ) ;
 return ( 23 ) ;
 return ( 12.34 ) ;
 return ;
 A function can return only one value at a time. Thus, the following statements are
invalid.
return ( a, b ) ;//this will return value of b
return ( x, 12 ) ;
 There is a way to get around this limitation, which would be discussed later when we
learn pointers.
08/23/1516
Calling Convention
Calling convention indicates the order in which arguments are
passed to a function when a function call is encountered. There
are two possibilities here:
(a) Arguments might be passed from left to right.
(b) Arguments might be passed from right to left.
C language follows the second order.
Consider the following function call:
fun (a, b, c, d ) ;
In this call it doesn’t matter whether the arguments are passed
from left to right or from right to left.
08/23/1517
Calling Convention(Cont…)
08/23/1518
•However, in some function call the order of passing arguments
becomes an important consideration.
•For example:
Ways to Pass Parameters
Call By Value:In this approach we pass copy of actual variables
in function as a parameter.
Hence any modification on parameters inside the function will not
reflect in the actual variable.
08/23/1519
Ways to Pass
Parameters(Cont..)
Call By Reference:In this approach we pass memory address of
actual variables in function as a parameter.
Hence any modification on parameters inside the function will
reflect in the actual variable.
08/23/1520
Swap two variables without using a
third variable
#include<stdio.h>
void swap(int *,int *);
void main(){
int a=5,b=10;
swap(&a,&b);
printf("%d %d",a,b);
}
void swap(int *a,int *b){
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
}
Types of Function
1st :Take something and Return something (Function
with return value and parameters)
Example: printf, scanf , strlen, strcmp etc.
2nd : Take something and Return nothing (Function
with no return value but parameters)
Example: delay,
3rd : Take nothing and return something (Function with
return value but no parameter)
Example: getch,
4th : Take nothing and return nothing (Function with
no return value and no parameter)
Example: clrscr,
08/23/1522
Take something and Return
something
08/23/1523
Take something and Return
nothing
08/23/1524
Take nothing and return
something
08/23/1525
Take nothing and return
nothing
08/23/1526
Nesting of function call
 If we are calling any function inside another function call is known as nesting
function call.
 Sometime it converts a difficult program in easy one.
 For example 1:
int max(int x,int y){
return x>y?x:y;
}
int fact(int);
void main(){
int a,b,c;
scanf(“%d%d”,&a,&b);
c=fact(max(a,b));
print(“factorial of %d is %d”,max(a,b),c);
}
08/23/1527
int fact(int a)
{
int fact=1;
while(a>0)
{
fact=fact*a;
a--;
}
return fact; 
}
O/P:6
7
factorial of 7 is 5040
Example 2 Nested calling
08/23/1528
Problem
Write a function which receives a float and an int from
main( ), finds the product of these two and returns the
product which is printed through main( ).
08/23/1529

More Related Content

What's hot

C function
C functionC function
C function
thirumalaikumar3
 
C programming function
C  programming functionC  programming function
C programming function
argusacademy
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
imtiazalijoono
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
Sampath Kumar
 
Functions in C
Functions in CFunctions in C
Functions in C
Princy Nelson
 
Functions in c
Functions in cFunctions in c
Functions in c
sunila tharagaturi
 
M11 operator overloading and type conversion
M11 operator overloading and type conversionM11 operator overloading and type conversion
M11 operator overloading and type conversion
NabeelaNousheen
 
Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++
NUST Stuff
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
imtiazalijoono
 
Function in c program
Function in c programFunction in c program
Function in c program
CGC Technical campus,Mohali
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
Vishalini Mugunen
 
Function in c
Function in cFunction in c
functions in C and types
functions in C and typesfunctions in C and types
functions in C and types
mubashir farooq
 

What's hot (20)

C function
C functionC function
C function
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Function in c
Function in cFunction in c
Function in c
 
C programming function
C  programming functionC  programming function
C programming function
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Function
FunctionFunction
Function
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Functions in c
Functions in cFunctions in c
Functions in c
 
M11 operator overloading and type conversion
M11 operator overloading and type conversionM11 operator overloading and type conversion
M11 operator overloading and type conversion
 
Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
Function in c program
Function in c programFunction in c program
Function in c program
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
Functions
FunctionsFunctions
Functions
 
Function in c
Function in cFunction in c
Function in c
 
functions in C and types
functions in C and typesfunctions in C and types
functions in C and types
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 

Similar to 11 functions

UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
NagasaiT
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
JVenkateshGoud
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
Mehul Desai
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
Mohammed Saleh
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
ssuser823678
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
ssuser2076d9
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5alish sha
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
LadallaRajKumar
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloadingankush_kumar
 
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
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
Venkatesh Goud
 
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 and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
Saranya saran
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
Amarjith C K
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
LidetAdmassu
 
Functions
FunctionsFunctions
Functions
zeeshan841
 
Unit iv functions
Unit  iv functionsUnit  iv functions
Unit iv functions
indra Kishor
 
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
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
Arpit Meena
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
TeshaleSiyum
 

Similar to 11 functions (20)

UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
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
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
 
Functions
FunctionsFunctions
Functions
 
Unit iv functions
Unit  iv functionsUnit  iv functions
Unit iv functions
 
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...
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
 

More from Rohit Shrivastava

1 introduction-to-computer
1 introduction-to-computer1 introduction-to-computer
1 introduction-to-computer
Rohit Shrivastava
 
17 structure-and-union
17 structure-and-union17 structure-and-union
17 structure-and-union
Rohit Shrivastava
 
16 dynamic-memory-allocation
16 dynamic-memory-allocation16 dynamic-memory-allocation
16 dynamic-memory-allocation
Rohit Shrivastava
 
3 algorithm-and-flowchart
3 algorithm-and-flowchart3 algorithm-and-flowchart
3 algorithm-and-flowchart
Rohit Shrivastava
 
4 evolution-of-programming-languages
4 evolution-of-programming-languages4 evolution-of-programming-languages
4 evolution-of-programming-languages
Rohit Shrivastava
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
Rohit Shrivastava
 

More from Rohit Shrivastava (13)

1 introduction-to-computer
1 introduction-to-computer1 introduction-to-computer
1 introduction-to-computer
 
17 structure-and-union
17 structure-and-union17 structure-and-union
17 structure-and-union
 
16 dynamic-memory-allocation
16 dynamic-memory-allocation16 dynamic-memory-allocation
16 dynamic-memory-allocation
 
14 strings
14 strings14 strings
14 strings
 
10 array
10 array10 array
10 array
 
8 number-system
8 number-system8 number-system
8 number-system
 
7 decision-control
7 decision-control7 decision-control
7 decision-control
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
5 introduction-to-c
5 introduction-to-c5 introduction-to-c
5 introduction-to-c
 
3 algorithm-and-flowchart
3 algorithm-and-flowchart3 algorithm-and-flowchart
3 algorithm-and-flowchart
 
2 memory-and-io-devices
2 memory-and-io-devices2 memory-and-io-devices
2 memory-and-io-devices
 
4 evolution-of-programming-languages
4 evolution-of-programming-languages4 evolution-of-programming-languages
4 evolution-of-programming-languages
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 

Recently uploaded

RMD24 | Retail media: hoe zet je dit in als je geen AH of Unilever bent? Heid...
RMD24 | Retail media: hoe zet je dit in als je geen AH of Unilever bent? Heid...RMD24 | Retail media: hoe zet je dit in als je geen AH of Unilever bent? Heid...
RMD24 | Retail media: hoe zet je dit in als je geen AH of Unilever bent? Heid...
BBPMedia1
 
Maksym Vyshnivetskyi: PMO Quality Management (UA)
Maksym Vyshnivetskyi: PMO Quality Management (UA)Maksym Vyshnivetskyi: PMO Quality Management (UA)
Maksym Vyshnivetskyi: PMO Quality Management (UA)
Lviv Startup Club
 
falcon-invoice-discounting-a-premier-platform-for-investors-in-india
falcon-invoice-discounting-a-premier-platform-for-investors-in-indiafalcon-invoice-discounting-a-premier-platform-for-investors-in-india
falcon-invoice-discounting-a-premier-platform-for-investors-in-india
Falcon Invoice Discounting
 
Project File Report BBA 6th semester.pdf
Project File Report BBA 6th semester.pdfProject File Report BBA 6th semester.pdf
Project File Report BBA 6th semester.pdf
RajPriye
 
The-McKinsey-7S-Framework. strategic management
The-McKinsey-7S-Framework. strategic managementThe-McKinsey-7S-Framework. strategic management
The-McKinsey-7S-Framework. strategic management
Bojamma2
 
Putting the SPARK into Virtual Training.pptx
Putting the SPARK into Virtual Training.pptxPutting the SPARK into Virtual Training.pptx
Putting the SPARK into Virtual Training.pptx
Cynthia Clay
 
一比一原版加拿大渥太华大学毕业证(uottawa毕业证书)如何办理
一比一原版加拿大渥太华大学毕业证(uottawa毕业证书)如何办理一比一原版加拿大渥太华大学毕业证(uottawa毕业证书)如何办理
一比一原版加拿大渥太华大学毕业证(uottawa毕业证书)如何办理
taqyed
 
LA HUG - Video Testimonials with Chynna Morgan - June 2024
LA HUG - Video Testimonials with Chynna Morgan - June 2024LA HUG - Video Testimonials with Chynna Morgan - June 2024
LA HUG - Video Testimonials with Chynna Morgan - June 2024
Lital Barkan
 
ikea_woodgreen_petscharity_dog-alogue_digital.pdf
ikea_woodgreen_petscharity_dog-alogue_digital.pdfikea_woodgreen_petscharity_dog-alogue_digital.pdf
ikea_woodgreen_petscharity_dog-alogue_digital.pdf
agatadrynko
 
ENTREPRENEURSHIP TRAINING.ppt for graduating class (1).ppt
ENTREPRENEURSHIP TRAINING.ppt for graduating class (1).pptENTREPRENEURSHIP TRAINING.ppt for graduating class (1).ppt
ENTREPRENEURSHIP TRAINING.ppt for graduating class (1).ppt
zechu97
 
3.0 Project 2_ Developing My Brand Identity Kit.pptx
3.0 Project 2_ Developing My Brand Identity Kit.pptx3.0 Project 2_ Developing My Brand Identity Kit.pptx
3.0 Project 2_ Developing My Brand Identity Kit.pptx
tanyjahb
 
Memorandum Of Association Constitution of Company.ppt
Memorandum Of Association Constitution of Company.pptMemorandum Of Association Constitution of Company.ppt
Memorandum Of Association Constitution of Company.ppt
seri bangash
 
Exploring Patterns of Connection with Social Dreaming
Exploring Patterns of Connection with Social DreamingExploring Patterns of Connection with Social Dreaming
Exploring Patterns of Connection with Social Dreaming
Nicola Wreford-Howard
 
Cree_Rey_BrandIdentityKit.PDF_PersonalBd
Cree_Rey_BrandIdentityKit.PDF_PersonalBdCree_Rey_BrandIdentityKit.PDF_PersonalBd
Cree_Rey_BrandIdentityKit.PDF_PersonalBd
creerey
 
ikea_woodgreen_petscharity_cat-alogue_digital.pdf
ikea_woodgreen_petscharity_cat-alogue_digital.pdfikea_woodgreen_petscharity_cat-alogue_digital.pdf
ikea_woodgreen_petscharity_cat-alogue_digital.pdf
agatadrynko
 
Kseniya Leshchenko: Shared development support service model as the way to ma...
Kseniya Leshchenko: Shared development support service model as the way to ma...Kseniya Leshchenko: Shared development support service model as the way to ma...
Kseniya Leshchenko: Shared development support service model as the way to ma...
Lviv Startup Club
 
The Influence of Marketing Strategy and Market Competition on Business Perfor...
The Influence of Marketing Strategy and Market Competition on Business Perfor...The Influence of Marketing Strategy and Market Competition on Business Perfor...
The Influence of Marketing Strategy and Market Competition on Business Perfor...
Adam Smith
 
20240425_ TJ Communications Credentials_compressed.pdf
20240425_ TJ Communications Credentials_compressed.pdf20240425_ TJ Communications Credentials_compressed.pdf
20240425_ TJ Communications Credentials_compressed.pdf
tjcomstrang
 
Unveiling the Secrets How Does Generative AI Work.pdf
Unveiling the Secrets How Does Generative AI Work.pdfUnveiling the Secrets How Does Generative AI Work.pdf
Unveiling the Secrets How Does Generative AI Work.pdf
Sam H
 
What are the main advantages of using HR recruiter services.pdf
What are the main advantages of using HR recruiter services.pdfWhat are the main advantages of using HR recruiter services.pdf
What are the main advantages of using HR recruiter services.pdf
HumanResourceDimensi1
 

Recently uploaded (20)

RMD24 | Retail media: hoe zet je dit in als je geen AH of Unilever bent? Heid...
RMD24 | Retail media: hoe zet je dit in als je geen AH of Unilever bent? Heid...RMD24 | Retail media: hoe zet je dit in als je geen AH of Unilever bent? Heid...
RMD24 | Retail media: hoe zet je dit in als je geen AH of Unilever bent? Heid...
 
Maksym Vyshnivetskyi: PMO Quality Management (UA)
Maksym Vyshnivetskyi: PMO Quality Management (UA)Maksym Vyshnivetskyi: PMO Quality Management (UA)
Maksym Vyshnivetskyi: PMO Quality Management (UA)
 
falcon-invoice-discounting-a-premier-platform-for-investors-in-india
falcon-invoice-discounting-a-premier-platform-for-investors-in-indiafalcon-invoice-discounting-a-premier-platform-for-investors-in-india
falcon-invoice-discounting-a-premier-platform-for-investors-in-india
 
Project File Report BBA 6th semester.pdf
Project File Report BBA 6th semester.pdfProject File Report BBA 6th semester.pdf
Project File Report BBA 6th semester.pdf
 
The-McKinsey-7S-Framework. strategic management
The-McKinsey-7S-Framework. strategic managementThe-McKinsey-7S-Framework. strategic management
The-McKinsey-7S-Framework. strategic management
 
Putting the SPARK into Virtual Training.pptx
Putting the SPARK into Virtual Training.pptxPutting the SPARK into Virtual Training.pptx
Putting the SPARK into Virtual Training.pptx
 
一比一原版加拿大渥太华大学毕业证(uottawa毕业证书)如何办理
一比一原版加拿大渥太华大学毕业证(uottawa毕业证书)如何办理一比一原版加拿大渥太华大学毕业证(uottawa毕业证书)如何办理
一比一原版加拿大渥太华大学毕业证(uottawa毕业证书)如何办理
 
LA HUG - Video Testimonials with Chynna Morgan - June 2024
LA HUG - Video Testimonials with Chynna Morgan - June 2024LA HUG - Video Testimonials with Chynna Morgan - June 2024
LA HUG - Video Testimonials with Chynna Morgan - June 2024
 
ikea_woodgreen_petscharity_dog-alogue_digital.pdf
ikea_woodgreen_petscharity_dog-alogue_digital.pdfikea_woodgreen_petscharity_dog-alogue_digital.pdf
ikea_woodgreen_petscharity_dog-alogue_digital.pdf
 
ENTREPRENEURSHIP TRAINING.ppt for graduating class (1).ppt
ENTREPRENEURSHIP TRAINING.ppt for graduating class (1).pptENTREPRENEURSHIP TRAINING.ppt for graduating class (1).ppt
ENTREPRENEURSHIP TRAINING.ppt for graduating class (1).ppt
 
3.0 Project 2_ Developing My Brand Identity Kit.pptx
3.0 Project 2_ Developing My Brand Identity Kit.pptx3.0 Project 2_ Developing My Brand Identity Kit.pptx
3.0 Project 2_ Developing My Brand Identity Kit.pptx
 
Memorandum Of Association Constitution of Company.ppt
Memorandum Of Association Constitution of Company.pptMemorandum Of Association Constitution of Company.ppt
Memorandum Of Association Constitution of Company.ppt
 
Exploring Patterns of Connection with Social Dreaming
Exploring Patterns of Connection with Social DreamingExploring Patterns of Connection with Social Dreaming
Exploring Patterns of Connection with Social Dreaming
 
Cree_Rey_BrandIdentityKit.PDF_PersonalBd
Cree_Rey_BrandIdentityKit.PDF_PersonalBdCree_Rey_BrandIdentityKit.PDF_PersonalBd
Cree_Rey_BrandIdentityKit.PDF_PersonalBd
 
ikea_woodgreen_petscharity_cat-alogue_digital.pdf
ikea_woodgreen_petscharity_cat-alogue_digital.pdfikea_woodgreen_petscharity_cat-alogue_digital.pdf
ikea_woodgreen_petscharity_cat-alogue_digital.pdf
 
Kseniya Leshchenko: Shared development support service model as the way to ma...
Kseniya Leshchenko: Shared development support service model as the way to ma...Kseniya Leshchenko: Shared development support service model as the way to ma...
Kseniya Leshchenko: Shared development support service model as the way to ma...
 
The Influence of Marketing Strategy and Market Competition on Business Perfor...
The Influence of Marketing Strategy and Market Competition on Business Perfor...The Influence of Marketing Strategy and Market Competition on Business Perfor...
The Influence of Marketing Strategy and Market Competition on Business Perfor...
 
20240425_ TJ Communications Credentials_compressed.pdf
20240425_ TJ Communications Credentials_compressed.pdf20240425_ TJ Communications Credentials_compressed.pdf
20240425_ TJ Communications Credentials_compressed.pdf
 
Unveiling the Secrets How Does Generative AI Work.pdf
Unveiling the Secrets How Does Generative AI Work.pdfUnveiling the Secrets How Does Generative AI Work.pdf
Unveiling the Secrets How Does Generative AI Work.pdf
 
What are the main advantages of using HR recruiter services.pdf
What are the main advantages of using HR recruiter services.pdfWhat are the main advantages of using HR recruiter services.pdf
What are the main advantages of using HR recruiter services.pdf
 

11 functions

  • 3. Advantages :  Program writing becomes easy  Program becomes easy to understand  Modification in large program becomes easy  Modularity comes in program when we use function 08/23/153
  • 4. Calling to a function. message(); main( ) { message( ) ; printf ( "nHello!" ) ; } message( ) { printf ( "nJUET..." ) ; } And here’s the output... JUET Hello! 08/23/154
  • 5. Calling to a function(Cont…) When main function calls message() the control passes to the function message( ). The activity of main( ) is temporarily suspended; it falls asleep while the message( ) function wakes up and goes to work. When the message( ) function runs out of statements to execute, the control returns to main( ), which comes to life again and begins executing its code at the exact point where it left off. Thus, main( ) becomes the ‘calling’ function, whereas message( ) becomes the ‘called’ function. 08/23/155
  • 6. Cont… main( ) { printf ( "nI am in main" ) ; italy( ) ; brazil( ) ; argentina( ) ; } italy( ){ printf ( "nI am in italy" ) ; } brazil( ){ printf ( "nI am in brazil" ) ; } argentina( ){ printf ( "nI am in argentina" ) ; } 08/23/156 I am in main I am in italy I am in brazil I am in argentina
  • 7. Function Declaration Declaration Syntax: Return_Type Function_Name(argument_list);  Return_Type can be any of data type like char, int, float, double, array, pointer etc.  argument_list can also be any of data type like char, int, float, double, array, pointer etc.  Declaration must be before the call of function in main function Example: int add(int a, int b); 08/23/157
  • 10. Sample Example #include<stdio.h> int sum (int,int); //function declaration void main(){ int p; p=sum(3,4); //function call printf(“%d”,p); } int sum( int a,int b){ int s; //function body s=a+b; return s; //function returning a value } 08/23/1510
  • 11. Passing arguments to functions In programming, argument(parameter) refers to data that is passed to function(function definition) while calling function. In following example two variable, num1 and num2 are passed to function during function call and these arguments are accepted by arguments a and b in function definition.  08/23/1511
  • 12. Cont…  Formal Parameter :Parameter written in Function Definition is Called “Formal Parameter”.  In last example a and b are formal parameters.  There are two methods of declaring the formal arguments. 1. Kernighan and Ritchie (or just K & R) method. calsum ( x, y, z ) int x, y, z ; 2. ANSI method calsum ( int x, int y, int z )  This method is called ANSI method and is more commonlyused these days.  Actual Parameter :Parameter written in Function Call is Called “Actual Parameter”.  In last example num1 and num2 were actual parameters. 08/23/1512
  • 13. Example: parameter passing /* Sending and receiving values between functions */ main( ){ int a, b, c, sum ; printf ( "nEnter any three numbers " ) ; scanf ( "%d %d %d", &a, &b, &c ) ; sum = calsum ( a, b, c ) ; printf ( "nSum = %d", sum ) ; } calsum ( x, y, z ) int x, y, z ; { int d ; d = x + y + z ; return ( d ) ; } And here is the output... Enter any three numbers 10 20 30 Sum = 60 08/23/1513
  • 14. Return statement In the message() function of previous example the moment closing brace ( } ) of the called function was encountered the control returned to the calling function. No separate return statement was necessary to send back the control. This approach is fine if the called function is not going to return any meaningful value to the calling function. In the above program, however, we want to return the sum of x, y and z. Therefore, it is necessary to use the return statement.  The return statement serves two purposes: (1) On executing the return statement it immediately transfers the control back to the calling program. (2) It returns the value present in the parentheses after return, to the calling function. In the above program the value of sum of three numbers is being returned. 08/23/1514
  • 15. Return statement(Cont…)  There is no restriction on the number of return statements that may be present in a function. Also, the return statement need not always be present at the end of the called function.  The following program illustrates these facts. fun( ){ char ch ; printf ( "nEnter any alphabet " ) ; scanf ( "%c", &ch ) ; if ( ch >= 65 && ch <= 90 ) return ( ch ) ; else return ( ch + 32 ) ; }  In this function different return statements will be executed depending on whether ch is capital or not. 08/23/1515
  • 16. Return statement(Cont…)  If a meaningful value is returned then it should be accepted in the calling program by equating the called function to some variable. For example,  sum = calsum ( a, b, c ) ;  All the following are valid return statements.  return ( a ) ;  return ( 23 ) ;  return ( 12.34 ) ;  return ;  A function can return only one value at a time. Thus, the following statements are invalid. return ( a, b ) ;//this will return value of b return ( x, 12 ) ;  There is a way to get around this limitation, which would be discussed later when we learn pointers. 08/23/1516
  • 17. Calling Convention Calling convention indicates the order in which arguments are passed to a function when a function call is encountered. There are two possibilities here: (a) Arguments might be passed from left to right. (b) Arguments might be passed from right to left. C language follows the second order. Consider the following function call: fun (a, b, c, d ) ; In this call it doesn’t matter whether the arguments are passed from left to right or from right to left. 08/23/1517
  • 18. Calling Convention(Cont…) 08/23/1518 •However, in some function call the order of passing arguments becomes an important consideration. •For example:
  • 19. Ways to Pass Parameters Call By Value:In this approach we pass copy of actual variables in function as a parameter. Hence any modification on parameters inside the function will not reflect in the actual variable. 08/23/1519
  • 20. Ways to Pass Parameters(Cont..) Call By Reference:In this approach we pass memory address of actual variables in function as a parameter. Hence any modification on parameters inside the function will reflect in the actual variable. 08/23/1520
  • 21. Swap two variables without using a third variable #include<stdio.h> void swap(int *,int *); void main(){ int a=5,b=10; swap(&a,&b); printf("%d %d",a,b); } void swap(int *a,int *b){ *a=*a+*b; *b=*a-*b; *a=*a-*b; }
  • 22. Types of Function 1st :Take something and Return something (Function with return value and parameters) Example: printf, scanf , strlen, strcmp etc. 2nd : Take something and Return nothing (Function with no return value but parameters) Example: delay, 3rd : Take nothing and return something (Function with return value but no parameter) Example: getch, 4th : Take nothing and return nothing (Function with no return value and no parameter) Example: clrscr, 08/23/1522
  • 23. Take something and Return something 08/23/1523
  • 24. Take something and Return nothing 08/23/1524
  • 25. Take nothing and return something 08/23/1525
  • 26. Take nothing and return nothing 08/23/1526
  • 27. Nesting of function call  If we are calling any function inside another function call is known as nesting function call.  Sometime it converts a difficult program in easy one.  For example 1: int max(int x,int y){ return x>y?x:y; } int fact(int); void main(){ int a,b,c; scanf(“%d%d”,&a,&b); c=fact(max(a,b)); print(“factorial of %d is %d”,max(a,b),c); } 08/23/1527 int fact(int a) { int fact=1; while(a>0) { fact=fact*a; a--; } return fact;  } O/P:6 7 factorial of 7 is 5040
  • 28. Example 2 Nested calling 08/23/1528
  • 29. Problem Write a function which receives a float and an int from main( ), finds the product of these two and returns the product which is printed through main( ). 08/23/1529