












































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Dr. Mehandi Nandakumar delivered this lecture at Baddi University of Emerging Sciences and Technologies for Introduction to Computer Programming course. Its main points are: Functions, Block, Code, Execution, Programs, Memory, C , Internal, External, Argument, Square, Roots
Typology: Slides
1 / 52
This page cannot be seen from the preview
Don't miss anything!













































Wednesday, May 2, 2012
ļ§
A function is a self contained block of codewith a specific purpose. ļ§
When a function is called, the function code isexecuted and when it is finished, the executionof the calling program continues immediatelyafter the point where the function was called. Functions
C++ allows the use of both internal (user-defined) and external functions. ļ§
External functions (e.g.,
abs
ceil
rand
sqrt, etc.) are usually grouped into specializedlibraries (e.g.,
iostream
stdlib
math
etc.)
5
Using functions āMath Library functions ļ§
C++ includes a library of Math functionsyou can use. ļ§
You have to know how to
call
these
functions before you can use them. ļ§
You have to know what they return. ļ§
You donāt have to know how they work!
7
The stuff we give to a function is called theargument(s).
is the argument here.
A C++ function canāt change the value ofan argument! ļ§
If
y
was
before we call
sqrt
, it will
always be
after we call
sqrt
8
int
i;
for
(i=1;i<10;i++)
cout
sqrt(i)
ā\nā;
But I thought we had to give
sqrt()
a double?
C++ does automatic
type conversion
for you.
10
ceil
floor
cos
sin
tan
exp
log
log
pow
fabs
fmod
ļ½
A
function definition
consists of
two parts
:
ļ½
The function Header:^ ā¦^
data type of the return value; ā¦^
name of the function; ā¦^
and the functionās parameter list (enclosed in parenthesis),e.g. int square(int value);
ļ½
The function Body:^ ā¦^
consists of the code to be executed when the function iscalled.
ļ½
If the function does not return any value then
void
may be used instead of return value. ļ½
If the function does not take any parameters, then^ void
can be used within parenthesis to show this.
How to define a function?
// Program 8.1 Calculating powersā¦ā¦ā¦ā¦.. Continuedint main(){ cout << endl;// Calculate powers of 8 from -3 to +3for(int i = -3 ; i <= 3 ; i++)
cout <<power(8.0, i)<< "
";
cout << endl;return 0;}
// Program 8.1 Calculating powers: Output
1
8
64
512
ļ½
Information is passed to a function using arguments by thecalling program. Argument is the actual value passed tothe function. ļ½
The function definition contains the parameters list whichreflects the data type expected by the function. ļ½
The arguments which are specified when calling a functionreplace the parameters that are used in definition. ļ½
The data types of arguments and parameters are generallythe same (if they are different, implicit cast may takeplace). It is not necessary that the name should be thesame. ļ½
If a return type is specified then the function returns asingle value of that type when its execution finishes.
// Program 8.2 Calculating powers - rearranged#include
// Prototype for power function
int main(){ cout << endl;// Calculate powers of 8 from -3 to +3for(int i = -3 ; i <= 3 ; i++)
cout <<power(8.0, i)<< "
";
cout << endl;return 0;}
// Program 8.2 Calculating powers ā rearrangedā¦ā¦ā¦ā¦ā¦. Continued// Function to calculate x to the power ndouble power(double x, int n){ double result = 1.0;if(n >= 0)
*for(int i = 0 ; i < n ; i++)result = x;elsefor(int i = 0 ; i < -n ; i++)result /= x;return result; }