SlideShare uma empresa Scribd logo
1 de 13
Prepared by
Mohammed Sikander
Technical Lead
Cranes Software International Limited
 What is a function?
 Write the Syntax of Function?
 What are the benefits of using a function?
 Is main a user-defined or library function?
 Explain the difference between call by value
and call by reference(address)?
mohammed.sikander@cranessoftware.com 2
 Name some Library (built-in) functions used
by you till now?
mohammed.sikander@cranessoftware.com 3
 SYNOPSIS
#include <math.h>
double sqrt(double x);
mohammed.sikander@cranessoftware.com 4
#include <stdio.h>
#include <math.h>
int main( )
{
float res = sqrt(16);
printf(“ %f “ , res);
}
SYNOPSIS
#include <math.h>
double pow(double x, double y);
mohammed.sikander@cranessoftware.com 5
int main( )
{
int x , y;
printf(“Enter two numbers : “);
scanf(“ %d %d” , &x, &y);
int res = pow ( x , y);
printf(“ %d “ , res);
}
#include <ctype.h>
int isupper(int c);
It checks whether the given character is uppercase or not. Returns o is not
non-zero otherwise
mohammed.sikander@cranessoftware.com 6
#include <ctype.h>
int main( )
{
char c;
printf(“Enter a character : “);
scanf(“ %c”,&c);
int res = isupper(c) ;
if(res == 0)
printf(“Not a Upper Case Character “);
else
printf(“UPPERCASECharacter “);
}
 Now we have learned how to call a function.
 Now lets write our (user-defined) function.
mohammed.sikander@cranessoftware.com 7
Returntype functionName(parameters)
{
body
}
Every parameter will have type and name
The parameter list can be empty
int main( )
{
------
}
mohammed.sikander@cranessoftware.com 8
 Write a factorial function which accepts a number and returns the
factorial of it.
 Eg: Input Output
5 120
3 6
mohammed.sikander@cranessoftware.com 9
int factorial( int num)
{
int res = 1;
for(i = 2 ; i <= num ; i++)
res = res * i;
return res;
}
int main( )
{
int res = factorial(5);
printf(“ %d n“ , res);
res = factorial(3);
printf(“ %d n“ , res);
}
int main( )
{
int n , r;
printf(“Enter the value of n and r : “);
scanf(“ %d %d”,&n,&r);
int nfact = factorial(n);
int rfact = factorial(r);
int nrfact = factorial(n - r);
int ncr = nfact / (rfact * nrfact);
int npr = nfact / nrfact;
printf(“ %d C %d = %d n“ , n , r , ncr);
printf(“ %d P %d = %d n“ , n , r , npr);
}
mohammed.sikander@cranessoftware.com 10
int gcd(int a,int b)
{
int rem;
while(b != 0)
{
rem = a % b;
a = b;
b = rem;
}
return a;
}
mohammed.sikander@cranessoftware.com 11
int main()
{
int a , b;
printf(“Enter two numbers : “);
scanf(“ %d %d”,&a,&b);
int res = gcd(a , b);
printf(“GCD(%d , %d) = %d”,a , b , res);
int lcm = a * b / res;
printf(“LCM(%d ,%d) = %d”,a , b , lcm);
}
#include <stdbool.h>
bool isprime(int num)
{
int count = 0;
for(int i = 2 ; i < num - 1 ; i++)
{
int rem = num % i;
if(rem == 0)
count++;
}
return count == 0?true:false;
}
mohammed.sikander@cranessoftware.com 12
int main( )
{
int num ;
bool result;
printf("Enter a number : ");
scanf(" %d" , &num);
if(res == true)
printf("Prime ");
else
printf("Not Prime ");
}
mohammed.sikander@cranessoftware.com 13
int main( )
{
int n1 , n2;
bool result;
printf("Enter the start and end range : ");
scanf(" %d %d" , &n1 , n2);
for(int i = n1 ; i <= n2 ; i++)
{
bool res = isprime( i );
if(res == true)
printf(“ %d “ , i);
}
}

Mais conteúdo relacionado

Mais procurados

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
hassaanciit
 

Mais procurados (20)

Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphism
 
