0% found this document useful (0 votes)
6 views30 pages

Ch6C++ Functions

Uploaded by

Mohammed Ibrahim
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
6 views30 pages

Ch6C++ Functions

Uploaded by

Mohammed Ibrahim
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 30

C++

LANGUAGE
C++ Functions
C++ FUNCTIONS

 In this tutorial, we will learn about the C++ function and function expressions with the help
of examples.
 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. We can create two
functions to solve this problem:

 a function to draw the circle

 a function to color the circle

 Dividing a complex problem into smaller chunks makes our program easy to understand
and reusable.

 There are two types of function:

 Standard Library Functions: Predefined in C++

 User-defined Function: Created by users


C++ USER-DEFINED FUNCTION

 C++ allows the programmer to define their own function.

 A user-defined function groups code to perform a specific task and that group of
code is given a name (identifier).

 When the function is invoked from any part of the program, it all executes the
codes defined in the body of the function.

 C++ Function Declaration


 The syntax to declare a function is:

 returnType functionName (parameter1, parameter2,...) {


 // function body
 }
HERE'S AN EXAMPLE OF A FUNCTION DECLARATION.

 // function declaration
 void greet() {
 cout << "Hello World";
}
 Here,

 the name of the function is greet()


 the return type of the function is void
 the empty parentheses mean it doesn't have any parameters
 the function body is written inside {}
 Note: We will learn about returnType and parameters later in this
tutorial.
CALLING A FUNCTION

 In the above program, we have declared a function named greet(). To use the
greet() function, we need to call it.

 Here's how we can call the above greet() function.

 int main() {

 // calling a function
 greet();
EXAMPLE 1: DISPLAY A TEXT

 #include <iostream>

 using namespace std;

 // declaring a function

 void greet() {

 cout << "Hello there!";


 }

 int main() {

 // calling the function


 greet();

 return 0;
 }
FUNCTION PARAMETERS

 As mentioned above, a function can be declared with


parameters (arguments). A parameter is a value that
is passed when declaring a function. We pass a value to the function parameter
while calling the function.
 For example, let us consider the function below:
int main() {
int n = 7;

 void printNum(int num) { // calling the function


 cout << num; // n is passed to the function as
argument
} printNum(n);
 Here, the int variable num is the function parameter.
return 0;
}
EXAMPLE 2: FUNCTION WITH PARAMETERS

 / program to print a text

int main() {
 #include <iostream>
 using namespace std; int num1 = 5;
double num2 = 5.5;

 // display a number // calling the function


displayNum(num1, num2);
 void displayNum(int n1, float n2) {
return 0;
 cout << "The int number is " << n1; }
 cout << "The double number is " << n2;
}
 In the above program, we have used a function that has one int parameter
and one double parameter.
 We then pass num1 and num2 as arguments. These values are stored by
the function parameters n1 and n2 respectively.
RETURN STATEMENT

 In the above programs, we have used void in the function declaration. For example,

 void displayNumber() {
 // code
 }
 This means the function is not returning any value.

 It's also possible to return a value from a function. For this, we need to specify the returnType of
the function during function declaration.

 Then, the return statement can be used to return a value from a function.

 For example,

 int add (int a, int b) {


 return (a + b);
 }
 Here, we have the data type int instead of void. This means that the
function returns an int value.

 The code return (a + b); returns the sum of the two parameters as the
function value.

 The return statement denotes that the function has ended. Any code after
return inside the function is not executed.
EXAMPLE 3: ADD TWO NUMBERS

 // program to add two numbers using a function

 #include <iostream>

int sum;
 using namespace std;
// calling the function and storing
// the returned value in sum
sum = add(100, 78);
 // declaring a function
 int add(int a, int b) { cout << "100 + 78 = " << sum << endl;

 return (a + b); return 0;


} }

 int main() {
 In the above program, the add() function is used to find the sum of two
numbers.
 We pass two int literals 100 and 78 while calling the function.
 We store the returned value of the function in the variable sum, and then we
print it.
FUNCTION PROTOTYPE

 In C++, the code of function declaration should be before the function call.
However, if we want to define a function after the function call, we need to use
the function prototype. For example,

// function prototype
void add(int, int);

int main() {
// calling the function before declaration.
add(5, 3);
return 0;
}

// function definition
void add(int a, int b) {
cout << (a + b);
}
 In the above code, the function prototype is:

 void add(int, int);


 This provides the compiler with information about the function name and
its parameters. That's why we can use the code to call a function before
the function has been defined.

 The syntax of a function prototype is:

 returnType functionName(dataType1, dataType2, ...);


EXAMPLE 4: C++ FUNCTION PROTOTYPE
 // using function definition after main() function
 // function prototype is declared before main()

int sum;
 #include <iostream>
// calling the function and storing
// the returned value in sum
 using namespace std; sum = add(100, 78);

cout << "100 + 78 = " << sum << endl;


 // function prototype
return 0;
 int add(int, int);
}

