C Programming Language Part 7

Rumman Ansari
Rumman AnsariSoftware Engineer
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:
C Programming Language Part 7
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;
}
1 von 23

Recomendados

C Programming Language Step by Step Part 2 von
C Programming Language Step by Step Part 2C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2Rumman Ansari
229 views5 Folien
C Programming Language Part 6 von
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6Rumman Ansari
424 views26 Folien
C Programming Language Part 4 von
C Programming Language Part 4C Programming Language Part 4
C Programming Language Part 4Rumman Ansari
269 views22 Folien
C Programming Language Part 9 von
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9Rumman Ansari
155 views8 Folien
C Programming Language Part 8 von
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8Rumman Ansari
401 views18 Folien
C Programming Language Step by Step Part 5 von
C Programming Language Step by Step Part 5C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5Rumman Ansari
217 views18 Folien

Más contenido relacionado

Was ist angesagt?

Expressions using operator in c von
Expressions using operator in cExpressions using operator in c
Expressions using operator in cSaranya saran
52 views27 Folien
Decision making and branching von
Decision making and branchingDecision making and branching
Decision making and branchingSaranya saran
88 views53 Folien
C Programming Language Part 11 von
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11Rumman Ansari
364 views13 Folien
7 functions von
7  functions7  functions
7 functionsMomenMostafa
81 views44 Folien
How c program execute in c program von
How c program execute in c program How c program execute in c program
How c program execute in c program Rumman Ansari
741 views28 Folien
Introduction to Computer and Programing - Lecture 04 von
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04hassaanciit
1K views12 Folien

Was ist angesagt?(20)

Expressions using operator in c von Saranya saran
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
Saranya saran52 views
Decision making and branching von Saranya saran
Decision making and branchingDecision making and branching
Decision making and branching
Saranya saran88 views
C Programming Language Part 11 von Rumman Ansari
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
Rumman Ansari364 views
How c program execute in c program von Rumman Ansari
How c program execute in c program How c program execute in c program
How c program execute in c program
Rumman Ansari741 views
Introduction to Computer and Programing - Lecture 04 von hassaanciit
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04
hassaanciit1K views
Core programming in c von Rahul Pandit
Core programming in cCore programming in c
Core programming in c
Rahul Pandit6.2K views
Input output statement in C von Muthuganesh S
Input output statement in CInput output statement in C
Input output statement in C
Muthuganesh S954 views
Mesics lecture 5 input – output in ‘c’ von eShikshak
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
eShikshak5.2K views
Functions and pointers_unit_4 von MKalpanaDevi
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
MKalpanaDevi153 views
Practical write a c program to reverse a given number von Mainak Sasmal
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 Sasmal9.9K views
Input Output Management In C Programming von Kamal Acharya
Input Output Management In C ProgrammingInput Output Management In C Programming
Input Output Management In C Programming
Kamal Acharya3.9K views

Destacado

C Programming Language Step by Step Part 1 von
C Programming Language Step by Step Part 1C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1Rumman Ansari
491 views13 Folien
Introduction to Basic C programming 02 von
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02Wingston
9.4K views78 Folien
C Programming Language Part 5 von
C Programming Language Part 5C Programming Language Part 5
C Programming Language Part 5Rumman Ansari
411 views18 Folien
Basic c programming and explanation PPT1 von
Basic c programming and explanation PPT1Basic c programming and explanation PPT1
Basic c programming and explanation PPT1Rumman Ansari
813 views9 Folien
My first program in c, hello world ! von
My first program in c, hello world !My first program in c, hello world !
My first program in c, hello world !Rumman Ansari
867 views13 Folien
Medicaid organization profile von
Medicaid organization profileMedicaid organization profile
Medicaid organization profileAnurag Byala
335 views19 Folien

Destacado(17)

