More About Functions


Objectives
In this lesson, you will learn to:
 Use in-built functions in a program
 Handle command line parameter in a program
 Handle pointer to functions




©NIIT                                   OOPS/Lesson 6/Slide 1 of 16
More About Functions


In-Built Functions
 C++ provides predefined functions to manipulate
  string and numeric data
 Some of the in-built string functions are:
     strcpy- copies the contents of the second string to
      the first string. The syntax is as follows:
        char * strcpy ( char * dest, const char * src );
        For example, strcpy(cStr1,“Judas Priest”);
     strcat- is used to join (concatenate) the second
      string with the first string. The syntax is as follows:
        char * strcat ( char * dest, const char * src );
        For example, strcat(cStr1, “ABC”);
©NIIT                                   OOPS/Lesson 6/Slide 2 of 16
More About Functions


In-Built Functions (Contd..)
     strncpy- Copies the specified number of characters
      of the second string to the first string. The syntax is
      as follows:
        char * strncpy ( char * dest, const char * src, sizet_t
        num );
        For example, strcpy(cStr1,“Judas
        Priest”,5);
     strlen- Returns the length for the string. The syntax
      is as follows:
        size_t strlen ( const char * str1 );
        For example, iNum=strlen(cStr1);

©NIIT                                      OOPS/Lesson 6/Slide 3 of 16
More About Functions


In-Built Functions (Contd..)
     strstr- Scans string1 for the first occurrence of
      string2. The syntax is as follows:
        Char * strstr (char * string1, const char * string2 );
        For example, char str1[]=”Hello World”;
        char str2[]=”or”;
        char * sFound=strstr(str1,str2);
        coutsFound-str1+1;
     strcmp- compares two strings (supplied as its
      parameters) and returns an integer value.


©NIIT                                      OOPS/Lesson 6/Slide 4 of 16
More About Functions


In-Built Functions (Contd..)
        The syntax is as follows:
        Char * strstr (char * string1, char * string2 );
        For example,
        iNum=strcmp(“madonna”,”maradona”);
     strchr- Searches for a character in a string. The
      syntax is as follows:
        char * strchr ( const char * string, int c );
        For example, ptr = strchr( s, '0' );




©NIIT                                      OOPS/Lesson 6/Slide 5 of 16
More About Functions


In-Built Functions (Contd..)
 Some of the in-built numeric functions are:
     abs- Returns absolute value of the integer
      parameter. The syntax is as follows:
        int abs ( int n );
        For example, abs(-2)
     ceil- Rounds up the parameter to the nearest
      integer value. The syntax is as follows:
        int ceil ( int n );
        For example, ceil(1.4)


©NIIT                                OOPS/Lesson 6/Slide 6 of 16
More About Functions


In-Built Functions (Contd..)
     floor- Returns the largest integer that is less than
      or equal to the integer passed as a parameter. The
      syntax is as follows:
        double floor ( double x );
        For example, floor(1.8)
     sqrt- Returns the square root of parameter. The
      syntax is as follows:
        double sqrt ( double x );
        For example, param = 1024.0;
        result = sqrt (param);

©NIIT                                  OOPS/Lesson 6/Slide 7 of 16
More About Functions


In-Built Functions (Contd..)
     atoi- Returns the integer value stored as a string.
      The syntax is as follows:
        int atoi (const char * string)
        For example, char str[]=”345A”;
        result=atoi(str);




©NIIT                                     OOPS/Lesson 6/Slide 8 of 16
More About Functions


Command Line Parameters
 Command line parameters are the values are passed
  from the command prompt to the program during
  program execution.
 The two parameters of the C++ main() function that
  interpret the values are:
     argc
     argv




©NIIT                             OOPS/Lesson 6/Slide 9 of 16
More About Functions


Declaration of Functions
 The syntax to declare a function is as follows:
  void main(int argc, char *argv[])
    {
    }




©NIIT                                 OOPS/Lesson 6/Slide 10 of 16
More About Functions


  Problem Statement 6.D.1
  Write a program, which accepts five numbers from
  command line and display it.




©NIIT                             OOPS/Lesson 6/Slide 11 of 16
More About Functions


Just a Minute…
 int argc gives ________________
 char *argv[] gives ________________.




©NIIT                            OOPS/Lesson 6/Slide 12 of 16
More About Functions


  Problem Statement 6.P.1
  Modify the existing empsalary module of Diaz
  Telecommunication Inc. to increment the salary of
  their employees. Increment amount need to be
  passed from command line.




©NIIT                              OOPS/Lesson 6/Slide 13 of 16
More About Functions


Pointer to Function
 Function pointers are pointers, i.e. variables, which
  point to the address of a function.
 The addresses that the function pointers contain
  reside in the code segment itself
 The syntax to declare to declare a function pointer is
  as follows:
  [return_type] (*pointer_name)[(list_of_parameters)]
 A pointer to a function can be initialized with the name
  of a function, within the declaration of the pointer. For
  example,
  long max(const long* array,int cnt); pFun=max;

©NIIT                                 OOPS/Lesson 6/Slide 14 of 16
More About Functions

Summary
In this lesson, you learned that:
 C++ provides several in-built functions.
 Some of the commonly used string functions are:
    strcpy
    strcat
    strncpy
    strlen
    strstr
    strcmp
    strchr
©NIIT                                OOPS/Lesson 6/Slide 15 of 16
More About Functions

Summary (Contd..)
 Some of the commonly used numeric functions are:
    floor
    ceil
    abs
    Sqrt
 Command line parameters add functionality to your
  programs by allowing you to pass parameters with
  your C++ program.
 Function pointers are pointers, i.e. variables, which
  point to the address of a function.


