Programming For Engineers Lecture 07 11
Programming For Engineers Lecture 07 11
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
Functions with no type. The use of void
•
The syntax shown above for functions:
•Requires the declaration to begin with a type. This is the type of the
value returned by the function. But what if the function does not
need to return a value? In this case, the type to be used is void,
which is a special type to represent the absence of value. For
example, a function that simply prints a message may not need to
return any value:
34
Void Function example
• // void function example • #include <iostream>
• using namespace std;
• #include <iostream> • void PrintMessage();
• void main(void)
• using namespace std;
•{
• char name[10];
• cout << "What is your first name? ";
• void printmessage () • cin >> name;
•{ • cout << "\n\nHello " << name << endl << endl;
• // Here is the function call
• cout << "I'm a function!"; • PrintMessage();
•} •}
• // Following is the function definition - function heading and code
• void PrintMessage()
• int main () •{
• cout << "********************\n";
•{ • cout << " Welcome to my\n";
• printmessage (); • cout << " wonderful program!\n";
• cout << "********************\n";
•} •}
35
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 −
36
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; •}
37
Flow of Execution
• Program Execution:
38
Call by Value or Reference
https://www.youtube.com/watch?v=ErMKBh1pobg
39
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
51
Function Overloading
52
• Function refers to a segment that groups code to perform a specific task.
• In C++ programming, two functions can have same name if number and/or type of arguments
passed are different.
• These functions having different number or type (or both) of parameters are known as
overloaded functions. For example:
• int test() { }
• int test(int a) { }
• float test(double a) { }
• int test(int a, double b) { }
53
• Here, all 4 functions are overloaded functions because argument(s) passed to these
functions are different.
• Notice that, the return type of all these 4 functions are not same. Overloaded functions
may or may not have different return type but it should have different argument(s).
• // Error code
• int test(int a) { }
• double test(int b){ }
• The number and type of arguments passed to these two functions are same even though
the return type is different. Hence, the compiler will throw error.
54
Example 1: Function Overloading
• #include <iostream> • return 0;
• using namespace std; •}
55
• Output • Here, the display() function is
called three times with different
type or number of arguments.
• Integer number: 5
• Float number: 5.5
• The return type of all these
• Integer number: 5 and float functions are same but it's not
number: 5.5 necessary.
56
Write a Program to compute absolute value which
Works both for integer and float
• #include <iostream> • int absolute(int var) {
• using namespace std;
• if (var < 0)
• int absolute(int); • var = -var;
• float absolute(float); • return var;
• int main() { •}
• int a = -5;
• float b = 5.5;
• float absolute(float var){
•
• cout << "Absolute value of " << a << " = " << absolute(a) • if (var < 0.0)
<< endl;
• var = -var;
• cout << "Absolute value of " << b << " = " << absolute(b);
• return 0; • return var;
•} •}
57
• Output • int absolute(int var) {
• if (var < 0)
• Absolute value of -5 = 5 • var = -var;
• Absolute value of 5.5 = 5.5 • return var;
• In the above example, two functions absolute() •}
are overloaded. • When absolute() function is called with float as an
argument, this function is called:
58
Header Files
#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