SlideShare a Scribd company logo
1 of 23
With Rumman Ansari
Functions
A function is a group of statements that together perform a task. Every C program
has at least one function, which is main(), and all the most trivial programs can
define additional functions.
A function declaration tells the compiler about a function's name, return type,
and parameters. A function definition provides the actual body of the function.
Defining a Function:
return_type function_name( parameter list )
{
body of the function
}
(a) C program is a collection of one or more functions.
Some important case
(b)A function gets called when the function name is followed by a semicolon. For example,
main( )
{
argentina( ) ;
}
(c)A function is defined when function name is followed by a pair of braces in which one or
more statements may be present. For example,
argentina( )
{
statement 1 ;
statement 2 ;
statement 3 ;
}
(d) Any function can be called from any other function. Even main( ) can be called from other
functions. For example,
main( )
{
message( ) ;
}
message( )
{
printf ( "nCan't imagine life without C" ) ;
main( ) ;
}
(e)A function can be called any number of times. For example,
main( )
{
message( ) ;
message( ) ;
}
message( )
{
printf ( "nJewel Thief!!" ) ;
}
(f) The order in which the functions are defined in a program and the order in which they get
called need not necessarily be same. For example,
main( )
{
message1( ) ;
message2( ) ;
}
message2( )
{
printf ( "nBut the butter was bitter" ) ;
}
message1( )
{
printf ( "nMary bought some butter" ) ;
}
Here, even though message1( ) is getting called before message2( ), still, message1( ) has been defined
after message2( ). However, it is advisable to define the functions in the same order in which they are called.
This makes the program easier to understand.
(g) A function can call itself. Such a process is called ‘recursion’. We would discuss this aspect
of C functions later in this chapter.
(h) A function can be called from other function, but a function cannot be defined in another function.
Thus, the following program code would be wrong, since argentina( ) is being defined inside
another function, main( ).
main( )
{
printf ( "nI am in main" ) ;
argentina( )
{
printf ( "nI am in argentina" ) ;
}
}
Types of C functions
Library function User defined function
Library function 1. main()
2. printf()
3. scanf()
User defined function #include <stdio.h>
void function_name(){
................
................
}
int main(){
...........
...........
function_name();
...........
...........
}
Types of User-defined Functions in C Programming
1. Function with no arguments and no return value
2. Function with no arguments and return value
3. Function with arguments but no return value
4. Function with arguments and return value.
Function with no arguments and no return value.
/*C program to check whether a number entered by user is prime or not using function with no arguments
and no return value*/
#include <stdio.h>
void prime();
int main(){
prime(); //No argument is passed to prime().
return 0;
}
void prime(){
/* There is no return value to calling function main(). Hence, return type of prime() is void */
int num,i,flag=0;
printf("Enter positive integer enter to check:n");
scanf("%d",&num);
for(i=2;i<=num/2;++i){
if(num%i==0){
flag=1;
}
}
if (flag==1)
printf("%d is not prime",num);
else
printf("%d is prime",num);
}
Function with no arguments but return value
/*C program to check whether a number entered by user is prime or not using function
with no arguments but having return value */
#include <stdio.h>
int input();
int main(){
int num,i,flag = 0;
num=input(); /* No argument is passed to input() */
for(i=2; i<=num/2; ++i){
if(num%i==0){
flag = 1;
break;
}
}
if(flag == 1)
printf("%d is not prime",num);
else
printf("%d is prime", num);
return 0;
}
int input(){ /* Integer value is returned from input() to calling function */
int n;
printf("Enter positive integer to check:n");
scanf("%d",&n);
return n;
}
Function with arguments and no return value
/*Program to check whether a number entered by user is prime or
not using function with arguments and no return value */
#include <stdio.h>
void check_display(int n);
int main(){
int num;
printf("Enter positive enter to check:n");
scanf("%d",&num);
check_display(num); /* Argument num is passed to function. */
return 0;
}
void check_display(int n){
/* There is no return value to calling function. Hence, return type of
function is void. */
int i, flag = 0;
for(i=2; i<=n/2; ++i){
if(n%i==0){
flag = 1;
break;
}
}
if(flag == 1)
printf("%d is not prime",n);
else
printf("%d is prime", n);
}
Function with argument and a return value
/* Program to check whether a number entered by user is prime or not
using function with argument and return value */
#include <stdio.h>
int check(int n);
int main(){
int num,num_check=0;
printf("Enter positive enter to check:n");
scanf("%d",&num);
num_check=check(num); /* Argument num is passed to check() function. */
if(num_check==1)
printf("%d is not prime",num);
else
printf("%d is prime",num);
return 0;
}
int check(int n){
/* Integer value is returned from function check() */
int i;
for(i=2;i<=n/2;++i){
if(n%i==0)
return 1;
}
return 0;
}
void Kulut(int a,int b)
{
c=a+b;
}
int Kulut(int a,int b)
{
c=a+b;
return 0;
}
Example:
/* function returning the max between two numbers */
int max(int num1, int num2)
{
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Example:
#include<stdio.h>
void main()
{
printf("n I am in main");
argentina();
}
void argentina()
{
printf("nI am in argentina n") ;
}
#include<stdio.h>
void main()
{
int c;
c=max(2,3);
printf("n this print is in main() %d n",c);
}
/* function returning the max between two numbers
*/
int max(int num1, int num2)
{
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
printf("n I am in max() %d n",result);
return result;
}
Example:
Example of
a Function:
#include <stdio.h>
/* function declaration */
int max(int num1, int num2);
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
int ret;
/* calling a function to get max value */
ret = max(a, b);
printf( "Max value is : %dn", ret );
return 0;
}
/* function returning the max between two numbers */
int max(int num1, int num2)
{
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
#include <stdio.h>
main( )
{
message( ) ;
message( ) ;
}
message( )
{
printf ( "n Rahul is Thief!!n" ) ;
}
Example:
Function Arguments
Call Type Description
Call by value This method copies the actual value of an
argument into the formal parameter of the
function. In this case, changes made to the
parameter inside the function have no effect on
the argument.
Call by reference This method copies the address of an argument
into the formal parameter. Inside the function, the
address is used to access the actual argument used
in the call. This means that changes made to the
parameter affect the argument.
call by value in C
The call by value method of passing arguments to a function copies the actual
value of an argument into the formal parameter of the function. In this case,
changes made to the parameter inside the function have no effect on the argument
By default, C programming language uses call by value method to pass arguments.
In general, this means that code within a function cannot alter the arguments used
to call the function. Consider the function swap() definition as follows.
/* function returning the max between two numbers */
int max(int num1, int num2)
{
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
#include <stdio.h>
/* function declaration */
void swap(int x, int y);
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
printf("Before swap, value of a : %dn", a );
printf("Before swap, value of b : %dn", b );
/* calling a function to swap the values */
swap(a, b);
printf("After swap, value of a : %dn", a );
printf("After swap, value of b : %dn", b );
return 0;
}
call by reference in C
The call by reference method of passing arguments to a function copies the
address of an argument into the formal parameter. Inside the function, the address is
used to access the actual argument used in the call. This means that changes made to
the parameter affect the passed argument.
/* function definition to swap the values */
void swap(int *x, int *y)
{
int temp;
temp = *x; /* save the value at address x */
*x = *y; /* put y into x */
*y = temp; /* put temp into y */
return;
}
#include <stdio.h>
/* function declaration */
void swap(int *x, int *y);
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
printf("Before swap, value of a : %dn", a );
printf("Before swap, value of b : %dn", b );
/* calling a function to swap the values.
* &a indicates pointer to a ie. address of variable a and
* &b indicates pointer to b ie. address of variable b.
*/
swap(&a, &b);
printf("After swap, value of a : %dn", a );
printf("After swap, value of b : %dn", b );
return 0;
}

