SlideShare a Scribd company logo
FUNCTIONS 
EC201- FUNDAMENTAL 
PROGRAMMING
What is Function 
 Block of code that performs a specific task. 
 It has a name and it is reusable i.e. it can be executed 
from as many different parts in a C Program as required. 
Every function has a unique name. This name is used to 
call function from “main()” function. A function can be 
called from within another function. 
 It also optionally returns a value to the calling program 
 Performs a specific task. A task is a distinct job that your 
program must perform as a part of its overall operation, 
such as adding two or more integer, sorting an array into 
numerical order, or calculating a cube root etc.
Sample Function Call 
#include <stdio.h> 
int main ( ) printf is the name of a predefined 
{ function in the stdio library 
printf (“Hello World!n”) ; this statement is 
return 0 ; is known as a 
} function call 
this is a string we are passing 
as an argument (parameter) to 
the printf function
Function 
• The ‘building blocks’ of a C program 
▫ You’ve used predefined functions already: 
 main() 
 printf(), scanf(), pow() 
▫ User-defined functions 
 Your own code 
 In combination with predefined functions
Sample User-Defined Function 
#include <stdio.h> 
void printMessage ( void ) ; 
int main ( void ) 
{ 
printMessage ( ) ; 
return 0 ; 
} 
void printMessage ( void ) 
{ 
printf (“A message for you:nn”) ; 
printf (“Have a nice day!n”) ; 
}
Examining printMessage 
#include <stdio.h> 
void printMessage ( void ) ; function prototype 
int main ( void ) 
{ 
printMessage ( ) ; function call 
return 0 ; 
} 
void printMessage ( void ) function header 
{ 
printf (“A message for you:nn”) ; function 
printf (“Have a nice day!n”) ; body 
} 
function definition
Program example: functions 
#include<stdio.h> 
kira(int t_semasa, int t_lahir); 
main() 
{ 
int t_semasa,t_lahir,u; 
printf("Please enter your birth year:"); 
scanf("%d",&t_lahir); 
printf("Please enter the current year:"); 
scanf("%d",&t_semasa); 
u = kira(t_semasa,t_lahir); 
printf("nYour age is: %d years oldnn",u); 
return 0; 
} 
kira(int t_semasa, int t_lahir) 
{ 
int umur; 
umur = t_semasa-t_lahir; 
return(umur); 
}
Types of functions 
Functions with no arguments and no return values. 
Functions with arguments and no return values. 
Functions with arguments and return values. 
Functions that return multiple values. 
Functions with no arguments and return values.
Functions with no arguments and no 
return value 
 A C function without any 
arguments means you cannot 
pass data (values like int, char 
etc) to the called function. 
 Similarly, function with no 
return type does not pass back 
data to the calling function. It is 
one of the simplest types of 
function in C. 
 This type of function which 
does not return any value 
cannot be used in an expression 
it can be used only as 
independent statement.
Program example: Functions with no arguments and no return 
value 
#include<stdio.h> 
cetak_line1(); 
cetak_line2(); 
main() 
{ 
printf("Welcome to function in C"); 
cetak_line1(); 
printf("Function easy to learn."); 
cetak_line1(); 
printf("Please make sure you really understand about this topic."); 
cetak_line2(); 
return 0; 
} 
cetak_line1() 
{ 
int i; 
printf("n"); 
for(i=0;i<30;i++) 
{ 
printf("-"); 
} 
printf("n"); 
return 0; 
} 
cetak_line2() 
{ 
printf("n--------------------------------------------------------"); 
return 0; 
}
Functions with arguments and no 
return value 
 In our previous example what we have 
noticed that “main()” function has no 
control over the UDF “printfline()”, it 
cannot control its output. Whenever 
“main()” calls “printline()”, it simply 
prints line every time. So the result 
remains the same. 
 A C function with arguments can 
perform much better than previous 
function type. 
 This type of function can accept data 
from calling function. In other words, 
you send data to the called function 
from calling function but you cannot 
send result data back to the calling 
function. Rather, it displays the result 
on the terminal. But we can control the 
output of function by providing various 
values as arguments.
Program example: Functions with arguments and no return value 
#include<stdio.h> 
tambah(int x, int y); 
main() 
{ 
int x,y; 
printf("Please enter a value x:"); 
scanf("%d",&x); 
printf("Please enter a value y:"); 
scanf("%d",&y); 
tambah(x,y); 
return 0; 
} 
tambah(int x, int y) 
{ 
int result; 
result = x+y; 
printf("Sum of %d and %d is %d.nn",x,y,result); 
return 0; 
}
Functions with arguments and return 
value 
 This type of function can send 
