4_Functions
4_Functions
Lengthier programs
– Prone to errors
– tedious to locate and correct the errors
To overcome this
Programs broken into a number of smaller
logical components, each of which serves a
specific task.
04/21/25
1
Modularization
2
04/21/25
Advantages of modularization
• Reusability
• Debugging is easier
• Build library
04/21/25
3
Functions
Standard functions
(library functions or built in functions)
User-defined functions
Written by the user(programmer)
04/21/25
5
Defining a Function
Name
– You should give functions descriptive names
– Same rules as variable names, generally
Return type
– Data type of the value returned to the part of
the program that activated (called) the
function.
04/21/25 6
Functions
Parameter list
– A list of variables that hold the values being
passed to the function
Body
– Statements enclosed in curly braces that
perform the function’s operations(tasks)
04/21/25 7
The general form of a function definition
return_type
function_name(parameter_definition)
{
variable declaration;
statement1;
statement2;
.
.
.
return(value_computed);
}
04/21/25
8
Functions: understanding
}
{
cout << “hello world\ Body
n”;
}
04/21/25
9
Functions
Return type Function Parameter
name List
void DisplayMessage(void)
}
{ cout << “Hello from the function”
<< “DisplayMessage.\n”;
} Body
void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;
}
04/21/25 10
Functions
Return type Function Parameter
name List
void DisplayMessage(void)
}
{ cout << “Hello from the function”
<< “DisplayMessage.\n”;
} Body
void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;
}
04/21/25 11
Functions
Return type Function Parameter
name List
void DisplayMessage(void)
}
{ cout << “Hello from the function”
<< “DisplayMessage.\n”;
} Body
void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;
}
04/21/25 12
Return type Function Parameter
name List
void DisplayMessage(void)
{ cout << “Hello from the function”
}
<< “DisplayMessage.\n”;
} Body
void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;
}
04/21/25 13
Return type Function Parameter
name List
void DisplayMessage(void)
}
{ cout << “Hello from the function”
<< “DisplayMessage.\n”;
} Body
void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;
}
04/21/25 14
Return type Function Parameter
name List
void DisplayMessage(void)
{ cout << “Hello from the function”
}
<< “DisplayMessage.\n”; Body }
void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;}
04/21/25 15
Return type Function Parameter
name List
void DisplayMessage(void)
}
{ cout << “Hello from the function”
<< “DisplayMessage.\n”;
} Body
void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;
}
04/21/25 16
Return type Function Parameter
name List
void DisplayMessage(void)
}
{ cout << “Hello from the function”
<< “DisplayMessage.\n”;
} Body
void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;
}
04/21/25 17
// FUNCTION DEFINITION
void DisplayMessage(void)
{ cout << “Hello from the function”
<< “DisplayMessage.\n”;
}
void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;
}
04/21/25 18
void DisplayMessage(void); //fn declaration
void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;
}
// FUNCTION DEFINITION
void DisplayMessage(void)
{ cout << “Hello from the function”
<< “DisplayMessage.\n”;
04/21/25
} 19
void First (void)
{ cout << “I am now inside function First\n”;}
void Second (void)
{ cout << “I am now inside function Second\n”;
} // book has now as not
void main ()
{ cout << “I am starting in function main\n”;
First ();
Second ();
cout << “Back in function main again.\n”;
04/21/25 } 20
Function- definition & call
Return type Function Parameter
name List
void DisplayMessage(void)
}
{ cout << “Hello from the function”
<< “DisplayMessage.\n”;
} Body
void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;
}
04/21/25 21
Arguments ( parameters)
Both arguments ( parameters) are variables used in a
program & function.
Variables used in the function reference or function
call are called as arguments. These are written
within the parenthesis followed by the name of the
function. They are also called actual parameters.
Variables used in function definition are called
parameters, They are also referred to as formal
parameters.
04/21/25 22
Home Work:
To be solved …Functions
Write appropriate functions to
1. Find the factorial of a number ‘n’.
2. Reverse a number ‘n’.
3. Check whether the number ‘n’ is a palindrome.
4. Generate the Fibonacci series for given limit ‘n’.
5. Check whether the number ‘n’ is prime.
6. Generate the prime series using the function
written for prime check, for a given limit.
04/21/25 23
To be solved … Functions
Factorial of a given number ‘n’
long factFn(int); long factFn(int num) //factorial
//prototype calculation
{
void main() { int i, fact=1;
long n, f;
cout<<"Enter the for (i=1; i<=num; i++)
number to evaluate its fact=fact * i;
factorial:";
cin>>n; return (fact); //returning the
f =factFn(n); //function factorial
call }
cout<<"\nFact of
"<<n<<" is "<< f;
04/21/25 24
}
To be solved …Functions
04/21/25 28
Function- definition & call
void dispChar(int n, char c);//proto-type
l r sdeclaration
a te void dispChar(int n, char c) // function
r m e
m
F o ra definition
p a
{
cout<<" You have entered "<< n<<
“&” <<c;
void } main(){ //calling program
int no; char ch;
cout<<"\nEnter a number & a character: \
n"; Actual
cin>>no>>ch; parameters
dispChar( no, ch); //Function
04/21/25 reference 29
}
Functions- points to note
1. The parameter list must be separated by commas.
dispChar( int n, char c);
2. The parameter names do not need to be the same in the
prototype declaration and the function definition.
3. The types must match the types of parameters in the function
definition, in number and order.
void dispChar ( int n, char c);//proto-type
void dispChar ( int num, char letter) // function
definition
{ cout<<" You have entered "<< n<< “&”
<<c; }
4. Use of parameter names in the declaration is optional.
void dispChar ( int , char);//proto-type
04/21/25 30
Functions- points to note
04/21/25 31
Functions- Categories
values.
04/21/25 33
Fn with Arguments/parameters &
No return values
void dispPattern(char c )
{ int i;
for (i=1;i<=20 ; i++)
cout << c;
cout<<“\n”;}
04/21/25 37