More Related Content

What's hot

Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in cSaranya saran
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branchingSaranya saran
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11Rumman Ansari
 
How c program execute in c program
How c program execute in c program How c program execute in c program
How c program execute in c program Rumman Ansari
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04hassaanciit
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]Abhishek Sinha
 
Core programming in c
Core programming in cCore programming in c
Core programming in cRahul Pandit
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in CMuthuganesh S
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’eShikshak
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4MKalpanaDevi
 
Simple c program
Simple c programSimple c program
Simple c programRavi Singh
 
Programming in C [Module One]
Programming in C [Module One]Programming in C [Module One]
Programming in C [Module One]Abhishek Sinha
 
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 numberMainak Sasmal
 
Input Output Management In C Programming
Input Output Management In C ProgrammingInput Output Management In C Programming
Input Output Management In C ProgrammingKamal Acharya
 

What's hot (20)

Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
7 functions
7  functions7  functions
7 functions
 
How c program execute in c program
How c program execute in c program How c program execute in c program
How c program execute in c program
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in C
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Simple c program
Simple c programSimple c program
Simple c program
 
Programming in C [Module One]
Programming in C [Module One]Programming in C [Module One]
Programming in C [Module One]
 
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
 
Input And Output
 Input And Output Input And Output
Input And Output
 
Input Output Management In C Programming
Input Output Management In C ProgrammingInput Output Management In C Programming
Input Output Management In C Programming
 