// function definition
 int main() {
int add(int a, int b) {
return (a + b);
}
BENEFITS OF USING USER-DEFINED FUNCTIONS

 Functions make the code reusable. We can declare them once and use
them multiple times.
 Functions make the program easier as each small task is divided into a
function.
 Functions increase readability.
C++ Library Functions
Library functions are the built-in functions in C++ programming.

Programmers can use library functions by invoking the functions directly; they don't need to
write the functions themselves.

Some common library functions in C++ are sqrt(), abs(), isdigit(), etc.

In order to use library functions, we usually need to include the header file in which these
library functions are defined.

For instance, in order to use mathematical functions such as sqrt() and abs(), we need to
include the header file cmath.
EXAMPLE 5: C++ PROGRAM TO FIND THE SQUARE ROOT OF A NUMBER

 #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;
 }
 In this program, the sqrt() library function is used to calculate the square
root of a number.

 The function declaration of sqrt() is defined in the cmath header file. That's
why we need to use the code #include <cmath> to use the sqrt() function.
TYPES OF USER-DEFINED FUNCTIONS
IN C++
USER-DEFINED FUNCTIONS CAN BE
CATEGORISED AS:
 For better understanding of arguments and return in functions,
 Function with no argument and no return value
 Function with no argument but return value
 Function with argument but no return value
 Function with argument and return value
 Consider a situation in which you have to check prime number. This
problem is solved below by making user-defined function in 4 different
ways as mentioned above.
EXAMPLE 1: NO ARGUMENTS PASSED AND NO RETURN VALUE

 # include
<iostream> // Return type of function is void
because value is not returned.
 using namespace std;
void prime()
{ if (flag == 1)
{
 void prime(); int num, i, flag = 0; cout << num << " is not a
prime number.";
cout << "Enter a positive }
 int main() integer enter to check: "; else
{ cin >> num; {
cout << num << " is a
 // No argument is for(i = 2; i <= num/2; ++i) prime number.";
passed to prime() { }

if(num % i == 0) }
prime(); {
 return 0; flag = 1;
break;
}
}
}
 In the above program, prime() is called from the main() with no arguments.

 prime() takes the positive number from the user and checks whether the
number is a prime number or not.

 Since, return type of prime() is void, no value is returned from the function.
EXAMPLE 2: NO ARGUMENTS PASSED BUT A RETURN VALUE

 #include <iostream>
if (flag == 1) / Return type of function is int
 using namespace std;
{ int prime()
cout<<num<<" is {
 int prime();
not a prime number."; int n;
}
 int main() else printf("Enter a positive integer to
 { { check: ");
 int num, i, flag = 0; cout<<num<<" is cin >> n;
a prime number.";
 // No argument is
passed to prime()
} return n;
 num = prime();
return 0; }
 for (i = 2; i <= num/2;
}
++i)
 {
 if (num%i == 0)
 {
 flag = 1;
 break;
 }
 }
 In the above program, prime() function is called from the main() with no
arguments.

 prime() takes a positive integer from the user. Since, return type of the
function is an int, it returns the inputted number from the user back to the
calling main() function.

 Then, whether the number is prime or not is checked in the main() itself
and printed onto the screen.
EXAMPLE 3: ARGUMENTS PASSED BUT NO RETURN VALUE

 #include <iostream>

 using namespace std; // There is no return value to


calling function. Hence, return if (flag == 1)
type of function is void. */ {
 void prime(int n);
void prime(int n) cout << n << " is not a
{ prime number.";
 int main() int i, flag = 0; }
 { for (i = 2; i <= n/2; ++i) else {

{ cout << n << " is a prime
int num;
if (n%i == 0) number.";
 cout << "Enter a
positive integer to { }
check: "; flag = 1; }
 cin >> num; break;

}
}
 // Argument num is
passed to the function
prime()
 prime(num);
 return 0;
 }
 In the above program, positive number is first asked from the user which is
stored in the variable num.

 Then, num is passed to the prime() function where, whether the number is
prime or not is checked and printed.

 Since, the return type of prime() is a void, no value is returned from the
function.
EXAMPLE 4: ARGUMENTS PASSED AND A RETURN VALUE .
 #include <iostream>
 using namespace std; /* This function returns integer value. */
int prime(int n)
 int prime(int n); {
int i;
 int main()
for(i = 2; i <= n/2; ++i)
 {

{
int num, flag = 0;
 cout << "Enter positive integer
if(n % i == 0)
to check: "; return 1;
 cin >> num; }
 // Argument num is passed to
check() function return 0;
 flag = prime(num); }

 if(flag == 1)
 cout << num << " is not a
prime number.";
 else
 cout<< num << " is a prime
number.";
 return 0;
 }
 In the above program, a positive integer is asked from the user and stored
in the variable num.

 Then, num is passed to the function prime() where, whether the number is
prime or not is checked.

 Since, the return type of prime() is an int, 1 or 0 is returned to the main()


calling function. If the number is a prime number, 1 is returned. If not, 0 is
returned.

 Back in the main() function, the returned 1 or 0 is stored in the variable


flag, and the corresponding text is printed onto the screen.

You might also like