©NIIT                                OOPS/Lesson 6/Slide 16 of 16

Aae oop xp_06

  • 1.
    More About Functions Objectives Inthis lesson, you will learn to: Use in-built functions in a program Handle command line parameter in a program Handle pointer to functions ©NIIT OOPS/Lesson 6/Slide 1 of 16
  • 2.
    More About Functions In-BuiltFunctions C++ provides predefined functions to manipulate string and numeric data Some of the in-built string functions are: strcpy- copies the contents of the second string to the first string. The syntax is as follows: char * strcpy ( char * dest, const char * src ); For example, strcpy(cStr1,“Judas Priest”); strcat- is used to join (concatenate) the second string with the first string. The syntax is as follows: char * strcat ( char * dest, const char * src ); For example, strcat(cStr1, “ABC”); ©NIIT OOPS/Lesson 6/Slide 2 of 16
  • 3.
    More About Functions In-BuiltFunctions (Contd..) strncpy- Copies the specified number of characters of the second string to the first string. The syntax is as follows: char * strncpy ( char * dest, const char * src, sizet_t num ); For example, strcpy(cStr1,“Judas Priest”,5); strlen- Returns the length for the string. The syntax is as follows: size_t strlen ( const char * str1 ); For example, iNum=strlen(cStr1); ©NIIT OOPS/Lesson 6/Slide 3 of 16
  • 4.
    More About Functions In-BuiltFunctions (Contd..) strstr- Scans string1 for the first occurrence of string2. The syntax is as follows: Char * strstr (char * string1, const char * string2 ); For example, char str1[]=”Hello World”; char str2[]=”or”; char * sFound=strstr(str1,str2); coutsFound-str1+1; strcmp- compares two strings (supplied as its parameters) and returns an integer value. ©NIIT OOPS/Lesson 6/Slide 4 of 16
  • 5.
    More About Functions In-BuiltFunctions (Contd..) The syntax is as follows: Char * strstr (char * string1, char * string2 ); For example, iNum=strcmp(“madonna”,”maradona”); strchr- Searches for a character in a string. The syntax is as follows: char * strchr ( const char * string, int c ); For example, ptr = strchr( s, '0' ); ©NIIT OOPS/Lesson 6/Slide 5 of 16
  • 6.
    More About Functions In-BuiltFunctions (Contd..) Some of the in-built numeric functions are: abs- Returns absolute value of the integer parameter. The syntax is as follows: int abs ( int n ); For example, abs(-2) ceil- Rounds up the parameter to the nearest integer value. The syntax is as follows: int ceil ( int n ); For example, ceil(1.4) ©NIIT OOPS/Lesson 6/Slide 6 of 16
  • 7.
    More About Functions In-BuiltFunctions (Contd..) floor- Returns the largest integer that is less than or equal to the integer passed as a parameter. The syntax is as follows: double floor ( double x ); For example, floor(1.8) sqrt- Returns the square root of parameter. The syntax is as follows: double sqrt ( double x ); For example, param = 1024.0; result = sqrt (param); ©NIIT OOPS/Lesson 6/Slide 7 of 16
  • 8.
    More About Functions In-BuiltFunctions (Contd..) atoi- Returns the integer value stored as a string. The syntax is as follows: int atoi (const char * string) For example, char str[]=”345A”; result=atoi(str); ©NIIT OOPS/Lesson 6/Slide 8 of 16
  • 9.
    More About Functions CommandLine Parameters Command line parameters are the values are passed from the command prompt to the program during program execution. The two parameters of the C++ main() function that interpret the values are: argc argv ©NIIT OOPS/Lesson 6/Slide 9 of 16
  • 10.
    More About Functions Declarationof Functions The syntax to declare a function is as follows: void main(int argc, char *argv[]) { } ©NIIT OOPS/Lesson 6/Slide 10 of 16
  • 11.
    More About Functions Problem Statement 6.D.1 Write a program, which accepts five numbers from command line and display it. ©NIIT OOPS/Lesson 6/Slide 11 of 16
  • 12.
    More About Functions Justa Minute… int argc gives ________________ char *argv[] gives ________________. ©NIIT OOPS/Lesson 6/Slide 12 of 16
  • 13.
    More About Functions Problem Statement 6.P.1 Modify the existing empsalary module of Diaz Telecommunication Inc. to increment the salary of their employees. Increment amount need to be passed from command line. ©NIIT OOPS/Lesson 6/Slide 13 of 16
  • 14.
    More About Functions Pointerto Function Function pointers are pointers, i.e. variables, which point to the address of a function. The addresses that the function pointers contain reside in the code segment itself The syntax to declare to declare a function pointer is as follows: [return_type] (*pointer_name)[(list_of_parameters)] A pointer to a function can be initialized with the name of a function, within the declaration of the pointer. For example, long max(const long* array,int cnt); pFun=max; ©NIIT OOPS/Lesson 6/Slide 14 of 16
  • 15.
    More About Functions Summary Inthis lesson, you learned that: C++ provides several in-built functions. Some of the commonly used string functions are: strcpy strcat strncpy strlen strstr strcmp strchr ©NIIT OOPS/Lesson 6/Slide 15 of 16
  • 16.
    More About Functions Summary(Contd..) Some of the commonly used numeric functions are: floor ceil abs Sqrt Command line parameters add functionality to your programs by allowing you to pass parameters with your C++ program. Function pointers are pointers, i.e. variables, which point to the address of a function. ©NIIT OOPS/Lesson 6/Slide 16 of 16