arguments (data) from the 
calling function to the called 
function and wait for the result to 
be returned back from the called 
function back to the calling 
function. 
 This type of function is mostly 
used in programming world 
because it can do two way 
communications; it can accept 
data as arguments as well as can 
send back data as return value. 
 The data returned by the 
function can be used later in our 
program for further calculations.
Program example: Functions with arguments and return value 
#include<stdio.h> 
tambah(int x, int y); 
main() 
{ 
int x,y,z; 
printf("Please enter a value x:"); 
scanf("%d",&x); 
printf("Please enter a value y:"); 
scanf("%d",&y); 
z = tambah(x,y); 
printf("Result %d.nn",z); 
return 0; 
} 
tambah(int x, int y) 
{ 
int result; 
result = x+y; 
return(result); 
}
Functions with no arguments but 
returns value 
 We may need a function which 
does not take any argument but 
only returns values to the 
calling function then this type 
of function is useful. The best 
example of this type of function 
is “getchar()” library function 
which is declared in the header 
file “stdio.h”. We can declare a 
similar library function of own.
Program example: Functions with no arguments but returns value 
#include<stdio.h> 
send(); 
main() 
{ 
int z; 
z = send(); 
printf("nYou entered : %d.", z); 
return 0; 
} 
send() 
{ 
int no1; 
printf("Enter a no : "); 
scanf("%d",&no1); 
return(no1); 
}
Functions that return multiple values 
• We have used arguments to send values to the 
called function, in the same way we can also use 
arguments to send back information to the 
calling function. 
• The arguments that are used to send back data 
are called Output Parameters. 
• Is a bit difficult for novice because this type of 
function uses pointer.
Program example: Functions that return multiple values 
#include<stdio.h> 
calc(int x, int y, int *add, int *sub); 
main() 
{ 
int a,b,p,q; 
printf("Please enter a value a:"); 
scanf("%d",&a); 
printf("Please enter a value b:"); 
scanf("%d",&b); 
calc(a,b,&p,&q); 
printf("Sum = %d, Sub = %d",p,q); 
return 0; 
} 
calc(int x, int y, int *add, int *sub) 
{ 
*add = x+y; 
*sub = x-y; 
return (0); 
}

More Related Content

What's hot

Function in c
Function in cFunction in c
Types of function call
Types of function callTypes of function call
Types of function call
ArijitDhali
 
Call by value
Call by valueCall by value
Call by valueDharani G
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
imtiazalijoono
 
function in c
function in cfunction in c
function in c
subam3
 
Function & Recursion in C
Function & Recursion in CFunction & Recursion in C
Function & Recursion in C
Aditya Nihal Kumar Singh
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
MKalpanaDevi
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
Sampath Kumar
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
UMA PARAMESWARI
 
Function lecture
Function lectureFunction lecture
Function lecture
DIT University, Dehradun
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
Janani Satheshkumar
 
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
 
Functions
FunctionsFunctions
Functions
Pragnavi Erva
 
C Prog - Functions
C Prog - FunctionsC Prog - Functions
C Prog - Functionsvinay arora
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
Saranya saran
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
psaravanan1985
 

What's hot (20)

Function in c
Function in cFunction in c
Function in c
 
Types of function call
Types of function callTypes of function call
Types of function call
 
Call by value
Call by valueCall by value
Call by value
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
function in c
function in cfunction in c
function in c
 
Function & Recursion in C
Function & Recursion in CFunction & Recursion in C
Function & Recursion in C
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Functions
FunctionsFunctions
Functions
 
Function lecture
Function lectureFunction lecture
Function lecture
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
 
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
 
Functions
FunctionsFunctions
Functions
 
C Prog - Functions
C Prog - FunctionsC Prog - Functions
C Prog - Functions
 
Function
FunctionFunction
Function
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 

Similar to CHAPTER 6

Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
Chandrakant Divate
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
KarthikSivagnanam2
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
Saranya saran
 