C Programming Language Step by Step Part 1 von Rumman Ansari
C Programming Language Step by Step Part 1C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1
Rumman Ansari491 views
Introduction to Basic C programming 02 von Wingston
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
Wingston9.4K views
C Programming Language Part 5 von Rumman Ansari
C Programming Language Part 5C Programming Language Part 5
C Programming Language Part 5
Rumman Ansari411 views
Basic c programming and explanation PPT1 von Rumman Ansari
Basic c programming and explanation PPT1Basic c programming and explanation PPT1
Basic c programming and explanation PPT1
Rumman Ansari813 views
My first program in c, hello world ! von Rumman Ansari
My first program in c, hello world !My first program in c, hello world !
My first program in c, hello world !
Rumman Ansari867 views
Medicaid organization profile von Anurag Byala
Medicaid organization profileMedicaid organization profile
Medicaid organization profile
Anurag Byala335 views
Основи мови Ci von Escuela
Основи мови CiОснови мови Ci
Основи мови Ci
Escuela5.7K views
Composicio Digital _Practica Pa4 von Marcos Baldovi
Composicio Digital _Practica Pa4Composicio Digital _Practica Pa4
Composicio Digital _Practica Pa4
Marcos Baldovi138 views
C programming basics von argusacademy
C  programming basicsC  programming basics
C programming basics
argusacademy1.6K views
11 gezond-is-vetcool von anfrancoise
11 gezond-is-vetcool11 gezond-is-vetcool
11 gezond-is-vetcool
anfrancoise705 views

Similar a C Programming Language Part 7

Function in c von
Function in cFunction in c
Function in cCGC Technical campus,Mohali
658 views22 Folien
Function in c von
Function in cFunction in c
Function in cRaj Tandukar
6.8K views22 Folien
UNIT3.pptx von
UNIT3.pptxUNIT3.pptx
UNIT3.pptxNagasaiT
6 views39 Folien
Dti2143 chapter 5 von
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5alish sha
736 views38 Folien
Presentation on function von
Presentation on functionPresentation on function
Presentation on functionAbu Zaman
11.2K views20 Folien
function_v1.ppt von
function_v1.pptfunction_v1.ppt
function_v1.pptssuser823678
12 views33 Folien

Similar a C Programming Language Part 7(20)

Dti2143 chapter 5 von alish sha
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
alish sha736 views
Presentation on function von Abu Zaman
Presentation on functionPresentation on function
Presentation on function
Abu Zaman11.2K views
Function in c program von umesh patil
Function in c programFunction in c program
Function in c program
umesh patil1.4K views
Recursion in C von v_jk
Recursion in CRecursion in C
Recursion in C
v_jk741 views
RECURSION IN C von v_jk
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk14.1K views
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx von vekariyakashyap
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
vekariyakashyap13 views
CH.4FUNCTIONS IN C_FYBSC(CS).pptx von SangeetaBorde3
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde330 views

Más de Rumman Ansari

Sql tutorial von
Sql tutorialSql tutorial
Sql tutorialRumman Ansari
1.9K views32 Folien
C programming exercises and solutions von
C programming exercises and solutions C programming exercises and solutions
C programming exercises and solutions Rumman Ansari
468 views2 Folien
Java Tutorial best website von
Java Tutorial best websiteJava Tutorial best website
Java Tutorial best websiteRumman Ansari
68 views2 Folien
Java Questions and Answers von
Java Questions and AnswersJava Questions and Answers
Java Questions and AnswersRumman Ansari
254 views10 Folien
servlet programming von
servlet programmingservlet programming
servlet programmingRumman Ansari
116 views3 Folien
C program to write c program without using main function von
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
847 views6 Folien

Más de Rumman Ansari(18)

C programming exercises and solutions von Rumman Ansari
C programming exercises and solutions C programming exercises and solutions
C programming exercises and solutions
Rumman Ansari468 views
Java Questions and Answers von Rumman Ansari
Java Questions and AnswersJava Questions and Answers
Java Questions and Answers
Rumman Ansari254 views
C program to write c program without using main function von Rumman Ansari
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
Rumman Ansari847 views
Steps for c program execution von Rumman Ansari
Steps for c program executionSteps for c program execution
Steps for c program execution
Rumman Ansari13.1K views
What is token c programming von Rumman Ansari
What is token c programmingWhat is token c programming
What is token c programming
Rumman Ansari2.3K views
What is identifier c programming von Rumman Ansari
What is identifier c programmingWhat is identifier c programming
What is identifier c programming
Rumman Ansari9.9K views
What is keyword in c programming von Rumman Ansari
What is keyword in c programmingWhat is keyword in c programming
What is keyword in c programming
Rumman Ansari3.8K views
Type casting in c programming von Rumman Ansari
Type casting in c programmingType casting in c programming
Type casting in c programming
Rumman Ansari4.4K views
C Programming Language Step by Step Part 3 von Rumman Ansari
C Programming Language Step by Step Part 3C Programming Language Step by Step Part 3
C Programming Language Step by Step Part 3
Rumman Ansari200 views
Tail Recursion in data structure von Rumman Ansari
Tail Recursion in data structureTail Recursion in data structure
Tail Recursion in data structure
Rumman Ansari4K views