Static and const members
Static and const membersStatic and const members
Static and const members
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Implementation of c string functions
Implementation of c string functionsImplementation of c string functions
Implementation of c string functions
 
C programs
C programsC programs
C programs
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
 
Double linked list
Double linked listDouble linked list
Double linked list
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
C++ file
C++ fileC++ file
C++ file
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
 
C++ programs
C++ programsC++ programs
C++ programs
 
Introduction to c part 2
Introduction to c   part  2Introduction to c   part  2
Introduction to c part 2
 
week-18x
week-18xweek-18x
week-18x
 
Double linked list
Double linked listDouble linked list
Double linked list
 
String Manipulation Function and Header File Functions
String Manipulation Function and Header File FunctionsString Manipulation Function and Header File Functions
String Manipulation Function and Header File Functions
 
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
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
4. chapter iii
4. chapter iii4. chapter iii
4. chapter iii
 
3. chapter ii
3. chapter ii3. chapter ii
3. chapter ii
 

Destaque

Data structures in C Singlly Linked List Concept
Data structures in C  Singlly Linked List ConceptData structures in C  Singlly Linked List Concept
Data structures in C Singlly Linked List Concept
ayman diab
 

Destaque (13)

3 palestra sergio 2012
3 palestra sergio 20123 palestra sergio 2012
3 palestra sergio 2012
 
Redes sociales
Redes socialesRedes sociales
Redes sociales
 
New definition for being fit - Just Dance
New definition for being fit - Just DanceNew definition for being fit - Just Dance
New definition for being fit - Just Dance
 
Colleen Kolakowski Resume 12-27-2016
Colleen Kolakowski Resume 12-27-2016Colleen Kolakowski Resume 12-27-2016
Colleen Kolakowski Resume 12-27-2016
 
Pointer level 2
Pointer   level 2Pointer   level 2
Pointer level 2
 
Standar asuhan keperawatan klien dengan halusinasi
Standar asuhan keperawatan klien dengan halusinasiStandar asuhan keperawatan klien dengan halusinasi
Standar asuhan keperawatan klien dengan halusinasi
 
Resti fiks fdf
Resti fiks fdfResti fiks fdf
Resti fiks fdf
 
Kuukausittainen matkailutilasto Etelä-Savo syyskuu 2015
Kuukausittainen matkailutilasto Etelä-Savo syyskuu 2015Kuukausittainen matkailutilasto Etelä-Savo syyskuu 2015
Kuukausittainen matkailutilasto Etelä-Savo syyskuu 2015
 
Data structures in C Singlly Linked List Concept
Data structures in C  Singlly Linked List ConceptData structures in C  Singlly Linked List Concept
Data structures in C Singlly Linked List Concept
 
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN IBU HAMIL PADA NY “S” DENGAN ...
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN IBU HAMIL PADA NY “S” DENGAN ...MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN IBU HAMIL PADA NY “S” DENGAN ...
MANAJEMEN DAN PENDOKUMENTASIAN ASUHAN KEBIDANAN IBU HAMIL PADA NY “S” DENGAN ...
 
Intan. iii.a
Intan. iii.aIntan. iii.a
Intan. iii.a
 
Successful Tourism Strategic Plans for Destinations - Dr. Harsh Varma
Successful Tourism Strategic Plans for Destinations - Dr. Harsh VarmaSuccessful Tourism Strategic Plans for Destinations - Dr. Harsh Varma
Successful Tourism Strategic Plans for Destinations - Dr. Harsh Varma
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examples
 

Semelhante a Function basics

Semelhante a Function basics (20)

Pointer basics
Pointer basicsPointer basics
Pointer basics
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
7 functions
7  functions7  functions
7 functions
 
C Programming
C ProgrammingC Programming
C Programming
 
C Programming Example
C Programming Example C Programming Example
C Programming Example
 
Functions
FunctionsFunctions
Functions
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using c
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
C lab programs
C lab programsC lab programs
C lab programs
 
C lab programs
C lab programsC lab programs
C lab programs
 
C programming function
C  programming functionC  programming function
C programming function
 
SaraPIC
SaraPICSaraPIC
SaraPIC
 
Functions
FunctionsFunctions
Functions
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
 