Function
FunctionFunction
Function
mshoaib15
 
Functions
FunctionsFunctions
Functions
PralhadKhanal1
 
Array Cont
Array ContArray Cont
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
Venkatesh Goud
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5alish sha
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
2017eee0459
 
Functions in c
Functions in cFunctions in c
Functions in c
KavithaMuralidharan2
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
Amarjith C K
 
Functions
Functions Functions
Functions
Dr.Subha Krishna
 
Functions in c
Functions in cFunctions in c
Functions in c
kalavathisugan
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
JVenkateshGoud
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
Rumman Ansari
 
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
vekariyakashyap
 

Similar to CHAPTER 6 (20)

Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Function
FunctionFunction
Function
 
Functions
FunctionsFunctions
Functions
 
Array Cont
Array ContArray Cont
Array Cont
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
 
Functions in c
Functions in cFunctions in c
Functions in c
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
 
Functions
Functions Functions
Functions
 
Functions in c
Functions in cFunctions in c
Functions in c
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
6. function
6. function6. function
6. function
 
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
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 

More from mohd_mizan

Mind map chapter 4
Mind map chapter 4Mind map chapter 4
Mind map chapter 4
mohd_mizan
 
Dbs 1012 synopsis
Dbs 1012 synopsisDbs 1012 synopsis
Dbs 1012 synopsis
mohd_mizan
 
Chapter 4 work, energy and power
Chapter 4 work, energy and powerChapter 4 work, energy and power
Chapter 4 work, energy and power
mohd_mizan
 
CHAPTER 3
CHAPTER 3CHAPTER 3
CHAPTER 3
mohd_mizan
 
CHAPTER 4
CHAPTER 4CHAPTER 4
CHAPTER 4
mohd_mizan
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
mohd_mizan
 
CHAPTER 2
CHAPTER 2CHAPTER 2
CHAPTER 2
mohd_mizan
 
CHAPTER 1
CHAPTER 1CHAPTER 1
CHAPTER 1
mohd_mizan
 

More from mohd_mizan (8)

Mind map chapter 4
Mind map chapter 4Mind map chapter 4
Mind map chapter 4
 
Dbs 1012 synopsis
Dbs 1012 synopsisDbs 1012 synopsis
Dbs 1012 synopsis
 
Chapter 4 work, energy and power
Chapter 4 work, energy and powerChapter 4 work, energy and power
Chapter 4 work, energy and power
 
CHAPTER 3
CHAPTER 3CHAPTER 3
CHAPTER 3
 
CHAPTER 4
CHAPTER 4CHAPTER 4
CHAPTER 4
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
 
CHAPTER 2
CHAPTER 2CHAPTER 2
CHAPTER 2
 
CHAPTER 1
CHAPTER 1CHAPTER 1
CHAPTER 1
 

Recently uploaded

Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 

Recently uploaded (20)

Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 

