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

Function basics

  • 1.
    Prepared by Mohammed Sikander TechnicalLead Cranes Software International Limited
  • 2.
     What isa 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 someLibrary (built-in) functions used by you till now? mohammed.sikander@cranessoftware.com 3
  • 4.
     SYNOPSIS #include <math.h> doublesqrt(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(doublex, 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(intc); 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 wehave learned how to call a function.  Now lets write our (user-defined) function. mohammed.sikander@cranessoftware.com 7
  • 8.
    Returntype functionName(parameters) { body } Every parameterwill have type and name The parameter list can be empty int main( ) { ------ } mohammed.sikander@cranessoftware.com 8
  • 9.
     Write afactorial 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( ) { intn , 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,intb) { 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(intnum) { 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); } }