CHAPTER 6

M
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”) ; 
}
CHAPTER 6
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); 
}
1 von 19

Recomendados

Functions in c von
Functions in cFunctions in c
Functions in cInnovative
1.1K views18 Folien
Functions in C von
Functions in CFunctions in C
Functions in CPrincy Nelson
444 views21 Folien
C programming function von
C  programming functionC  programming function
C programming functionargusacademy
1.2K views17 Folien
lets play with "c"..!!! :):) von
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)Rupendra Choudhary
594 views23 Folien
Function in c program von
Function in c programFunction in c program
Function in c programCGC Technical campus,Mohali
560 views24 Folien
parameter passing in c# von
parameter passing in c#parameter passing in c#
parameter passing in c#khush_boo31
1.9K views22 Folien

Más contenido relacionado

Was ist angesagt?

Function in c von
Function in cFunction in c
Function in cCGC Technical campus,Mohali
655 views22 Folien
Types of function call von
Types of function callTypes of function call
Types of function callArijitDhali
373 views14 Folien
Call by value von
Call by valueCall by value
Call by valueDharani G
8.6K views9 Folien
Programming Fundamentals Decisions von
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions imtiazalijoono
45 views32 Folien
function in c von
function in cfunction in c
function in csubam3
132 views31 Folien
Function & Recursion in C von
Function & Recursion in CFunction & Recursion in C
Function & Recursion in CAditya Nihal Kumar Singh
251 views15 Folien

Was ist angesagt?(20)

Types of function call von ArijitDhali
Types of function callTypes of function call
Types of function call
ArijitDhali373 views
Call by value von Dharani G
Call by valueCall by value
Call by value
Dharani G8.6K views
Programming Fundamentals Decisions von imtiazalijoono
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
imtiazalijoono45 views
function in c von subam3
function in cfunction in c
function in c
subam3132 views
Function in c program von umesh patil
Function in c programFunction in c program
Function in c program
umesh patil1.3K views
Functions and pointers_unit_4 von MKalpanaDevi
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
MKalpanaDevi153 views
Programming Fundamentals Arrays and Strings von imtiazalijoono
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
imtiazalijoono179 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
C Prog - Functions von vinay arora
C Prog - FunctionsC Prog - Functions
C Prog - Functions
vinay arora1.2K views
Decision making and branching von Saranya saran
Decision making and branchingDecision making and branching
Decision making and branching
Saranya saran88 views

Similar a CHAPTER 6

Function in C program von
Function in C programFunction in C program
Function in C programNurul Zakiah Zamri Tan
70.4K views23 Folien
Functions in C.pptx von
Functions in C.pptxFunctions in C.pptx
Functions in C.pptxKarthikSivagnanam2
4 views19 Folien
Functions and pointers_unit_4 von
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4Saranya saran
82 views112 Folien
Function von
FunctionFunction
Functionmshoaib15
18 views15 Folien
Functions von
FunctionsFunctions
FunctionsPralhadKhanal1
77 views36 Folien
Array Cont von
Array ContArray Cont
Array ContAshutosh Srivasatava
456 views86 Folien

Similar a CHAPTER 6(20)

Functions and pointers_unit_4 von Saranya saran
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
Saranya saran82 views
Dti2143 chapter 5 von alish sha
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
alish sha736 views
Presentation on Function in C Programming von Shuvongkor Barman
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman14.3K views
C Programming Language Part 7 von Rumman Ansari
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
Rumman Ansari465 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
vekariyakashyap12 views
Unit 5 Foc von JAYA
Unit 5 FocUnit 5 Foc
Unit 5 Foc
JAYA2.1K views

Más de mohd_mizan

Mind map chapter 4 von
Mind map chapter 4Mind map chapter 4
Mind map chapter 4mohd_mizan
1.6K views1 Folie
Dbs 1012 synopsis von
Dbs 1012 synopsisDbs 1012 synopsis
Dbs 1012 synopsismohd_mizan
380 views1 Folie
Chapter 4 work, energy and power von
Chapter 4 work, energy and powerChapter 4 work, energy and power
Chapter 4 work, energy and powermohd_mizan
11.8K views32 Folien
CHAPTER 3 von
CHAPTER 3CHAPTER 3
CHAPTER 3mohd_mizan
480 views19 Folien
CHAPTER 4 von
CHAPTER 4CHAPTER 4
CHAPTER 4mohd_mizan
664 views21 Folien
CHAPTER 5 von
CHAPTER 5CHAPTER 5
CHAPTER 5mohd_mizan
411 views33 Folien

