SlideShare a Scribd company logo
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

More Related Content

PPT
PPTX
Optimization of basic blocks
DOCX
Type header file in c++ and its function
PPTX
String .h
PPTX
C programming - String
PPTX
Managing I/O & String function in C
PPTX
Code optimization
Optimization of basic blocks
Type header file in c++ and its function
String .h
C programming - String
Managing I/O & String function in C
Code optimization

What's hot (20)

PPTX
Register allocation and assignment
PPTX
expression in cpp
PDF
A nice 64-bit error in C
PPT
Code generator
PPTX
Dynamic memory allocation
PPTX
Implementation Of String Functions In C
PPTX
Pointers
PPTX
Function overloading
PDF
03 function overloading
PPTX
Lecture 2. mte 407
PPTX
C++ theory
PDF
Chapter 10 Library Function
PDF
L6
PDF
Intermediate code generation in Compiler Design
PPTX
Unit iv(simple code generator)
PPTX
Pointers in c++
PDF
A peek on numerical programming in perl and python e christopher dyken 2005
Register allocation and assignment
expression in cpp
A nice 64-bit error in C
Code generator
Dynamic memory allocation
Implementation Of String Functions In C
Pointers
Function overloading
03 function overloading
Lecture 2. mte 407
C++ theory
Chapter 10 Library Function
L6
Intermediate code generation in Compiler Design
Unit iv(simple code generator)
Pointers in c++
A peek on numerical programming in perl and python e christopher dyken 2005
Ad

Viewers also liked (20)

PPS
Vb.net session 09
PPS
15 ooad uml-20
PPS
Dacj 2-2 c
PPS
Oops recap
PPS
11 ds and algorithm session_16
PPS
09 iec t1_s1_oo_ps_session_13
PDF
OOP Java
PPS
Rdbms xp 01
PPS
Jdbc session01
PDF
Shizuoka pref public_library(20101020)
PPS
07 asp.net session10
PDF
Presentación junio
PPS
03 asp.net session04
PPTX
Mantenimiento de computo
PPTX
simple present tense
PPTX
CacharreAndo
PPTX
Presentación1
PPTX
Presentación del sistema genesis y portal institucional j a
PPTX
Idocaedro aplicacion
Vb.net session 09
15 ooad uml-20
Dacj 2-2 c
Oops recap
11 ds and algorithm session_16
09 iec t1_s1_oo_ps_session_13
OOP Java
Rdbms xp 01
Jdbc session01
Shizuoka pref public_library(20101020)
07 asp.net session10
Presentación junio
03 asp.net session04
Mantenimiento de computo
simple present tense
CacharreAndo
Presentación1
Presentación del sistema genesis y portal institucional j a
Idocaedro aplicacion
Ad

Similar to Aae oop xp_06 (20)

PPTX
6. Functions in C ++ programming object oriented programming
PPTX
Header file.pptx
PPT
Strings
PDF
PDF
c++ referesher 1.pdf
PDF
Cpprm
PPTX
Object oriented programming system with C++
PPTX
C++ Functions.pptx
PPTX
Fundamental of programming Fundamental of programming
PPTX
Chapter 1 (2) array and structure r.pptx
PPT
PPTX
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
PPTX
Learning c++
PPTX
CPP06 - Functions
PPTX
Functions in C++
PPTX
C++ PROGRAMMING BASICS
PPT
Basics of c++
PPTX
Functions in C++
PPTX
Functions in C++ programming language.pptx
PDF
Chapter 3 - Characters and Strings - Student.pdf
6. Functions in C ++ programming object oriented programming
Header file.pptx
Strings
c++ referesher 1.pdf
Cpprm
Object oriented programming system with C++
C++ Functions.pptx
Fundamental of programming Fundamental of programming
Chapter 1 (2) array and structure r.pptx
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
Learning c++
CPP06 - Functions
Functions in C++
C++ PROGRAMMING BASICS
Basics of c++
Functions in C++
Functions in C++ programming language.pptx
Chapter 3 - Characters and Strings - Student.pdf

More from Niit Care (20)

PPS
Ajs 1 b
PPS
Ajs 4 b
PPS
Ajs 4 a
PPS
Ajs 4 c
PPS
Ajs 3 b
PPS
Ajs 3 a
PPS
Ajs 3 c
PPS
Ajs 2 b
PPS
Ajs 2 a
PPS
Ajs 2 c
PPS
Ajs 1 a
PPS
Ajs 1 c
PPS
Dacj 4 2-c
PPS
Dacj 4 2-b
PPS
Dacj 4 2-a
PPS
Dacj 4 1-c
PPS
Dacj 4 1-b
PPS
Dacj 4 1-a
PPS
Dacj 1-2 b
PPS
Dacj 1-3 c
Ajs 1 b
Ajs 4 b
Ajs 4 a
Ajs 4 c
Ajs 3 b
Ajs 3 a
Ajs 3 c
Ajs 2 b
Ajs 2 a
Ajs 2 c
Ajs 1 a
Ajs 1 c
Dacj 4 2-c
Dacj 4 2-b
Dacj 4 2-a
Dacj 4 1-c
Dacj 4 1-b
Dacj 4 1-a
Dacj 1-2 b
Dacj 1-3 c

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Big Data Technologies - Introduction.pptx
PPT
Teaching material agriculture food technology
PPTX
MYSQL Presentation for SQL database connectivity
PDF
KodekX | Application Modernization Development
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
cuic standard and advanced reporting.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Approach and Philosophy of On baking technology
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation theory and applications.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Unlocking AI with Model Context Protocol (MCP)
20250228 LYD VKU AI Blended-Learning.pptx
Understanding_Digital_Forensics_Presentation.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Big Data Technologies - Introduction.pptx
Teaching material agriculture food technology
MYSQL Presentation for SQL database connectivity
KodekX | Application Modernization Development
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
cuic standard and advanced reporting.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Approach and Philosophy of On baking technology
MIND Revenue Release Quarter 2 2025 Press Release
Network Security Unit 5.pdf for BCA BBA.
Reach Out and Touch Someone: Haptics and Empathic Computing
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation theory and applications.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Unlocking AI with Model Context Protocol (MCP)

Aae oop xp_06

  • 1. 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
  • 2. 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
  • 3. 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
  • 4. 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
  • 5. 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
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. 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
  • 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 Just a 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 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
  • 15. 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
  • 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