C programming
C programmingC programming
C programming
 
week-10x
week-10xweek-10x
week-10x
 

Viewers also liked

C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1Rumman Ansari
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02Wingston
 
C Programming Language Part 5
C Programming Language Part 5C Programming Language Part 5
C Programming Language Part 5Rumman Ansari
 
Basic c programming and explanation PPT1
Basic c programming and explanation PPT1Basic c programming and explanation PPT1
Basic c programming and explanation PPT1Rumman Ansari
 
My first program in c, hello world !
My first program in c, hello world !My first program in c, hello world !
My first program in c, hello world !Rumman Ansari
 
Medicaid organization profile
Medicaid organization profileMedicaid organization profile
Medicaid organization profileAnurag Byala
 
Основи мови Ci
Основи мови CiОснови мови Ci
Основи мови CiEscuela
 
Steps for Developing a 'C' program
 Steps for Developing a 'C' program Steps for Developing a 'C' program
Steps for Developing a 'C' programSahithi Naraparaju
 
Introduction to programming with c,
Introduction to programming with c,Introduction to programming with c,
Introduction to programming with c,Hossain Md Shakhawat
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C BasicsBharat Kalia
 
Composicio Digital _Practica Pa4
Composicio Digital _Practica Pa4Composicio Digital _Practica Pa4
Composicio Digital _Practica Pa4Marcos Baldovi
 
C programming basics
C  programming basicsC  programming basics
C programming basicsargusacademy
 
11 gezond-is-vetcool
11 gezond-is-vetcool11 gezond-is-vetcool
11 gezond-is-vetcoolanfrancoise
 

Viewers also liked (17)

C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
C Programming Language Part 5
C Programming Language Part 5C Programming Language Part 5
C Programming Language Part 5
 
Basic c programming and explanation PPT1
Basic c programming and explanation PPT1Basic c programming and explanation PPT1
Basic c programming and explanation PPT1
 
My first program in c, hello world !
My first program in c, hello world !My first program in c, hello world !
My first program in c, hello world !
 
Medicaid organization profile
Medicaid organization profileMedicaid organization profile
Medicaid organization profile
 
C language. Introduction
C language. IntroductionC language. Introduction
C language. Introduction
 
Основи мови Ci
Основи мови CiОснови мови Ci
Основи мови Ci
 
C programming tutorial for beginners
C programming tutorial for beginnersC programming tutorial for beginners
C programming tutorial for beginners
 
C tutorial
C tutorialC tutorial
C tutorial
 
Steps for Developing a 'C' program
 Steps for Developing a 'C' program Steps for Developing a 'C' program
Steps for Developing a 'C' program
 
Introduction to programming with c,
Introduction to programming with c,Introduction to programming with c,
Introduction to programming with c,
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C Basics
 
Composicio Digital _Practica Pa4
Composicio Digital _Practica Pa4Composicio Digital _Practica Pa4
Composicio Digital _Practica Pa4
 
C programming basics
C  programming basicsC  programming basics
C programming basics
 
11 gezond-is-vetcool
11 gezond-is-vetcool11 gezond-is-vetcool
11 gezond-is-vetcool
 
Aviation catalog
Aviation catalogAviation catalog
Aviation catalog
 

Similar to C Programming Language Part 7

Similar to C Programming Language Part 7 (20)

Function in c
Function in cFunction in c
Function in c
 
Function in c
Function in cFunction in c
Function in c
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
Function in c program
Function in c programFunction in c program
Function in c program
 
C function
C functionC function
C function
 
Array Cont
Array ContArray Cont
Array Cont
 
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...
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
 
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
 
C and C++ functions
C and C++ functionsC and C++ functions
C and C++ functions
 
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
FunctionsFunctions
Functions
 
Functions in c
Functions in cFunctions in c
Functions in c
 

More from Rumman Ansari

C programming exercises and solutions
C programming exercises and solutions C programming exercises and solutions
C programming exercises and solutions Rumman Ansari
 
Java Tutorial best website
Java Tutorial best websiteJava Tutorial best website
Java Tutorial best websiteRumman Ansari
 
Java Questions and Answers
Java Questions and AnswersJava Questions and Answers
Java Questions and AnswersRumman Ansari
 
C program to write c program without using main function
C program to write c program without using main functionC program to write c program without using main function
C program to write c program without using main functionRumman Ansari
 
Steps for c program execution
Steps for c program executionSteps for c program execution
Steps for c program executionRumman Ansari
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c programRumman Ansari
 
What is token c programming
What is token c programmingWhat is token c programming
What is token c programmingRumman Ansari
 