CHAPTER 6

  • 2. What is Function  Block of code that performs a specific task.  It has a name and it is reusable i.e. it can be executed from as many different parts in a C Program as required. Every function has a unique name. This name is used to call function from “main()” function. A function can be called from within another function.  It also optionally returns a value to the calling program  Performs a specific task. A task is a distinct job that your program must perform as a part of its overall operation, such as adding two or more integer, sorting an array into numerical order, or calculating a cube root etc.
  • 3. Sample Function Call #include <stdio.h> int main ( ) printf is the name of a predefined { function in the stdio library printf (“Hello World!n”) ; this statement is return 0 ; is known as a } function call this is a string we are passing as an argument (parameter) to the printf function
  • 4. Function • The ‘building blocks’ of a C program ▫ You’ve used predefined functions already:  main()  printf(), scanf(), pow() ▫ User-defined functions  Your own code  In combination with predefined functions
  • 5. Sample User-Defined Function #include <stdio.h> void printMessage ( void ) ; int main ( void ) { printMessage ( ) ; return 0 ; } void printMessage ( void ) { printf (“A message for you:nn”) ; printf (“Have a nice day!n”) ; }
  • 6.
  • 7. Examining printMessage #include <stdio.h> void printMessage ( void ) ; function prototype int main ( void ) { printMessage ( ) ; function call return 0 ; } void printMessage ( void ) function header { printf (“A message for you:nn”) ; function printf (“Have a nice day!n”) ; body } function definition
  • 8. Program example: functions #include<stdio.h> kira(int t_semasa, int t_lahir); main() { int t_semasa,t_lahir,u; printf("Please enter your birth year:"); scanf("%d",&t_lahir); printf("Please enter the current year:"); scanf("%d",&t_semasa); u = kira(t_semasa,t_lahir); printf("nYour age is: %d years oldnn",u); return 0; } kira(int t_semasa, int t_lahir) { int umur; umur = t_semasa-t_lahir; return(umur); }
  • 9. Types of functions Functions with no arguments and no return values. Functions with arguments and no return values. Functions with arguments and return values. Functions that return multiple values. Functions with no arguments and return values.
  • 10. Functions with no arguments and no return value  A C function without any arguments means you cannot pass data (values like int, char etc) to the called function.  Similarly, function with no return type does not pass back data to the calling function. It is one of the simplest types of function in C.  This type of function which does not return any value cannot be used in an expression it can be used only as independent statement.
  • 11. Program example: Functions with no arguments and no return value #include<stdio.h> cetak_line1(); cetak_line2(); main() { printf("Welcome to function in C"); cetak_line1(); printf("Function easy to learn."); cetak_line1(); printf("Please make sure you really understand about this topic."); cetak_line2(); return 0; } cetak_line1() { int i; printf("n"); for(i=0;i<30;i++) { printf("-"); } printf("n"); return 0; } cetak_line2() { printf("n--------------------------------------------------------"); return 0; }
  • 12. Functions with arguments and no return value  In our previous example what we have noticed that “main()” function has no control over the UDF “printfline()”, it cannot control its output. Whenever “main()” calls “printline()”, it simply prints line every time. So the result remains the same.  A C function with arguments can perform much better than previous function type.  This type of function can accept data from calling function. In other words, you send data to the called function from calling function but you cannot send result data back to the calling function. Rather, it displays the result on the terminal. But we can control the output of function by providing various values as arguments.
  • 13. Program example: Functions with arguments and no return value #include<stdio.h> tambah(int x, int y); main() { int x,y; printf("Please enter a value x:"); scanf("%d",&x); printf("Please enter a value y:"); scanf("%d",&y); tambah(x,y); return 0; } tambah(int x, int y) { int result; result = x+y; printf("Sum of %d and %d is %d.nn",x,y,result); return 0; }
  • 14. Functions with arguments and return value  This type of function can send arguments (data) from the calling function to the called function and wait for the result to be returned back from the called function back to the calling function.  This type of function is mostly used in programming world because it can do two way communications; it can accept data as arguments as well as can send back data as return value.  The data returned by the function can be used later in our program for further calculations.
  • 15. Program example: Functions with arguments and return value #include<stdio.h> tambah(int x, int y); main() { int x,y,z; printf("Please enter a value x:"); scanf("%d",&x); printf("Please enter a value y:"); scanf("%d",&y); z = tambah(x,y); printf("Result %d.nn",z); return 0; } tambah(int x, int y) { int result; result = x+y; return(result); }
  • 16. Functions with no arguments but returns value  We may need a function which does not take any argument but only returns values to the calling function then this type of function is useful. The best example of this type of function is “getchar()” library function which is declared in the header file “stdio.h”. We can declare a similar library function of own.
  • 17. Program example: Functions with no arguments but returns value #include<stdio.h> send(); main() { int z; z = send(); printf("nYou entered : %d.", z); return 0; } send() { int no1; printf("Enter a no : "); scanf("%d",&no1); return(no1); }
  • 18. Functions that return multiple values • We have used arguments to send values to the called function, in the same way we can also use arguments to send back information to the calling function. • The arguments that are used to send back data are called Output Parameters. • Is a bit difficult for novice because this type of function uses pointer.
  • 19. Program example: Functions that return multiple values #include<stdio.h> calc(int x, int y, int *add, int *sub); main() { int a,b,p,q; printf("Please enter a value a:"); scanf("%d",&a); printf("Please enter a value b:"); scanf("%d",&b); calc(a,b,&p,&q); printf("Sum = %d, Sub = %d",p,q); return 0; } calc(int x, int y, int *add, int *sub) { *add = x+y; *sub = x-y; return (0); }