Circuit Chapter 3 - 4-1
Circuit Chapter 3 - 4-1
function
Introduction to function
✓ A function is a complete unit of executable code that can perform
specific task
✓ It is a named independent section of a C++ code that performs a
specific tasks and optionally returns value to the calling function.
✓ Function is considered as a fundamental building of the language
Syntax
General form: type-specifier function-name (argument
declaration)
{
body of the function;
return (expression);}
✓ Argument — is a comma separated list of variable names that
receive the values, when function is called, (parenthesis
required even no arguments)
Types of function
✓ There are two types of functions in C++ programming:
✓ One of the best ways to tackle a problem is to start with the overall
goal, then divide this goal into several smaller tasks
Declaring function
➢ The interface of a function (also called its prototype) specifies how
it may be used. It consists of three entities
➢ The function return type. This specifies the type of value the
function returns. A function which returns nothing should have a
return type void.
➢ The function name. this is simply a unique identifier
➢ The function parameters (also called its signature). This is a set of
zero or more typed identifiers used for passing values to and from
the function.
Defining a function
➢ When the function is called and x passed to it, num receives a copy
of the value of x.
Cont. …
Num = 0
x = 10
➢ C++ enables you to create more than one function with the same name.
this is called function overloading
➢ The functions must differ in their parameter list, with a different type of
parameter, a different number of parameters, or both. Here's an example:
➢ The first and the second versions differ in the types of the parameter, and
the third differs in the number of parameter.
cont. ..
➢ You should note that two functions with the same name and parameter
list, but different return types, generate a compiler error.
➢ Analysis: - the double function is overloaded with int ,long, double and float
➢ In the body of the main program, eight local variables are declared.
➢ When double() function is called, the calling function does not distinguish
which one to call. It just pass in an argument, and the correct one is invoked