What is identifier c programming
What is identifier c programmingWhat is identifier c programming
What is identifier c programmingRumman Ansari
 
What is keyword in c programming
What is keyword in c programmingWhat is keyword in c programming
What is keyword in c programmingRumman Ansari
 
Type casting in c programming
Type casting in c programmingType casting in c programming
Type casting in c programmingRumman Ansari
 
C Programming Language Step by Step Part 3
C Programming Language Step by Step Part 3C Programming Language Step by Step Part 3
C Programming Language Step by Step Part 3Rumman Ansari
 
Tail Recursion in data structure
Tail Recursion in data structureTail Recursion in data structure
Tail Recursion in data structureRumman Ansari
 

More from Rumman Ansari (18)

Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
C programming exercises and solutions
C programming exercises and solutions C programming exercises and solutions
C programming exercises and solutions
 
Java Tutorial best website
Java Tutorial best websiteJava Tutorial best website
Java Tutorial best website
 
Java Questions and Answers
Java Questions and AnswersJava Questions and Answers
Java Questions and Answers
 
servlet programming
servlet programmingservlet programming
servlet programming
 
C program to write c program without using main function
C program to write c program without using main functionC program to write c program without using main function
C program to write c program without using main function
 
Steps for c program execution
Steps for c program executionSteps for c program execution
Steps for c program execution
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
What is token c programming
What is token c programmingWhat is token c programming
What is token c programming
 
What is identifier c programming
What is identifier c programmingWhat is identifier c programming
What is identifier c programming
 
What is keyword in c programming
What is keyword in c programmingWhat is keyword in c programming
What is keyword in c programming
 
Type casting in c programming
Type casting in c programmingType casting in c programming
Type casting in c programming
 
C Programming Language Step by Step Part 3
C Programming Language Step by Step Part 3C Programming Language Step by Step Part 3
C Programming Language Step by Step Part 3
 
C Programming
C ProgrammingC Programming
C Programming
 
Tail recursion
Tail recursionTail recursion
Tail recursion
 
Tail Recursion in data structure
Tail Recursion in data structureTail Recursion in data structure
Tail Recursion in data structure
 
Spyware manual
Spyware  manualSpyware  manual
Spyware manual
 
Linked list
Linked listLinked list
Linked list
 

Recently uploaded

The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdfSuman Jyoti
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 

Recently uploaded (20)

The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 

