Lec 11 - Functions - I
Lec 11 - Functions - I
Fundamentals of Programming
Lecture 11
Functions – I
Course Instructor:
Qurrat-ul-ain Babar Institute of Geographical Information Systems
Road Map for Today
• Functions
• Parameters & Arguments
A Quick Recap
Return-type Function-name(parameter-list);
Return-type Function-name(parameter-list)
{
statement(s);
Here we can have 0, 1 or
} many parameters
Function Definitions
Return- Function
type -name Parameter-
list
Function-name(parameter-list);
C++ Functions
C++ allows the use of both internal and external
functions.
// include statements
// function prototypes
// main() function – has the function call
// function definitions
Internal Functions
int main()
{
int yourNumber = 14; Function Function
int myNumber = 9; Call Call
int main()
{
int yourNumber = 14; Function Function
int myNumber = 9; Call Call
int main()
{
int yourNumber = 14; Function Function
int myNumber = 9; Call Call
}
Exercise
int main ()
{
Int x;
Cin >> x;5
fetchusername(5);
}
Recap – Example
#include <iostream>
using namespace std; No. of
parameters?
int absolute(int a)
{
int r; ?
r = abs(a);
return r;
}
Function
int main () Prototype?
{
int z; output?
z = absolute (-5);
cout << "The result is " << z;
}
Recap – An Exercise
• Write a function called DisplayMessage() which
when called from main() displays the message
“It’s a beautiful day today!”
Parameters Arguments
int absolute(int a)
{
int r;
r = abs(a);
return r;
}
Argument
void main ()
{
int z;
z = absolute (-5);
cout << "The result is " << z;
}
Thank You