0% found this document useful (0 votes)
13 views32 pages

Chapter - 6 User-Defined Function I

Uploaded by

parkwooby05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views32 pages

Chapter - 6 User-Defined Function I

Uploaded by

parkwooby05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

CHAPTER - 6

User-Defined Function I
Why use in function?
• must learn to break the problem into
manageable pieces.
1.functions previously defined
2.then discusses user-defined functions.
• Functions are like building blocks.
• divide complicated programs into manageable
pieces
• Functions are often called modules.
2
They have other advantages, too:
• While working on one function, you can focus on just
that part of the program and construct it, debug it, and
perfect it.
• Different people can work on different functions
simultaneously.
• If a function is needed in more than one place in a
program or in different programs, you can write it once
and use it many times.
• Using functions greatly enhances the program’s
readability because it reduces the complexity of the
function main.
Predefined Functions
• review a concept from a college algebra course
• a function can be considered a rule or
correspondence between values, called the function’s
arguments
• In C++, the concept of a function, either predefined or
user-defined, is similar to that of a function in
algebra.
• every function has a name and, depending on the
values specified by the user, it does some
computation.
• power function, pow(x, y)
• x and y are called the parameters (or arguments)
of the function pow
• The square root function, sqrt(x), calculates the
nonnegative square root of x for x >= 0.0. For
example, sqrt(2.25) is 1.5.
User-Defined Functions
• classified into two categories:
I. Value-returning functions: specific data
type using the return statement
II. Void functions: do not use a return
statement
Value-Returning Functions
1. The name of the function
2. The number of parameters, if any
3. The data type of each parameter
4. The data type of the value computed (that is,
the value returned) by the function, called the
type of the function
5. The code required to accomplish the task
Because the value returned by a value-
returning function is unique,
To use the value in one of three ways:
• Save the value for further calculation.
• Use the value in some calculation.
• Print the value.
• a value-returning function is used:
I.In an assignment statement.
II.As a parameter in a function call.
III.In an output statement.
• First Four properties form what is called the
heading of the function (also called the
function header)
• Fifth property is called the body of the
function
The variable declared in the heading of the function
abs is called the formal parameter of the function
abs.

Formal Parameter: A variable declared in


the function heading.
Actual Parameter: A variable or expression
listed in a call to a function.
functionType is the type of the value that the function returns (data
type or the return type)
Thus, to call a value returning function, you use its name,
with the actual parameters (if any) in parentheses.

which expr is a variable, constant value, or


Function Prototype
• Function Prototype: The function heading
without the body of the function.
• we place function prototypes before any
function definition (including the definition of
main).
Value-Returning Functions
• A value-returning function must return a
value.
• If the value of the parameter, x, is greater than
5, it returns twice the value of x; otherwise,
the value of x remains unchanged.
• Recall that in a value-returning function, the
return statement returns the value.
• Consider the following return statement:

• If a return statement contains more than one


expression, only the value of the last
expression is returned.
• For example, suppose that the input is 18281.
• Because the first and last digits of 18281 are
the same, remove the first and last digits to get
the number 828.
• Repeat this process of comparing the first and
last digits on 828.
• Once again, the first and last digits are the
same.
• After removing the first and last digits of 828,
the resulting number is 2, which is less than 10.
• Thus, 18281 is a palindrome.
• To remove the first and last digits of num, you
first need to find the highest power of 10 that
divides num and call it pwr.
• The highest power of 10 that divides18281 is
4, that is, pwr = 4.
• Now18281 % 10pwr = 8281, so the first digit is
removed.
• Also, because 8281 / 10 = 828, the last digit is
removed.

You might also like