Más de mohd_mizan(8)

Mind map chapter 4 von mohd_mizan
Mind map chapter 4Mind map chapter 4
Mind map chapter 4
mohd_mizan1.6K views
Dbs 1012 synopsis von mohd_mizan
Dbs 1012 synopsisDbs 1012 synopsis
Dbs 1012 synopsis
mohd_mizan380 views
Chapter 4 work, energy and power von mohd_mizan
Chapter 4 work, energy and powerChapter 4 work, energy and power
Chapter 4 work, energy and power
mohd_mizan11.8K views

Último

Women from Hackney’s History: Stoke Newington by Sue Doe von
Women from Hackney’s History: Stoke Newington by Sue DoeWomen from Hackney’s History: Stoke Newington by Sue Doe
Women from Hackney’s History: Stoke Newington by Sue DoeHistory of Stoke Newington
133 views21 Folien
Class 10 English notes 23-24.pptx von
Class 10 English notes 23-24.pptxClass 10 English notes 23-24.pptx
Class 10 English notes 23-24.pptxTARIQ KHAN
95 views53 Folien
Student Voice von
Student Voice Student Voice
Student Voice Pooky Knightsmith
148 views33 Folien
The Open Access Community Framework (OACF) 2023 (1).pptx von
The Open Access Community Framework (OACF) 2023 (1).pptxThe Open Access Community Framework (OACF) 2023 (1).pptx
The Open Access Community Framework (OACF) 2023 (1).pptxJisc
77 views7 Folien
JiscOAWeek_LAIR_slides_October2023.pptx von
JiscOAWeek_LAIR_slides_October2023.pptxJiscOAWeek_LAIR_slides_October2023.pptx
JiscOAWeek_LAIR_slides_October2023.pptxJisc
72 views8 Folien

Último(20)

Class 10 English notes 23-24.pptx von TARIQ KHAN
Class 10 English notes 23-24.pptxClass 10 English notes 23-24.pptx
Class 10 English notes 23-24.pptx
TARIQ KHAN95 views
The Open Access Community Framework (OACF) 2023 (1).pptx von Jisc
The Open Access Community Framework (OACF) 2023 (1).pptxThe Open Access Community Framework (OACF) 2023 (1).pptx
The Open Access Community Framework (OACF) 2023 (1).pptx
Jisc77 views
JiscOAWeek_LAIR_slides_October2023.pptx von Jisc
JiscOAWeek_LAIR_slides_October2023.pptxJiscOAWeek_LAIR_slides_October2023.pptx
JiscOAWeek_LAIR_slides_October2023.pptx
Jisc72 views
American Psychological Association 7th Edition.pptx von SamiullahAfridi4
American Psychological Association  7th Edition.pptxAmerican Psychological Association  7th Edition.pptx
American Psychological Association 7th Edition.pptx
SamiullahAfridi474 views
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively von PECB
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks EffectivelyISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively
PECB 457 views
7 NOVEL DRUG DELIVERY SYSTEM.pptx von Sachin Nitave
7 NOVEL DRUG DELIVERY SYSTEM.pptx7 NOVEL DRUG DELIVERY SYSTEM.pptx
7 NOVEL DRUG DELIVERY SYSTEM.pptx
Sachin Nitave56 views
Narration lesson plan.docx von TARIQ KHAN
Narration lesson plan.docxNarration lesson plan.docx
Narration lesson plan.docx
TARIQ KHAN99 views
AI Tools for Business and Startups von Svetlin Nakov
AI Tools for Business and StartupsAI Tools for Business and Startups
AI Tools for Business and Startups
Svetlin Nakov89 views
Are we onboard yet University of Sussex.pptx von Jisc
Are we onboard yet University of Sussex.pptxAre we onboard yet University of Sussex.pptx
Are we onboard yet University of Sussex.pptx
Jisc71 views
Community-led Open Access Publishing webinar.pptx von Jisc
Community-led Open Access Publishing webinar.pptxCommunity-led Open Access Publishing webinar.pptx
Community-led Open Access Publishing webinar.pptx
Jisc69 views

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”) ; }
  • 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); }