C Programming Language Part 7

  • 2. Functions A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions. A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. Defining a Function: return_type function_name( parameter list ) { body of the function }
  • 3. (a) C program is a collection of one or more functions. Some important case (b)A function gets called when the function name is followed by a semicolon. For example, main( ) { argentina( ) ; } (c)A function is defined when function name is followed by a pair of braces in which one or more statements may be present. For example, argentina( ) { statement 1 ; statement 2 ; statement 3 ; }
  • 4. (d) Any function can be called from any other function. Even main( ) can be called from other functions. For example, main( ) { message( ) ; } message( ) { printf ( "nCan't imagine life without C" ) ; main( ) ; } (e)A function can be called any number of times. For example, main( ) { message( ) ; message( ) ; } message( ) { printf ( "nJewel Thief!!" ) ; }
  • 5. (f) The order in which the functions are defined in a program and the order in which they get called need not necessarily be same. For example, main( ) { message1( ) ; message2( ) ; } message2( ) { printf ( "nBut the butter was bitter" ) ; } message1( ) { printf ( "nMary bought some butter" ) ; } Here, even though message1( ) is getting called before message2( ), still, message1( ) has been defined after message2( ). However, it is advisable to define the functions in the same order in which they are called. This makes the program easier to understand.
  • 6. (g) A function can call itself. Such a process is called ‘recursion’. We would discuss this aspect of C functions later in this chapter. (h) A function can be called from other function, but a function cannot be defined in another function. Thus, the following program code would be wrong, since argentina( ) is being defined inside another function, main( ). main( ) { printf ( "nI am in main" ) ; argentina( ) { printf ( "nI am in argentina" ) ; } }
  • 7. Types of C functions Library function User defined function Library function 1. main() 2. printf() 3. scanf() User defined function #include <stdio.h> void function_name(){ ................ ................ } int main(){ ........... ........... function_name(); ........... ........... }
  • 8. Types of User-defined Functions in C Programming 1. Function with no arguments and no return value 2. Function with no arguments and return value 3. Function with arguments but no return value 4. Function with arguments and return value. Function with no arguments and no return value. /*C program to check whether a number entered by user is prime or not using function with no arguments and no return value*/ #include <stdio.h> void prime(); int main(){ prime(); //No argument is passed to prime(). return 0; } void prime(){ /* There is no return value to calling function main(). Hence, return type of prime() is void */ int num,i,flag=0; printf("Enter positive integer enter to check:n"); scanf("%d",&num); for(i=2;i<=num/2;++i){ if(num%i==0){ flag=1; } } if (flag==1) printf("%d is not prime",num); else printf("%d is prime",num); }
  • 9. Function with no arguments but return value /*C program to check whether a number entered by user is prime or not using function with no arguments but having return value */ #include <stdio.h> int input(); int main(){ int num,i,flag = 0; num=input(); /* No argument is passed to input() */ for(i=2; i<=num/2; ++i){ if(num%i==0){ flag = 1; break; } } if(flag == 1) printf("%d is not prime",num); else printf("%d is prime", num); return 0; } int input(){ /* Integer value is returned from input() to calling function */ int n; printf("Enter positive integer to check:n"); scanf("%d",&n); return n; }
  • 10. Function with arguments and no return value /*Program to check whether a number entered by user is prime or not using function with arguments and no return value */ #include <stdio.h> void check_display(int n); int main(){ int num; printf("Enter positive enter to check:n"); scanf("%d",&num); check_display(num); /* Argument num is passed to function. */ return 0; } void check_display(int n){ /* There is no return value to calling function. Hence, return type of function is void. */ int i, flag = 0; for(i=2; i<=n/2; ++i){ if(n%i==0){ flag = 1; break; } } if(flag == 1) printf("%d is not prime",n); else printf("%d is prime", n); }
  • 11. Function with argument and a return value /* Program to check whether a number entered by user is prime or not using function with argument and return value */ #include <stdio.h> int check(int n); int main(){ int num,num_check=0; printf("Enter positive enter to check:n"); scanf("%d",&num); num_check=check(num); /* Argument num is passed to check() function. */ if(num_check==1) printf("%d is not prime",num); else printf("%d is prime",num); return 0; } int check(int n){ /* Integer value is returned from function check() */ int i; for(i=2;i<=n/2;++i){ if(n%i==0) return 1; } return 0; }
  • 12. void Kulut(int a,int b) { c=a+b; } int Kulut(int a,int b) { c=a+b; return 0; }
  • 13. Example: /* function returning the max between two numbers */ int max(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; }
  • 14. Example: #include<stdio.h> void main() { printf("n I am in main"); argentina(); } void argentina() { printf("nI am in argentina n") ; }
  • 15. #include<stdio.h> void main() { int c; c=max(2,3); printf("n this print is in main() %d n",c); } /* function returning the max between two numbers */ int max(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; printf("n I am in max() %d n",result); return result; } Example:
  • 16. Example of a Function: #include <stdio.h> /* function declaration */ int max(int num1, int num2); int main () { /* local variable definition */ int a = 100; int b = 200; int ret; /* calling a function to get max value */ ret = max(a, b); printf( "Max value is : %dn", ret ); return 0; } /* function returning the max between two numbers */ int max(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; }
  • 17. #include <stdio.h> main( ) { message( ) ; message( ) ; } message( ) { printf ( "n Rahul is Thief!!n" ) ; } Example:
  • 18.
  • 19. Function Arguments Call Type Description Call by value This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. Call by reference This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.
  • 20. call by value in C The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument By default, C programming language uses call by value method to pass arguments. In general, this means that code within a function cannot alter the arguments used to call the function. Consider the function swap() definition as follows. /* function returning the max between two numbers */ int max(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; }
  • 21. #include <stdio.h> /* function declaration */ void swap(int x, int y); int main () { /* local variable definition */ int a = 100; int b = 200; printf("Before swap, value of a : %dn", a ); printf("Before swap, value of b : %dn", b ); /* calling a function to swap the values */ swap(a, b); printf("After swap, value of a : %dn", a ); printf("After swap, value of b : %dn", b ); return 0; }
  • 22. call by reference in C The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument. /* function definition to swap the values */ void swap(int *x, int *y) { int temp; temp = *x; /* save the value at address x */ *x = *y; /* put y into x */ *y = temp; /* put temp into y */ return; }
  • 23. #include <stdio.h> /* function declaration */ void swap(int *x, int *y); int main () { /* local variable definition */ int a = 100; int b = 200; printf("Before swap, value of a : %dn", a ); printf("Before swap, value of b : %dn", b ); /* calling a function to swap the values. * &a indicates pointer to a ie. address of variable a and * &b indicates pointer to b ie. address of variable b. */ swap(&a, &b); printf("After swap, value of a : %dn", a ); printf("After swap, value of b : %dn", b ); return 0; }