Último

_MAKRIADI-FOTEINI_diploma thesis.pptx von
_MAKRIADI-FOTEINI_diploma thesis.pptx_MAKRIADI-FOTEINI_diploma thesis.pptx
_MAKRIADI-FOTEINI_diploma thesis.pptxfotinimakriadi
12 views32 Folien
Proposal Presentation.pptx von
Proposal Presentation.pptxProposal Presentation.pptx
Proposal Presentation.pptxkeytonallamon
67 views36 Folien
SUMIT SQL PROJECT SUPERSTORE 1.pptx von
SUMIT SQL PROJECT SUPERSTORE 1.pptxSUMIT SQL PROJECT SUPERSTORE 1.pptx
SUMIT SQL PROJECT SUPERSTORE 1.pptxSumit Jadhav
22 views26 Folien
MongoDB.pdf von
MongoDB.pdfMongoDB.pdf
MongoDB.pdfArthyR3
49 views6 Folien
sam_software_eng_cv.pdf von
sam_software_eng_cv.pdfsam_software_eng_cv.pdf
sam_software_eng_cv.pdfsammyigbinovia
10 views5 Folien
GDSC Mikroskil Members Onboarding 2023.pdf von
GDSC Mikroskil Members Onboarding 2023.pdfGDSC Mikroskil Members Onboarding 2023.pdf
GDSC Mikroskil Members Onboarding 2023.pdfgdscmikroskil
63 views62 Folien

Último(20)

_MAKRIADI-FOTEINI_diploma thesis.pptx von fotinimakriadi
_MAKRIADI-FOTEINI_diploma thesis.pptx_MAKRIADI-FOTEINI_diploma thesis.pptx
_MAKRIADI-FOTEINI_diploma thesis.pptx
fotinimakriadi12 views
SUMIT SQL PROJECT SUPERSTORE 1.pptx von Sumit Jadhav
SUMIT SQL PROJECT SUPERSTORE 1.pptxSUMIT SQL PROJECT SUPERSTORE 1.pptx
SUMIT SQL PROJECT SUPERSTORE 1.pptx
Sumit Jadhav 22 views
MongoDB.pdf von ArthyR3
MongoDB.pdfMongoDB.pdf
MongoDB.pdf
ArthyR349 views
GDSC Mikroskil Members Onboarding 2023.pdf von gdscmikroskil
GDSC Mikroskil Members Onboarding 2023.pdfGDSC Mikroskil Members Onboarding 2023.pdf
GDSC Mikroskil Members Onboarding 2023.pdf
gdscmikroskil63 views
REACTJS.pdf von ArthyR3
REACTJS.pdfREACTJS.pdf
REACTJS.pdf
ArthyR337 views
ASSIGNMENTS ON FUZZY LOGIC IN TRAFFIC FLOW.pdf von AlhamduKure
ASSIGNMENTS ON FUZZY LOGIC IN TRAFFIC FLOW.pdfASSIGNMENTS ON FUZZY LOGIC IN TRAFFIC FLOW.pdf
ASSIGNMENTS ON FUZZY LOGIC IN TRAFFIC FLOW.pdf
AlhamduKure8 views
Web Dev Session 1.pptx von VedVekhande
Web Dev Session 1.pptxWeb Dev Session 1.pptx
Web Dev Session 1.pptx
VedVekhande17 views
Design of machine elements-UNIT 3.pptx von gopinathcreddy
Design of machine elements-UNIT 3.pptxDesign of machine elements-UNIT 3.pptx
Design of machine elements-UNIT 3.pptx
gopinathcreddy37 views
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx von lwang78
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
lwang78180 views
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc... von csegroupvn
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...
csegroupvn8 views
Design_Discover_Develop_Campaign.pptx von ShivanshSeth6
Design_Discover_Develop_Campaign.pptxDesign_Discover_Develop_Campaign.pptx
Design_Discover_Develop_Campaign.pptx
ShivanshSeth649 views
Ansari: Practical experiences with an LLM-based Islamic Assistant von M Waleed Kadous
Ansari: Practical experiences with an LLM-based Islamic AssistantAnsari: Practical experiences with an LLM-based Islamic Assistant
Ansari: Practical experiences with an LLM-based Islamic Assistant
M Waleed Kadous9 views

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:
  • 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; }