Top Banner

of 25

MELJUN CORTES C++_Functions

Jun 02, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/10/2019 MELJUN CORTES C++_Functions

    1/25

    USINGFUNCTIONS

    IN VISUAL C++

    MELJUN CORTES MBA MPA BSCS

  • 8/10/2019 MELJUN CORTES C++_Functions

    2/25

    MELJUN CORTES, MBA,MPA,BSCS

    FUNCTION

    A function is a group of computer instructionsthat performs a well-defined task inside a computerprogram.

    Functions provide a way to break up a large

    program into more manageable parts. Functionsalso make it possible to perform the same task atvarious points within the program without repeatingthe code.

    In computer programming, functions are alsocalled subprograms. Basically, a function is aprogram by itself it may have input(s), output(s)and will perform some kind of processing steps.

  • 8/10/2019 MELJUN CORTES C++_Functions

    3/25

    MELJUN CORTES, MBA,MPA,BSCS

    CALLING THE FUNCTION

    In Visual C++ the act of transferring control to afunction is known as calling the function. When

    a function is called, you supply a function name

    and list of parameters, if any.

  • 8/10/2019 MELJUN CORTES C++_Functions

    4/25

    MELJUN CORTES, MBA,MPA,BSCS

    CALLING THE FUNCTION

    The following steps take place when a function iscalled:

    The compiler makes a note of the location from which

    the function was called and makes a copy of the

    parameter list, if any.

    Any storage required for the function to execute is

    temporarily created.

    The called function starts executing, using copies of the

    data that was supplied in the parameter list.

    After the function has finished executing, control is

    returned to the calling function and memory used by the

    function is released.

  • 8/10/2019 MELJUN CORTES C++_Functions

    5/25

    MELJUN CORTES, MBA,MPA,BSCS

    TYPES OF FUNCTIONS

    Pre-defined functions are those functionsprogrammers can already use without having to

    write codes for them. Some examples of pre-

    defined functions in C++ are cout and cin library

    functions.

    User-defined functionsare function that we are

    going to write/implement by ourselves. The main()

    function is a good example of a user-definedfunction although we are not limited to writing just

    one user-defined function.

  • 8/10/2019 MELJUN CORTES C++_Functions

    6/25

    MELJUN CORTES, MBA,MPA,BSCS

    USER-DEFINED FUNCTIONS

    User-defined functions are group of statements that is executedwhen it is called from some point of the program. The following isits format:

    type name ( parameter1, parameter2, ...) { statements }

    type the data type specifier of the data returned by the

    function. name is the identifier by which it will be possible to call thefunction.

    parameter consists of a data type specifier followed by anidentifier, like any regular variable declaration (for example: int x)and which acts within the function as a regular local variable. They

    allow to pass arguments to the function when it is called. Thedifferent parameters are separated by commas. You can use themas many as needed.

    statements the function's body. It is a block of statementssurrounded by braces { }.

  • 8/10/2019 MELJUN CORTES C++_Functions

    7/25MELJUN CORTES, MBA,MPA,BSCS

  • 8/10/2019 MELJUN CORTES C++_Functions

    8/25MELJUN CORTES, MBA,MPA,BSCS

    // user-defined function example#include using namespace std;

    int addition (int a, int b){ int r;

    r=a+b;

    return (r); }

    int main (){ int z;

    z = addition (5,3);cout

  • 8/10/2019 MELJUN CORTES C++_Functions

    9/25MELJUN CORTES, MBA,MPA,BSCS

    // user-defined function example#include using namespace std;

    int addition (int a, int b);

    int main (){ int z;

    z = addition (5,3);cout

  • 8/10/2019 MELJUN CORTES C++_Functions

    10/25MELJUN CORTES, MBA,MPA,BSCS

    PREDEFINED FUNCTIONS

    Pre-defined functions (also called built-infunctions) are stored in what we calllibraries. In C++, a library is a collection offunctions related to a particular application.

    To be able to use these functions in ourprograms, we have to declare their librarythrough the #include (read as macro

    include) directive.- have written by for us

  • 8/10/2019 MELJUN CORTES C++_Functions

    11/25MELJUN CORTES, MBA,MPA,BSCS

    STANDARD C++ LIBRARY

  • 8/10/2019 MELJUN CORTES C++_Functions

    12/25MELJUN CORTES, MBA,MPA,BSCS

    MATH FUNCTIONS

    The C++ math functions provides quite anumber of math functions. In order to use mathfunctions, header file or isincluded into the program.

  • 8/10/2019 MELJUN CORTES C++_Functions

    13/25MELJUN CORTES, MBA,MPA,BSCS

    Most commonly used functions (from cmath)Funct ion s Returns Examples

    abs(x) the absolute value of x abs(-12) is 12

    (an int) abs(3) is 3ceil(x) the smallest integer not ceil (5.1) is 6.0

    less than x ceil (-0.9) is 0.0

    floor(x) the largest integer not floor (5.9) is 5.0

    greater than x floor (99.99) is 99.00pow(x,y) x to the y power (x^y) pow(2,5) (=2^5) 32.0

    sqrt(x) the square root of x sqrt(81) is 9.0

    labs(l) the absolute value of l labs(-40000)is40000

    (a long)

    ceil (5.1) return 6.0

    ceil (6.0)

    floor(5.0) return 5.0

    floor(5.9)

    Except for abs &

    labs, all arguments

    and return values

    are of type double

  • 8/10/2019 MELJUN CORTES C++_Functions

    14/25MELJUN CORTES, MBA,MPA,BSCS

    # include

    #include

    using namespace std;

    int main()

    {

    int x;x = 2;

    cout

  • 8/10/2019 MELJUN CORTES C++_Functions

    15/25

    MELJUN CORTES, MBA,MPA,BSCS

  • 8/10/2019 MELJUN CORTES C++_Functions

    16/25

    MELJUN CORTES, MBA,MPA,BSCS

  • 8/10/2019 MELJUN CORTES C++_Functions

    17/25

    MELJUN CORTES, MBA,MPA,BSCS

    CHARACTER FUNCTIONS

    The C++ char functions are extremely

    useful for testing and transforming characters.

    These functions are widely used and accepted.

    In order to use character functions headerfile or is included into the

    program. Some of the commonly used

    character functions are:

  • 8/10/2019 MELJUN CORTES C++_Functions

    18/25

    MELJUN CORTES, MBA,MPA,BSCS

    CHARACTER FUNCTIONS

    isalnum() - The function isalnum() returns nonzero value if its argument

    is either an alphabet or integer. If the character is not an integer or alphabetthen it returns zero.

    isalpha() - The function isalpha() returns nonzero if the character is anuppercase or lower case letter otherwise it returns zero.

    isdigit() - The function isdigit() returns nonzero if the character is a digit,i.e. through 0 to 9. It returns zero for non digit character.

    islower() - The function islower() returns nonzero for a lowercase letterotherwise it returns zero.

    ispunct() - The function ispunct() returns nonzero for punctuationcharacters otherwise it returns zero. The punctuation character excludesalphabets, digits and space.

    isupper()- The function isupper() returns nonzero for an uppercase letterotherwise it returns zero.

  • 8/10/2019 MELJUN CORTES C++_Functions

    19/25

    MELJUN CORTES, MBA,MPA,BSCS

    isxdigit()- The function isxdigit() returns nonzero for hexadecimaldigit i.e. digitfrom 0 to 9, alphabet 'a' to 'f' or 'A' to 'F' otherwise it returns zero.

    tolower() - The function tolower() changes the upper case letter to itsequivalent lower case letter. The character other than upper case letter remains

    unchanged.

    toupper()-The function toupper() changes the lower case letter to itsequivalent upper case letter. The character other than lower case letter remainsunchanged.

    iscntrl() - The function iscntrl() returns nonzero if the character is a controlcharacter otherwise it returns zero.

    isgraph() - The function isgraph() returns nonzero if the character is anyprintable character other than space otherwise it returns zero.

    isprint() - The function isprint() returns nonzero for printable characterincluding space otherwise it returns zero.

    isspace() - The function isspace() returns nonzero for space, horizontal tab,newline character, vertical tab, formfeed, carriage return; otherwise it returns zero.

    http://en.wikipedia.org/wiki/Hexadecimalhttp://en.wikipedia.org/wiki/Hexadecimal
  • 8/10/2019 MELJUN CORTES C++_Functions

    20/25

    MELJUN CORTES, MBA,MPA,BSCS

  • 8/10/2019 MELJUN CORTES C++_Functions

    21/25

    MELJUN CORTES, MBA,MPA,BSCS

    STRING FUNCTIONS

    It stores a collection of functions for stringmanipulation . In order to use stringfunctions, header file or isincluded into the program.

  • 8/10/2019 MELJUN CORTES C++_Functions

    22/25

    MELJUN CORTES, MBA,MPA,BSCS

  • 8/10/2019 MELJUN CORTES C++_Functions

    23/25

    MELJUN CORTES, MBA,MPA,BSCS

  • 8/10/2019 MELJUN CORTES C++_Functions

    24/25

    MELJUN CORTES, MBA,MPA,BSCS

  • 8/10/2019 MELJUN CORTES C++_Functions

    25/25