0% found this document useful (0 votes)
23 views18 pages

C++-Lesson 4 Functions

Uploaded by

thamandishe97
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
23 views18 pages

C++-Lesson 4 Functions

Uploaded by

thamandishe97
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

Learning Outcomes of the Week: WEEK 6

Computer Programming II C++

FUNCTIONS
a) Refer to pdf: Chap_04
b) C++ How to program chapter 6

4/17/2024 © 2022. Cavendish University. Rights Reserved 0


Introduction

1: A function is a group of statements that together


perform a task. Every C++ program has at least one
function, which is main()
2: A function is a block of code that performs a specific
task.
• Suppose we need to create a program to create a circle
and color it repeatedly . We can create two functions to
solve this problem:
i. a function to draw the circle
ii. a function to color the circle
Introduction

• There are two types of function:


1.Standard Library Functions: Predefined in C++
2.User-defined Function: Created by users

The C++ standard library provides numerous built-


in functions that your program can call. eg,
function strcat() to concatenate two strings,
function memcpy() to copy one memory location
to another location and many more functions.
( C++ Standard Library Headers page 205:
Deitel, C++ How to program) eg: <iostream>, <cstdlib><ctime>
N.B: Don’t confuse Header: <> and function: ()
Math Library Functions
• Math library functions
– Allow the programmer to perform common mathematical
calculations
– Are used by including the header file <cmath>
• Functions called by writing
functionName (argument)
• Example
cout << sqrt( 900.0 );
– Calls the sqrt (square root) function. The preceding
statement would print 30
– The sqrt function takes an argument of type double and
returns a result of type double, as do all functions in the
math library
Math Library Functions

• Function arguments can be


– Constants
sqrt( 4 );
– Variables
sqrt( x );
– Expressions
sqrt( sqrt( x ) ) ;
sqrt( 3 - 6x );
– Other notable examples include: from <cmath>
All trigonometry functions
Pow() etc etc

\
return_type function_name( parameter list ) { body of the function }

A function is known with various names like a


method or a sub-routine or a procedure etc.

User defined function


• Defining a Function
The general form of a C++ function definition is as
follows −
return_type function_name( parameter list ) {

body of the function

}
Why Write Functions?

i. Reusability
ii. Fewer errors introduced when code isn’t rewritten
iii. Reduces complexity of code
iv. Programs are easier to maintain
v. Programs are easier to understand
vi. Less time when re coding
return_type function_name( parameter list ) { body of the function }

Function Definition

A C++ function definition consists of a function header


and a function body. Here are all the parts of a function

i. Return Type − A function may return a value.


The return_type is the data type of the value the
function returns. Some functions perform the desired
operations without returning a value. In this case, the
return_type is the keyword void.
ii. Function Name − This is the actual name of the
function. The function name and the parameter list
together constitute the function signature.
return_type function_name( parameter list ) { body of the function }

Function Definition

A C++ function definition consists of a function header


and a function body. Here are all the parts of a function

i. Parameters − A parameter is like a placeholder. When a


function is invoked, you pass a value to the
parameter. This value is referred to as actual
parameter or argument. The parameter list refers to
the type, order, and number of the parameters of a
function. Parameters are optional; that is, a function
may contain no parameters.
ii. Function Body − The function body contains a collection
of statements that define what the function does.
return_type function_name( parameter list ) { body of the function }

Example: Following is the source code for a function


called max().

// function returning the max between two numbers


int max(int num1, int num2) {
// local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result; }
return_type function_name( parameter list ) { body of the function }

Example: Following is the source code for a function


called max().

#include <iostream>
#include <cmath>
using namespace std;
int main() {
double number, squareRoot;
number = 25.0;
// sqrt() is a library function to calculate the square root
squareRoot = sqrt(number);
cout << "Square root of " << number << " = " << squareRoot;
return 0; } //make it Dynamic
return_type function_name( parameter list ) { body of the function }

Example:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double number, squareRoot;
cout << "Enter Number :: ";
cin >> number;
// sqrt() is a library function to calculate the square root
squareRoot = sqrt(number);
cout << "Square root of " << number << " = " << squareRoot;
return 0;
}
return_type function_name( parameter list ) { body of the function }

Example: Calculate the distance between two points


return_type function_name( parameter list ) { body of the function }

Example: Calculate the distance between two points

To calculate the distance between two points on a grid


using C++, you can use the Pythagorean theorem, which
states that the distance between two points (x1, y1) and
(x2, y2) is the square root of ((x2 - x1) ^ 2 + (y2 - y1) ^ 2).
return_type function_name( parameter list ) { body of the function }

Function overloading
With function overloading, multiple
functions can have the same name with
different parameters:
Example:
int myFunction(int x)
float myFunction(float x)
double myFunction(double x, double y)
return_type function_name( parameter list ) { body of the function }

Consider the following example, which have two functions


that add numbers of different type:
int plusFuncInt(int x, int y) {
return x + y;
}
double plusFuncDouble(double x, double y) {
return x + y;
}
int main() {
int myNum1 = plusFuncInt(8, 5);
double myNum2 = plusFuncDouble(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
return 0;
}
Instead of defining two functions that should do the same thing, it is
better to overload one.
we overload the plusFunc function to work for both int and double:
return_type function_name( parameter list ) { body of the function }

Consider the following example, which have two functions


that add numbers of different type:
int plusFunc(int x, int y) {
return x + y;
}

double plusFunc(double x, double y) {


return x + y;
}

int main() {
int myNum1 = plusFunc(8, 5);
double myNum2 = plusFunc(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
return 0;
}
Function Overloading

• Function overloading
– Having functions with same name and different parameters
– Should perform similar tasks ( i.e., a function to square ints,
and function to square floats).
int square( int x) {return x * x;}
float square(float x) { return x * x; }
– Program chooses function by signature
• signature determined by function name and parameter types
– Can have the same return types

You might also like