C- Programming Assignment 4 solution
C- Programming Assignment 4 solutionC- Programming Assignment 4 solution
C- Programming Assignment 4 solution
 
C lab
C labC lab
C lab
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Functions
FunctionsFunctions
Functions
 
CHAPTER 6
CHAPTER 6CHAPTER 6
CHAPTER 6
 

Último

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Último (20)

Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 

Function basics

  • 1. Prepared by Mohammed Sikander Technical Lead Cranes Software International Limited
  • 2.  What is a function?  Write the Syntax of Function?  What are the benefits of using a function?  Is main a user-defined or library function?  Explain the difference between call by value and call by reference(address)? mohammed.sikander@cranessoftware.com 2
  • 3.  Name some Library (built-in) functions used by you till now? mohammed.sikander@cranessoftware.com 3
  • 4.  SYNOPSIS #include <math.h> double sqrt(double x); mohammed.sikander@cranessoftware.com 4 #include <stdio.h> #include <math.h> int main( ) { float res = sqrt(16); printf(“ %f “ , res); }
  • 5. SYNOPSIS #include <math.h> double pow(double x, double y); mohammed.sikander@cranessoftware.com 5 int main( ) { int x , y; printf(“Enter two numbers : “); scanf(“ %d %d” , &x, &y); int res = pow ( x , y); printf(“ %d “ , res); }
  • 6. #include <ctype.h> int isupper(int c); It checks whether the given character is uppercase or not. Returns o is not non-zero otherwise mohammed.sikander@cranessoftware.com 6 #include <ctype.h> int main( ) { char c; printf(“Enter a character : “); scanf(“ %c”,&c); int res = isupper(c) ; if(res == 0) printf(“Not a Upper Case Character “); else printf(“UPPERCASECharacter “); }
  • 7.  Now we have learned how to call a function.  Now lets write our (user-defined) function. mohammed.sikander@cranessoftware.com 7
  • 8. Returntype functionName(parameters) { body } Every parameter will have type and name The parameter list can be empty int main( ) { ------ } mohammed.sikander@cranessoftware.com 8
  • 9.  Write a factorial function which accepts a number and returns the factorial of it.  Eg: Input Output 5 120 3 6 mohammed.sikander@cranessoftware.com 9 int factorial( int num) { int res = 1; for(i = 2 ; i <= num ; i++) res = res * i; return res; } int main( ) { int res = factorial(5); printf(“ %d n“ , res); res = factorial(3); printf(“ %d n“ , res); }
  • 10. int main( ) { int n , r; printf(“Enter the value of n and r : “); scanf(“ %d %d”,&n,&r); int nfact = factorial(n); int rfact = factorial(r); int nrfact = factorial(n - r); int ncr = nfact / (rfact * nrfact); int npr = nfact / nrfact; printf(“ %d C %d = %d n“ , n , r , ncr); printf(“ %d P %d = %d n“ , n , r , npr); } mohammed.sikander@cranessoftware.com 10
  • 11. int gcd(int a,int b) { int rem; while(b != 0) { rem = a % b; a = b; b = rem; } return a; } mohammed.sikander@cranessoftware.com 11 int main() { int a , b; printf(“Enter two numbers : “); scanf(“ %d %d”,&a,&b); int res = gcd(a , b); printf(“GCD(%d , %d) = %d”,a , b , res); int lcm = a * b / res; printf(“LCM(%d ,%d) = %d”,a , b , lcm); }
  • 12. #include <stdbool.h> bool isprime(int num) { int count = 0; for(int i = 2 ; i < num - 1 ; i++) { int rem = num % i; if(rem == 0) count++; } return count == 0?true:false; } mohammed.sikander@cranessoftware.com 12 int main( ) { int num ; bool result; printf("Enter a number : "); scanf(" %d" , &num); if(res == true) printf("Prime "); else printf("Not Prime "); }
  • 13. mohammed.sikander@cranessoftware.com 13 int main( ) { int n1 , n2; bool result; printf("Enter the start and end range : "); scanf(" %d %d" , &n1 , n2); for(int i = n1 ; i <= n2 ; i++) { bool res = isprime( i ); if(res == true) printf(“ %d “ , i); } }