You are on page 1of 15

Functions

int add (int num 1, int num2){


___ ________
____ __ ______
_________ _____ }
Functions
Itn add( int num 1, int num 2)
{ ___ ____
____ ___ Need
___ __ __ }
___ ___ Implementation
{____ __
___
__ ____ _
Execution
___ ____
___ }
Factorial of
a number
Formula of a factorial
Factorial of n,n!= n*n-1*n2……..**1
Find the factorial of 2 numbers given as input

Int n1,n2;
Cin>>n1>>;

Int fact=1;
For (int i=2;i<=n1;i++){
Fact 1=fact 1*I;
}

Int fact 2=1;


For(int i=2;i<=n2;i++ ){
Fact2=fact*I;
}
Cout<<fac1<<“ ”<<fact2<<endl;
Function is a
piece of code
that performs a
specific task
Factorial function
factorial (n)
n1!= Factorial (n1)
n2!=Factorial(n2)
Syntax of a Function
Return type function name (parameter 1, parameter 2,…… ){

}
Write a function to add 2 integers
Int add (int num 1,int num 2){
Int sum=num1+num2;
Return sum;}

Int main () {
Return 0; }
Int main () {
Abc () ;
Return 0;
}
Write a function to add 2 integers
Int add (int num1 ,int num2 ){
Int sum = num 1+num 2;
Return sum;
}
Int main () {
Int a=2;
Int b=3;
Cout<<add(a,b)<<endl;
Return 0;
}
Write a function to add 2 integers
Void print (int num){
Cout<<num<<endl;
Return ;
}
Int add (int num 1, int num 2){
Print (num1);
Print (num 2);
Int sum=num 1+ num 2;
Return sum;
}
Int main () {
Int a=2;
Int b=3;
Cout <<add(a,b)<<endl;
Return 0;
}
Concept of
call stack
Call stack

Void function A() { Memory assigned Function A


Function code,
}
Local variables ,
Parametrs
Write a function to add 2 integers
Void print (int num){
Cout<<num<<endl;
Return;
}
Int add (int num1,int num 2){
Print (num 1);
Print (num 2);
Int sum=num 1+ num 2;
Return sum;
}
Int main (){
Int a=2;
Int b=3;
Cout<<add(a,b)<<endl;
Return 0;}

You might also like