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 de 23

Más contenido relacionado

La actualidad más candente

7  functions7  functions
7 functionsMomenMostafa
81 vistas44 diapositivas

La actualidad más candente(20)

Expressions using operator in cExpressions using operator in c
Expressions using operator in c
Saranya saran52 vistas
Decision making and branchingDecision making and branching
Decision making and branching
Saranya saran88 vistas
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
Rumman Ansari364 vistas
7  functions7  functions
7 functions
MomenMostafa81 vistas
How c program execute in c program How c program execute in c program
How c program execute in c program
Rumman Ansari739 vistas
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
Abhishek Sinha680 vistas
88 c-programs88 c-programs
88 c-programs
Leandro Schenone953 vistas
Core programming in cCore programming in c
Core programming in c
Rahul Pandit6.2K vistas
Input output statement in CInput output statement in C
Input output statement in C
Muthuganesh S924 vistas
C lab-programsC lab-programs
C lab-programs
Tony Kurishingal9.1K vistas
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
MKalpanaDevi153 vistas
Simple c programSimple c program
Simple c program
Ravi Singh3K vistas
Programming in C [Module One]Programming in C [Module One]
Programming in C [Module One]
Abhishek Sinha561 vistas
 Input And Output Input And Output
Input And Output
Ghaffar Khan2.5K vistas
Input Output Management In C ProgrammingInput Output Management In C Programming
Input Output Management In C Programming
Kamal Acharya3.9K vistas
C programmingC programming
C programming
Samsil Arefin464 vistas
week-10xweek-10x
week-10x
KITE www.kitecolleges.com595 vistas

Destacado(17)

C Programming Language Part 5C Programming Language Part 5
C Programming Language Part 5
Rumman Ansari411 vistas
My first program in c, hello world !My first program in c, hello world !
My first program in c, hello world !
Rumman Ansari863 vistas
Medicaid organization profileMedicaid organization profile
Medicaid organization profile
Anurag Byala335 vistas
C language. IntroductionC language. Introduction
C language. Introduction
Alexey Bovanenko518 vistas
Основи мови CiОснови мови Ci
Основи мови Ci
Escuela5.6K vistas
C programming tutorial for beginnersC programming tutorial for beginners
C programming tutorial for beginners
Thiyagarajan Soundhiran905 vistas
C tutorialC tutorial
C tutorial
Chukka Nikhil Chakravarthy1.3K vistas
 Steps for Developing a 'C' program Steps for Developing a 'C' program
Steps for Developing a 'C' program
Sahithi Naraparaju8.5K vistas
Introduction to programming with c,Introduction to programming with c,
Introduction to programming with c,
Hossain Md Shakhawat5.7K vistas
Programming in C BasicsProgramming in C Basics
Programming in C Basics
Bharat Kalia4.9K vistas
Composicio Digital _Practica Pa4Composicio Digital _Practica Pa4
Composicio Digital _Practica Pa4
Marcos Baldovi138 vistas
C  programming basicsC  programming basics
C programming basics
argusacademy1.6K vistas
11 gezond-is-vetcool11 gezond-is-vetcool
11 gezond-is-vetcool
anfrancoise705 vistas
Aviation catalogAviation catalog
Aviation catalog
clockworkalpha143 vistas

Similar a C Programming Language Part 7

Function in cFunction in c
Function in cCGC Technical campus,Mohali
655 vistas22 diapositivas
Function in cFunction in c
Function in cRaj Tandukar
6.8K vistas22 diapositivas
UNIT3.pptxUNIT3.pptx
UNIT3.pptxNagasaiT
6 vistas39 diapositivas
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5alish sha
736 vistas38 diapositivas
Presentation on functionPresentation on function
Presentation on functionAbu Zaman
11.2K vistas20 diapositivas
function_v1.pptfunction_v1.ppt
function_v1.pptssuser823678
12 vistas33 diapositivas

Similar a C Programming Language Part 7(20)

