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

More Related Content

PPTX
C Programming Language Step by Step Part 2
PPTX
C Programming Language Part 6
PPTX
C Programming Language Part 4
PPTX
C Programming Language Part 9
PPTX
C Programming Language Part 8
PPTX
C Programming Language Step by Step Part 5
PPSX
C programming function
PDF
1 introducing c language
C Programming Language Step by Step Part 2
C Programming Language Part 6
C Programming Language Part 4
C Programming Language Part 9
C Programming Language Part 8
C Programming Language Step by Step Part 5
C programming function
1 introducing c language

What's hot (20)

PPTX
Expressions using operator in c
PPTX
Decision making and branching
PPTX
C Programming Language Part 11
PDF
7 functions
PPTX
How c program execute in c program
PDF
Introduction to Computer and Programing - Lecture 04
PPSX
Concepts of C [Module 2]
PDF
88 c-programs
DOCX
Core programming in c
PPTX
Input output statement in C
DOC
C lab-programs
PPT
Mesics lecture 5 input – output in ‘c’
PPT
Functions and pointers_unit_4
PPTX
Simple c program
PPSX
Programming in C [Module One]
DOCX
Practical write a c program to reverse a given number
PPT
Input And Output
PPTX
Input Output Management In C Programming
PDF
C programming
Expressions using operator in c
Decision making and branching
C Programming Language Part 11
7 functions
How c program execute in c program
Introduction to Computer and Programing - Lecture 04
Concepts of C [Module 2]
88 c-programs
Core programming in c
Input output statement in C
C lab-programs
Mesics lecture 5 input – output in ‘c’
Functions and pointers_unit_4
Simple c program
Programming in C [Module One]
Practical write a c program to reverse a given number
Input And Output
Input Output Management In C Programming
C programming
Ad

Viewers also liked (17)

PPTX
C Programming Language Step by Step Part 1
PPTX
Introduction to Basic C programming 02
PPTX
C Programming Language Part 5
PPTX
Basic c programming and explanation PPT1
PPTX
My first program in c, hello world !
PPTX
Medicaid organization profile
ODP
C language. Introduction
PPTX
Основи мови Ci
PPTX
C programming tutorial for beginners
PPT
Steps for Developing a 'C' program
PPT
Introduction to programming with c,
PPTX
Programming in C Basics
PDF
Composicio Digital _Practica Pa4
PPSX
C programming basics
PPTX
11 gezond-is-vetcool
PPTX
Aviation catalog
C Programming Language Step by Step Part 1
Introduction to Basic C programming 02
C Programming Language Part 5
Basic c programming and explanation PPT1
My first program in c, hello world !
Medicaid organization profile
C language. Introduction
Основи мови Ci
C programming tutorial for beginners
Steps for Developing a 'C' program
Introduction to programming with c,
Programming in C Basics
Composicio Digital _Practica Pa4
C programming basics
11 gezond-is-vetcool
Aviation catalog
Ad

Similar to C Programming Language Part 7 (20)

PPTX
Detailed concept of function in c programming
PPTX
CH.4FUNCTIONS IN C (1).pptx
PPTX
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
PPTX
UNIT3.pptx
PDF
programlama fonksiyonlar c++ hjhjghjv jg
PPTX
PPTX
Function (rule in programming)
PDF
Principals of Programming in CModule -5.pdfModule-3.pdf
DOC
Functions struct&union
PPTX
Presentation on Function in C Programming
PPTX
Functions in c language
PPTX
Functions in c language
PDF
unit3 part2 pcds function notes.pdf
PPTX
CHAPTER 6
PDF
Functions
DOC
4. function
PDF
Programming in C Functions PPT Presentation.pdf
PPTX
unit_2.pptx
PPTX
Functions in C
PPTX
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Detailed concept of function in c programming
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
UNIT3.pptx
programlama fonksiyonlar c++ hjhjghjv jg
Function (rule in programming)
Principals of Programming in CModule -5.pdfModule-3.pdf
Functions struct&union
Presentation on Function in C Programming
Functions in c language
Functions in c language
unit3 part2 pcds function notes.pdf
CHAPTER 6
Functions
4. function
Programming in C Functions PPT Presentation.pdf
unit_2.pptx
Functions in C
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx

More from Rumman Ansari (18)

PDF
Sql tutorial
PDF
C programming exercises and solutions
PDF
Java Tutorial best website
DOCX
Java Questions and Answers
DOCX
servlet programming
PPTX
C program to write c program without using main function
PPTX
Steps for c program execution
PPTX
Pointer in c program
PPTX
What is token c programming
PPTX
What is identifier c programming
PPTX
What is keyword in c programming
PPTX
Type casting in c programming
PPTX
C Programming Language Step by Step Part 3
DOCX
C Programming
PPTX
Tail recursion
PPTX
Tail Recursion in data structure
PDF
Spyware manual
PPTX
Linked list
Sql tutorial
C programming exercises and solutions
Java Tutorial best website
Java Questions and Answers
servlet programming
C program to write c program without using main function
Steps for c program execution
Pointer in c program
What is token c programming
What is identifier c programming
What is keyword in c programming
Type casting in c programming
C Programming Language Step by Step Part 3
C Programming
Tail recursion
Tail Recursion in data structure
Spyware manual
Linked list

Recently uploaded (20)

PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PDF
composite construction of structures.pdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
PPT on Performance Review to get promotions
PPT
Project quality management in manufacturing
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Well-logging-methods_new................
DOCX
573137875-Attendance-Management-System-original
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Structs to JSON How Go Powers REST APIs.pdf
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
OOP with Java - Java Introduction (Basics)
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
CH1 Production IntroductoryConcepts.pptx
UNIT 4 Total Quality Management .pptx
composite construction of structures.pdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Operating System & Kernel Study Guide-1 - converted.pdf
Arduino robotics embedded978-1-4302-3184-4.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPT on Performance Review to get promotions
Project quality management in manufacturing
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Well-logging-methods_new................
573137875-Attendance-Management-System-original
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx

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