You are on page 1of 6

Course Title: Application of Computer Programming for Optimization

of Ship Design
Course Code: NAME 430 June 13,
2021

We have seen simple programs where all programming effort is contained in main(). This
works very well for really small programs and applications. The larger and more complex
your program gets, the longer the contents of main() become, unless you choose to structure
your program using functions. So the editing becomes difficult.

Functions give you a way to compartmentalize and organize your program’s execution logic.
They enable you to split the contents of your application into logical blocks that are invoked
(Calling Functions) sequentially.

A function is a subprogram that optionally takes parameters and returns a value, and it needs
to be invoked to perform its task. In this lesson we will learn-
need for programming functions
prototypes and function definition
parameters to functions and returning values from them
functions
functions

Functions in C++

A function is a set of statements that take zero or more arguments (Parameters and
arguments are almost same), does some specific computation and returns some value.
All C++ programs have at least one function - that is the main() function.
The idea is to put code that is repeated multiple times into a function so that instead of
writing the same code again and again for different inputs, we can call the function.

Why do we need functions?

Functions help us in reducing code redundancy (Using code repeatedly). If the same
functionality is necessary at multiple places in the project, then rather than writing the
same code, again and again, we create a function and call it whenever needed.
This also helps with code maintenance as we only have to change the code in one place
and that change is good for every time the function gets called.
Consider a big project with thousands of lines of code. Using functions helps to reduce the
number of lines of code, makes the code easier to read and make code maintenance easier.

→There are two tasks that are required when coding functions: provide a function prototype
and provide the function definition.

Problem:
Write a program that asks the user to enter the radius of a circle and then computes the
circumference and area.
Course Title: Application of Computer Programming for Optimization
of Ship Design
Course Code: NAME 430 June 13,
2021
Course Title: Application of Computer Programming for Optimization
of Ship Design
Course Code: NAME 430 June 13,
2021

What Is a Function Prototype?


The function prototype basically tells what a function is called (the name, Area), the list
of parameters the function accepts (one parameter, a double called InputRadius) and the
return type of the function (a double).

What Is a Function Definition?

A function definition is always comprised of a statement block. A return statement is


necessary unless the function is declared with return type void. In this case, Area needs to
return a value because the return type of the function has not been declared as void.
The statement block contains statements within open and closed braces ({…}) that are
executed when the function is called.

What Is a Function Call, and What Are Arguments?

Invoking a function is also called making a function call. When a function is declared as one
with parameters, the function call needs to send arguments that are values the function
requests within its parameter list.
Course Title: Application of Computer Programming for Optimization
of Ship Design
Course Code: NAME 430 June 13,
2021

Programming a Function with Multiple Parameters

Function That Accepts Two Parameters to Compute the Surface Area of a Cylinder

Scope of Variables in C++


There are mainly two types of variable scopes:
1. Local Variables
2. Global Variables

Local Variables
Variables defined within a function or block are said to be local to those functions.
Anything between ‘{‘ and ‘}’ is said to inside a block.
Local variables do not exist outside the block in which they are declared, i.e. they
cannot be accessed or used outside that block.
Declaring local variables: Local variables are declared inside a block.
Global Variables
As the name suggests, Global Variables can be accessed from any part of the
program.
They are available throughout the life time of a program.
They are declared at the top of the program outside all of the functions or blocks.
Declaring global variables: Global variables are usually declared outside of all of the
functions and blocks, at the top of the program. They can be accessed from any
portion of the program.
Course Title: Application of Computer Programming for Optimization
of Ship Design
Course Code: NAME 430 June 13,
2021

Return Statement in C++


The return statement returns the flow of the execution to the location from where it is
called. As soon as the return statement is executed, the flows of the program stop
immediately and return the control from where it was called.

Programming Functions with No Parameters


Function Parameters with Default Values
Recursion—Functions That Invoke themselves:

In certain cases, you can actually have a function call itself. Such a function is called a
Recursive function. Note that a recursive function should have a very clearly defined
exit condition where it returns without invoking itself again.

In the absence of an exit condition or in the event of a bug in the same, your program
execution gets stuck at the recursive function that won’t stop invoking itself, and this
eventually stops when the stack overflows, causing an application crash.

Overloading Functions
Functions with the same name and return type but with different parameters or set of
parameters are said to be overloaded functions. Overloaded functions can be quite useful
in applications where a function with a particular name that produces a certain type of
output might need to be invoked with different sets of parameters.
Course Title: Application of Computer Programming for Optimization
of Ship Design
Course Code: NAME 430 June 13,
2021

Assignment:

1. Write overloaded functions that calculate the volume of a sphere and a cylinder.
The formulas are the following:
Volume of sphere = (4 * Pi * Radius * Radius * Radius) / 3
Volume of a cylinder = Pi * Radius * Radius * Height

2. What is wrong with the following code?

#include <iostream>
using namespace std;
const double Pi = 3.1416;
void Area(double Radius, double Result)
{
Result = Pi * Radius * Radius;
}
int main()
{
cout << “Enter radius: “;
double Radius = 0;
cin >> Radius;
double AreaFetched = 0;
Area(Radius, AreaFetched);
cout << “The area is: “ << AreaFetched << endl;
return 0;
}

3. What is wrong with the following function declaration?


double Area(double Pi = 3.14, double Radius);

4. Write a function with return type void that still helps the caller calculate the area
and circumference of a circle when supplied the radius.

5. Using Recursive Functions to Calculate a Number in the Fibonacci Series.

You might also like