0% found this document useful (0 votes)
15 views4 pages

Lecture 04

Uploaded by

sourovtarofder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Lecture 04

Uploaded by

sourovtarofder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

C++ Functions

A function is a block of code that performs a specific task. C++ program must contain
a main function to execute the other functions. Functions divide a complex problem
into smaller chunks and make 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

Standard Library Functions:


<cmath> library have a lot of predefined functions such as max(x, y), pow(x, y) etc.
max(10, 15): return maximum value from 10 and 15, i.e. 15.
pow(2, 3): return 3 power of 2, i.e. 23 = 8.

User Define Functions:


The syntax to declare a function is:
returnType functionName (parameter1, parameter2,...)
{
// function body
}
 returnType: All data types can be the return type such as void, int, float, double
string, char, etc.

Functions without any parameters:


Example of void-return type function:
void myFunction() {
cout << "The user defined function has been executed!";
}

int main() {
myFunction();
return 0;
}
# void means that the function does not have any return value.
Example of int-return type function:
int myFunction() {
int myVal1 = 10;
return myVal1;
}

int main() {
int myVal2 = 20;
cout << "Sum of two values = " << myFunction()+myVal2;
return 0;
}

Functions with parameters:


Example of void-return type function:
void displaySum(int num1, int num2) {
int sum = num1 + num2;
cout << "The sum of " << num1 << " and " << num2 << " is: " << sum
<<endl;
}

int main() {
displaySum(10, 20);
displaySum(-5, 8);
return 0;
}
Example of int-return type function:
int max(int x, int y) {
if (x > y)
return x;
else
return y;
}
int main() {
int a = 10, b = 20;
int m = max(a, b);
cout << "Larger value is " << m;
return 0;
}

Other way to use functions in C++:


#include <iostream>
using namespace std;

// function declaration
int max(int num1, int num2);

int main () {
int a = 100;
int b = 200;
int ret;
ret = max(a, b);
cout << "Max value is : " << ret << endl;
return 0;
}

// Function definition
int max(int num1, int num2) {
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Default parameter:
#include<iostream>
using namespace std;
//Function declaration
void addressFunction(string , int , string district="Gazipur");
int main()
{
addressFunction("Raja", 34);
return 0;
}

//Function definition
void addressFunction(string nickname, int age = 0, string district)
{
cout<<"Let's welcome "<<age<<" years old Mr/Ms "<<nickname<<"
from "<<district<<" in the C++ class.";
}

You might also like