Programming For Engineers Lecture 07
Programming For Engineers Lecture 07
Loops
While
Do while
1
Train your mind……….!!!
2
Programming Toolkit
• Decisions
• Loops
• Sequences
Laboratory Stool
Constructing a laboratory Stool
Constructing a laboratory Stool
7
C++ Functions
• C++ functions work in largely the same way. Format of a C++ function call:
• functionName(argumentList)
• where the argumentList is a comma-separated list of arguments (data being sent into
the function).
• In general, function arguments may be constants, variables or more complex
expressions.
• Functions are building blocks
• Also called modules, methods, procedures, or sub-procedures
• Like miniature programs
• Can be put together to form larger program
• One of two principle means of modularization in C++ (other is classes)
8
Why use Functions?
• Divide and Conquer
• Allow complicated programs to be divided into manageable components
• Programmer can focus on just the function: develop it, debug it, and test it
• Various developers can work on different functions simultaneously
• Reusability:
• Can be used in more than one place in a program--or in different programs
• Avoids repetition of code, thus simplifying code maintenance
• Can be called multiple times from anywhere in the program
• Components:
• Custom functions and classes that you write
• Prepackaged functions and classes available in the C++ Standard Library
9
Predefined Functions
• Using predefined functions:
• C++ Standard Library contains many predefined functions to perform
various operations
• Predefined functions are organized into separate libraries
• I/O functions are in iostream header
• Math functions are in cmath header
• Some predefined C++ mathematical functions:
• pow(x,y)
• sqrt(x)
• floor(x)
10
• Power Function - pow(x,y):
• Power function pow(x,y) has two parameters
• pow(x,y) returns value of type double
• pow(x,y) calculates x to the power of y: pow(2,3) = 8.0
• x and y called parameters (or arguments) of function pow
• Square Root Function - sqrt(x):
• Square root function sqrt(x) has only one parameter
• sqrt(x) returns value of type double
• sqrt(x) calculates non-negative square root of x, for x >= 0.0: sqrt(2.25) = 1.5
• Floor Function - floor(x):
• Floor function floor(x) has only one parameter
• floor(x) returns value of type double
• floor(x) calculates largest whole number not greater than x: floor(48.79) = 48.0
11
Math.h
#include < math.h >
double sqrt ( double );
• int main()
•{
• double number, squareRoot;
• cout << "Enter a number: ";
• cin >> number;
13
14
15
Example:
• n general, function arguments may be constants, • cout << sqrt(49); // because of automatic
variables or more complex expressions type conversion rules
• The pre-defined math function sqrt listed above • // we can send an int where a double is
takes one input value (of type double) and returns expected
its square root. Sample calls:
• // this call returns 7.0
• double x = 9.0, y = 16.0, z;
• z = sqrt(36.0); // sqrt returns 6.0 (gets stored in z) • // in this last one, sqrt(625.0) returns 25.0,
which gets sent as the
• z = sqrt(x);// sqrt returns 3.0 (gets stored in z)
• // argument to the outer sqrt call. This one
• z = sqrt(x + y);// sqrt returns 5.0 (gets stored in z)
returns 5.0, which gets
• // printed
• cout << sqrt(100.0);// sqrt returns 10.0, which
gets printed
• cout << sqrt(sqrt(625.0));
16
User-Defined Functions
• Two types:
• Void functions (nonvalue-returning): no return type, do not return a value
• Value-returning functions: have a data type, return only one value to caller
• When utilizing functions:
• Include correct header file
• Know function name
• Know number of parameters, if any
• Know data type of parameters
• Know data type of value computed by function
17
• Value-returning function used 3 ways:
• Assignment statement
• Output statement
• Argument (actual parameter) in another function call
• Creating functions:
•
Mnemonic: "ProDeCall":
• Prototype
• Definition
• Call
18
What we will study today …
https://www.youtube.com/watch?v=2UqzHffkFbU
23
Declaration of Function
return-value-type function-name( argument--type-list) ;
main ( )
{
:
}
Example
int function-name ( int , int , double ) ;
void main ( )
{
….
}
Definition of Function
Definition
int x ;
x = square ( i ) ;
Example : Square of a Number
32
function example with definition before main()
33
Default Values for Parameters
• When you define a function, you can specify a default value for each
of the last parameters. This value will be used if the corresponding
argument is left blank when calling to the function.
• This is done by using the assignment operator and assigning values for
the arguments in the function definition. If a value for that parameter
is not passed when the function is called, the default given value is
used, but if a value is specified, this default value is ignored and the
passed value is used instead. Consider the following example −
34
Consider the following example −
• #include <iostream> • int b = 200;
• using namespace std; • int result;
• •
• int sum(int a, int b = 20) { • // calling a function to add the values.
• result = sum(a, b);
• int result;
• cout << "Total value is :" << result << endl;
• result = a + b;
•
• // calling a function again as follows.
• return (result); • result = sum(a);
•} • cout << "Total value is :" << result << endl;
• int main () { •
• // local variable declaration: • return 0;
• int a = 100; •}
35
Flow of Execution
• Program Execution:
36
Call by Value or Reference
https://www.youtube.com/watch?v=ErMKBh1pobg
37
Call By Value
Calling function
Called function
Area of the Ring
Area of Outer Circle ____ Area of Inner Circle = Area of the Ring
Example: Function to calculate
the area of a circle
}
Exercises
1. Modify the raise to power function so that it
can handle negative power of x, zero and
positive power of x.
main ( )
{
double x = 123.456 ;
square ( &x ) ;
}
Value of ‘x’ is not passed , but the memory
address of ‘x’ is passed
Example: Call by Reference
#include <iostream.h>
Prototype
Assignment List with
Return value
data type
Circumference = 2 * pi * radius
Scope of Identifiers
• Identifier is any name user creates in his/her program