0% found this document useful (0 votes)
24 views34 pages

Lec 11 - Functions - I

Uploaded by

5rqz945t6w
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views34 pages

Lec 11 - Functions - I

Uploaded by

5rqz945t6w
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

CS-114

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

Various constructs to help us write useful programs


• Variables & Input/output statements
• Arithmetic & Assignment statements
• Sequential and conditional statements
• Iteration/looping constructs
• Arrays

All encapsulated within “main( )”


Overview of This Lecture
• Break away from the monopoly of “main()”
Have other sub-units of a program that can
compute
Reduce the burden of programming each and
Hello
everything Dear Functions!!!
in “main()”

• A complex problem is often easier to solve by


dividing it into several smaller parts, each of which
can be solved by itself. (divide and conquer)
Introduction to Functions
• Functions are the building blocks of C++
programs.

• A function is a named block of code that performs


some ACTION.
Introduction to Functions
• The statements written in a function are executed
when it is called by its name.
• Each function has a UNIQUE NAME.

• One function can be called MULTIPLE TIMES in a


single program.
Advantages of Functions
• Easier to Understand
• Easier to Modify
• Easier to Maintain
• Provide Program Reusability
• Separate the Concept (what is done) from the
Implementation (how it is done)
C++ Functions
C++ Function – syntax

There are three main function components:


• Function Prototypes
• Function Definitions
• Function Call
Function Prototypes
A function prototype is a declaration of
a function that specifies the function's name
and type signature but omits the function body.

Return-type Function-name(parameter-list);

Function Note the


signature semicolon here
Function Definitions

A set of statements that explains what a function


does is called Function Definition.

Return-type Function-name(parameter-list)
{
statement(s);
Here we can have 0, 1 or
} many parameters
Function Definitions

In an program, a function definition can be written


at:
• Before main() function
• After main() function
• In a separate file
Function Prototype - Example

Return- Function
type -name Parameter-
list

int Sum(int a , int b);


Function Definition - Example
Return-
Function Parameter-
type
-name list

int Sum(int a , int b)


{
int sum;
sum = a + b; Multiple parameters should be
return sum; separated by commas
}
Function Call

• A statement that activates a function is known


as a Function Call.

• One function calls another by using function


signature.
Function Call

The following steps take


place when a function is
called:
1. The control moves to the
function that is called.
2. All statements in function
body are executed.
3. Control returns back to
calling function.
Function Call – Syntax

Function-name(parameter-list);
C++ Functions
C++ allows the use of both internal and external
functions.

• Internal Functions – User defined

• External functions – Pre-defined (e.g., abs,


ceil, rand, sqrt, etc.) are usually grouped
into specialized libraries (e.g., iostream,
stdlib, math, etc.)
Internal Functions

C++ programs usually have the following form:

// include statements
// function prototypes
// main() function – has the function call
// function definitions
Internal Functions

C++ programs usually have the following form:

// include statements What if we skip the


prototype here?
// function prototypes
// main() function – has the function call
// function definitions
Internal Functions

C++ programs usually have the following form:


Give the function
// include statements definition before the
function is called
// function definitions
// main() function – has the function call
External Functions
C++ Function – Example (Cube)
#include <iostream>
using namespace std;
Function
int Cube (int n); prototype

int main()
{
int yourNumber = 14; Function Function
int myNumber = 9; Call Call

cout << "My Number =" << myNumber ;


cout << " its cube is" << Cube (myNumber) << endl ;
cout << "Your Number = " << yourNumber ;
cout << " its cube is " << Cube (yourNumber) << endl ;
}

int Cube (int n)


Function
{
definition
return n * n * n ;
}
C++ Function – Example (Cube)
#include <iostream>
using namespace std;
Function
int Cube (int n); prototype

int Cube (int n)


{ Function
return n * n * n ; definition
}

int main()
{
int yourNumber = 14; Function Function
int myNumber = 9; Call Call

cout << "My Number =" << myNumber ;


cout << " its cube is" << Cube (myNumber) << endl ;
cout << "Your Number = " << yourNumber ;
cout << " its cube is " << Cube (yourNumber) << endl ;
}
C++ Function – Example (Cube)
#include <iostream>
using namespace std;

int Cube (int n)


{
return n * n * n ; Function
} definition

int main()
{
int yourNumber = 14; Function Function
int myNumber = 9; Call Call

cout << "My Number =" << myNumber ;


cout << " its cube is" << Cube (myNumber) << endl ;
cout << "Your Number = " << yourNumber ;
cout << " its cube is " << Cube (yourNumber) << endl ;
}
C++ Function – Example (AddNumbers)
#include <iostream>
using namespace std;

int addition (int a, int b) Function


{ definition
int r;
r = a + b;
return r;
}
Function
void main () Call
{
int z;
z = addition (5,3);
cout << "The result is " << z;

}
Exercise

Write a program which calls a function which


prints ten asterisks (*) in line. (**********).

In this example main function just calls the


function named asterisks which just prints the
line of ten asterisks.
Class Work – Solution
#include <iostream>
using namespace std;

void asterisks (int x)


{
for (int i=0; i<x; i++)
cout << "*";
}

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!”

• Write a function called welcome() which takes


name of the user as parameter and when called
from main() displays the message
“Hello [nameOfUser]!”
Parameters & Arguments
Classified by Location

Parameters Arguments

Always appear in Always appear in


the function definition, a function call.
or function prototype.
Parameters & Arguments – Example
#include <iostream>
#include <cstdlib> Parameter
using namespace std;

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

The illiterate of the 21st century will not be those who


cannot read and write,
but those who cannot learn, unlearn, and relearn.
--Unknown

You might also like