0% found this document useful (0 votes)
2 views7 pages

C++ Functions

The document provides examples of various functions in C++ including calling functions, returning values, calculating sums, finding maximum values, computing factorials, and summing the digits of a number. Each example includes code snippets and their corresponding output. The document serves as a tutorial for basic function usage in C++ programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

C++ Functions

The document provides examples of various functions in C++ including calling functions, returning values, calculating sums, finding maximum values, computing factorials, and summing the digits of a number. Each example includes code snippets and their corresponding output. The document serves as a tutorial for basic function usage in C++ programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1) How to call a function?

#include <iostream>
using namespace std;

//Function definition
void printHello()
{
cout <<"Hello";
}

int main() {

//Function call
printHello();

return 0;
}

Output:
Hello

2) To return a value
#include <iostream>
using namespace std;

//Function definition
int printHello(){
cout <<"Hello\n";
cout << "Val: ";
return 3;
}

int main() {
//Function call
cout << printHello() << endl;

return 0;
}

Output:
Hello
Val: 3
3) Functions to find sum of 2 nos

#include <iostream>
using namespace std;

int sum(int a,int b){


int s = a + b;
return s;
}

int main() {
cout << "Sum: " << sum(10,20) << endl;

return 0;
}

output:
Sum: 30

4) Functions to find sum of first 5 nos


#include <iostream>
using namespace std;

int SUM(int n){


int sum = 0;
for(int i=0;i<=n;i++){
sum += i; //sum = sum + i;
}
return sum;
}

int main() {
cout << "Sum: " << SUM(6) << endl;

return 0;
}

Output:
Sum: 21
5) Functions to find maximum of 2 numbers

#include <iostream>
using namespace std;

int Max(int a,int b){

if(a > b){


return a;
}
else return b;
}

int main() {
cout << "Max: " << Max(60,20) << endl;

return 0;
}

Output:
60

6) Functions to find factorial of a no.


#include <iostream>
using namespace std;

int Fact(int n){

int fact = 1;
for(int i=1;i<=n;i++){
fact *= i ; // fact = fact * i
}
return fact;
}

int main() {
cout << "Factorial: " << Fact(5) << endl;

return 0;
}

Output:
120

7) Functions to find sum of the digits of a number.


#include <iostream>
using namespace std;

int SumOfDigit(int num){

int digiSum = 0;
while(num>0){
int lastDigit = num % 10 ;
num = num/10;

digiSum = digiSum + lastDigit;


}
return digiSum;
}

int main() {
cout << "Sum of digits: " << SumOfDigit(521) << endl;

return 0;
}

Output:
Sum of digits: 8

You might also like