Function in cFunction in c
Function in c
CGC Technical campus,Mohali655 vistas
Function in cFunction in c
Function in c
Raj Tandukar6.8K vistas
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
NagasaiT6 vistas
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
alish sha736 vistas
Presentation on functionPresentation on function
Presentation on function
Abu Zaman11.2K vistas
function_v1.pptfunction_v1.ppt
function_v1.ppt
ssuser82367812 vistas
Functions in CFunctions in C
Functions in C
Shobhit Upadhyay4K vistas
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
psaravanan198573 vistas
Function in c programFunction in c program
Function in c program
umesh patil1.3K vistas
C functionC function
C function
thirumalaikumar3493 vistas
Array ContArray Cont
Array Cont
Ashutosh Srivasatava456 vistas
Recursion in CRecursion in C
Recursion in C
v_jk741 vistas
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk13.9K vistas
C and C++ functionsC and C++ functions
C and C++ functions
kavitha muneeshwaran1.3K vistas
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde329 vistas
FunctionsFunctions
Functions
zeeshan841146 vistas
Functions in cFunctions in c
Functions in c
KavithaMuralidharan2175 vistas
FunctionsFunctions
Functions
SANTOSH RATH365 vistas
FunctionincprogramFunctionincprogram
Functionincprogram
Sampath Kumar44 vistas

Más de Rumman Ansari

Sql tutorialSql tutorial
Sql tutorialRumman Ansari
1.9K vistas32 diapositivas
Java Tutorial best websiteJava Tutorial best website
Java Tutorial best websiteRumman Ansari
68 vistas2 diapositivas
Java Questions and AnswersJava Questions and Answers
Java Questions and AnswersRumman Ansari
245 vistas10 diapositivas
servlet programmingservlet programming
servlet programmingRumman Ansari
116 vistas3 diapositivas

Más de Rumman Ansari(18)

Sql tutorialSql tutorial
Sql tutorial
Rumman Ansari1.9K vistas
C programming exercises and solutions C programming exercises and solutions
C programming exercises and solutions
Rumman Ansari467 vistas
Java Tutorial best websiteJava Tutorial best website
Java Tutorial best website
Rumman Ansari68 vistas
Java Questions and AnswersJava Questions and Answers
Java Questions and Answers
Rumman Ansari245 vistas
servlet programmingservlet programming
servlet programming
Rumman Ansari116 vistas
Steps for c program executionSteps for c program execution
Steps for c program execution
Rumman Ansari13.1K vistas
Pointer in c programPointer in c program
Pointer in c program
Rumman Ansari1.8K vistas
What is token c programmingWhat is token c programming
What is token c programming
Rumman Ansari2.3K vistas
What is identifier c programmingWhat is identifier c programming
What is identifier c programming
Rumman Ansari9.8K vistas
What is keyword in c programmingWhat is keyword in c programming
What is keyword in c programming
Rumman Ansari3.8K vistas
Type casting in c programmingType casting in c programming
Type casting in c programming
Rumman Ansari4.4K vistas
C ProgrammingC Programming
C Programming
Rumman Ansari256 vistas
Tail recursionTail recursion
Tail recursion
Rumman Ansari1.8K vistas
Tail Recursion in data structureTail Recursion in data structure
Tail Recursion in data structure
Rumman Ansari4K vistas
Spyware  manualSpyware  manual
Spyware manual
Rumman Ansari706 vistas
Linked listLinked list
Linked list
Rumman Ansari314 vistas

Último(20)

Wire Cutting & StrippingWire Cutting & Stripping
Wire Cutting & Stripping
Iwiss Tools Co.,Ltd6 vistas
CHI-SQUARE ( χ2) TESTS.pptxCHI-SQUARE ( χ2) TESTS.pptx
CHI-SQUARE ( χ2) TESTS.pptx
ssusera597c511 vistas
cloud computing-virtualization.pptxcloud computing-virtualization.pptx
cloud computing-virtualization.pptx
RajaulKarim2072 vistas
Codes and Conventions.pptxCodes and Conventions.pptx
Codes and Conventions.pptx
IsabellaGraceAnkers5 vistas
SICTECH CORPORATE PRESENTATIONSICTECH CORPORATE PRESENTATION
SICTECH CORPORATE PRESENTATION
SiCtechInduction15 vistas
IWISS Catalog 2022IWISS Catalog 2022
IWISS Catalog 2022
Iwiss Tools Co.,Ltd22 vistas
FLOW IN PIPES NOTES.pdfFLOW IN PIPES NOTES.pdf
FLOW IN PIPES NOTES.pdf
Dearest Arhelo82 vistas
String.pptxString.pptx
String.pptx
Ananthi Palanisamy45 vistas
Investor PresentationInvestor Presentation
Investor Presentation
eser sevinç10 vistas
Pointers.pptxPointers.pptx
Pointers.pptx
Ananthi Palanisamy58 vistas
Object Oriented Programming with JAVAObject Oriented Programming with JAVA
Object Oriented Programming with JAVA
Demian Antony D'Mello49 vistas
SEMI CONDUCTORSSEMI CONDUCTORS
SEMI CONDUCTORS
pavaniaalla200515 vistas

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