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

More Related Content

PPSX
Functions in c
PPTX
Functions in C
PPSX
C programming function
PPT
lets play with "c"..!!! :):)
PPTX
Function in c program
PPTX
parameter passing in c#
PPTX
C function
PPTX
Function in c
Functions in c
Functions in C
C programming function
lets play with "c"..!!! :):)
Function in c program
parameter passing in c#
C function
Function in c

What's hot (20)

PPTX
PPTX
Types of function call
PPTX
Call by value
PDF
Programming Fundamentals Decisions
PPTX
function in c
PPTX
Function in c program
PPT
Functions and pointers_unit_4
PDF
Programming Fundamentals Arrays and Strings
PPTX
Functionincprogram
DOC
Functions struct&union
PDF
Functions
PDF
Function lecture
PPT
Fucntions & Pointers in C
PDF
Introduction to Computer and Programing - Lecture 04
PDF
Functions
PDF
C Prog - Functions
ODP
Function
PPTX
Decision making and branching
DOC
Unit 4 (1)
PPTX
Function in c language(defination and declaration)
Types of function call
Call by value
Programming Fundamentals Decisions
function in c
Function in c program
Functions and pointers_unit_4
Programming Fundamentals Arrays and Strings
Functionincprogram
Functions struct&union
Functions
Function lecture
Fucntions & Pointers in C
Introduction to Computer and Programing - Lecture 04
Functions
C Prog - Functions
Function
Decision making and branching
Unit 4 (1)
Function in c language(defination and declaration)
Ad

Similar to CHAPTER 6 (20)

PPTX
Fundamentals of functions in C program.pptx
PPTX
luckfuckfunctioneekefkfejewnfiwnfnenf.pptx
PPTX
Function in C program
PPTX
Functions in C.pptx
PPT
Functions and pointers_unit_4
PPTX
Function
PPTX
Functions
DOCX
PPTX
unit_2.pptx
PPTX
Dti2143 chapter 5
PPT
eee2-day4-structures engineering college
PPT
Functions in c
PPTX
Detailed concept of function in c programming
PPT
c-Functions power point presentation on c functions
PDF
cp Module4(1)
DOC
Functions
PPTX
Functions in c
PPTX
unit_2 (1).pptx
PPTX
Presentation on Function in C Programming
PPTX
C Programming Language Part 7
Fundamentals of functions in C program.pptx
luckfuckfunctioneekefkfejewnfiwnfnenf.pptx
Function in C program
Functions in C.pptx
Functions and pointers_unit_4
Function
Functions
unit_2.pptx
Dti2143 chapter 5
eee2-day4-structures engineering college
Functions in c
Detailed concept of function in c programming
c-Functions power point presentation on c functions
cp Module4(1)
Functions
Functions in c
unit_2 (1).pptx
Presentation on Function in C Programming
C Programming Language Part 7
Ad

More from mohd_mizan (8)

PDF
Mind map chapter 4
PDF
Dbs 1012 synopsis
PDF
Chapter 4 work, energy and power
PPTX
CHAPTER 3
PPTX
CHAPTER 4
PPTX
CHAPTER 5
PPTX
CHAPTER 2
PPT
CHAPTER 1
Mind map chapter 4
Dbs 1012 synopsis
Chapter 4 work, energy and power
CHAPTER 3
CHAPTER 4
CHAPTER 5
CHAPTER 2
CHAPTER 1

Recently uploaded (20)

PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
RMMM.pdf make it easy to upload and study
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Cell Structure & Organelles in detailed.
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Week 4 Term 3 Study Techniques revisited.pptx
O7-L3 Supply Chain Operations - ICLT Program
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Insiders guide to clinical Medicine.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
RMMM.pdf make it easy to upload and study
PPH.pptx obstetrics and gynecology in nursing
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
human mycosis Human fungal infections are called human mycosis..pptx
Pharma ospi slides which help in ospi learning
Cell Structure & Organelles in detailed.
Microbial diseases, their pathogenesis and prophylaxis
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf

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