You are on page 1of 15

Functions

Functions
• A function is a block of code that performs a
specific task.
• 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++ Program Structure
Functions
• C++ programs usually have the following
form:

// include statements
// function prototypes
// main() function
// function definitions
Functions - Terminology
A function definition specifies
1. The name of the function
2. The types and number of parameters it
expects to receive
3. Its return type.
4. Function body with the declarations of its
local variables, and the statements to
perform the specific task assigned to the
function
Declaration of a function
Function definition
returnType functionName (parameter1, parameter2,...) {
// function body
}
– Return statement terminates execution of the current function
– Control returns to the calling function (the function who calls this
function)
– if return is an expression then
• The value of expression is returned as the value of the function
call
• Only one value can be returned (Exception for arrays).

– The return type is not mandatory, in case of no return type the


return type must be void (keyword)- at the function prototype and
also at the function definition.
main() is the ‘calling function’
greet() is the ‘called function’
Example
#include <iostream>
using namespace std;

void myFunction() {
cout << "Welcome";
}

int main() {
myFunction();
return 0;
}
• A function can be called multiple times:
#include <iostream>
using namespace std;
void myFunction() {
cout << "Welcome";
}
int main() {
myFunction();
myFunction();
myFunction();
return 0;
}
Example 2: Function with Parameters
#include <iostream>
using namespace std;
// declaring a function
int add(int a, int b) {
return (a + b);
}
int main() {
int sum,x,y;
cout<< " Enter x,y values "<<endl;
cin>>x>>y;
sum = add(x,y);
cout << " sum of x and y = " << sum << endl;
return 0;
}
// Another Way of Writing functions ( declaration first and definition at
last)
#include <iostream>
using namespace std;
int addFunc(int, int);
int main() {
int x, y, sum;
cout << "Enter two numbers: ";
cin >> x >> y;
sum = addFunc(x, y);
cout <<"The sum of "<<x<< " and " <<y<<" is: "<<sum;
return 0;
}
int addFunc(int num1, int num2) {
int addFunc;
addFunc = num1 + num2;
return addFunc;
}
//Returning value as expression
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
int main()
{
int sum,x,y;
cout<< " Enter x,y values "<<endl;
cin>>x>>y;
sum = add(x,y);
cout << " sum of x and y = " << sum << endl;
return 0;
}
// Program to perform arithmetic operations using functions
#include<iostream> int div(int m,int n)
using namespace std; {
int sum(int a,int b) return m/n;
{ }
int rem=a+b; int main()
return rem; {
} int m,n,p;
int sub(int x,int y) cout<<"Enter m,n values"<<endl;
{ cin>>m>>n;
return x-y; p=sum(m,n);
} cout<<"sum ="<<p<<endl;
void mul(int m,int n) cout<<"Difference="<<sub(m,n);
{ cout<<endl;
cout<<"Product="<<m*n; mul(m,n);
cout<<endl; p=div(m,n);
} cout<<"Division="<<p<<endl;
int mod(int m,int n) p=mod(m,n);
{ cout<<"Modulo="<<p<<endl;
return m%n; return 0;
} }
Parameter Passing methods
• Call by value

• Call by reference
Function parameters
■ Actual parameters ( In main() )
■ Actual parameters (also known as arguments) are what are passed
by the caller.

■ Formal parameters ( Function Definition we use parameters are


called as Formal Parameters)

■ Formal parameters are the parameters as they are known in the


function definition.

You might also like