functions in programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Functions

By
Anam Javaid

COMSATS University Islamabad, Wah Campus


Previous Lecture

Pointers

Arrays and Pointers

Double Pointers
Lecture Outline
Introduction to Functions

Defining Functions

Calling Function

Function Prototype

Void Functions
Functions
• A program may be broken up into manageable functions.

▪ A function is a collection of statements that performs a specific task.


1) We have created a function named main in every program we’ve written
2) We have used library functions such as strlen, strcat
• Functions are commonly used to break a problem down into small
manageable pieces.
Functions Provide Reusability
• Another reason to write functions is that they simplify programs
• If a specific task is performed in several places in a program, a
function can be written once to perform that task, and then be
executed anytime it is needed.
• For example: We need to find the sum of integers from 1 to 10, from
20 to 37, and from 35 to 49, respectively.
Reusability: Example
Defining Function
• A function definition contains the statements that make up the
function.
• A function definition consists of its function name, parameters, return
value type,and body.
• The syntax for defining a function is as follows:
Defining Function: Example
• Let’s look at a function created to find which of two integers is bigger.
Caution
• In the function header, you need to declare each parameter
separately. For instance, int max(int num1, int num2) is correct, but
max(int num1, num2) is wrong.
Calling a Function
• Calling a function executes the code in the function.
• To use a function, you have to call or invoke it.
• There are two ways to call a function, depending on whether or not it
returns a value.
• If the function returns a value, a call to that function is usually treated
as a value. For example,

• calls max(3, 4) and assigns the result of the function to the variable
larger.
Calling a Function
• Another example of such a call is:
cout << max(3, 4);
which prints the return value of the function call max(3, 4).

• When the max function is invoked, the flow of control transfers to its
definition. Once the max function is finished, it returns control to the
caller.
Calling a Function
Activation Record
• Each time a function is invoked, the system creates an activation
record (also called an activation frame) that stores its arguments and
variables for the function and places the activation record in an area
of memory known as a call stack.
• Stack is Last in First out (LIFO).
• When a function calls another function, the caller’s activation record
is kept intact and a new activation record is created for the new
function call.
• When a function finishes its work and returns control to its caller, its
activation record is removed from the call stack.
Activation Record
More about Functions
a) Function with no return type and no arguments
More about Functions
b) Function with a return type but no arguments
More about Functions
c) Function with no return type while having arguments.
More about Functions
d) Function with both return type and arguments.
Function Prototypes
• A function prototype declares a function without having to implement
it.
• Before a function is called, its header must be declared.
• One way to ensure this is to place the definition before all function
calls.
• Another approach is to define a function prototype before the
function is called. A function prototype, also known as function
declaration, is a function header without implementation. The
implementation is given later in the program.
Function Prototype

No need to declare a function Need to declare a function otherwise


error will be generated that “ sum is not
decaled in this scope”
Class Activity 1
• Write function headers (not the bodies) for the following functions:
a) Write a function prototype which takes two floating point numbers as
arguments and return their product.
b) Write a function prototype which takes a lower case alphabet as an
argument and returns its equivalent upper case alphabet.
c) Write a function prototype which takes a string and an integer as an
argument and Displays that string a specified number of times provided in
integer value.
d) Display a message a specified number of times.
Solution
a) float prod(float a , float b);
b) char alpha(char );
same as
char alpha(char c);

c) void display(string s, int a);


d) void dis();
Class Activity 2
Home Activity
• What would be wrong with not writing a return statement in a value-
returning function? Can you have a return statement in a void
function? Does the return statement in the following function cause
syntax errors?
void p(double x, double y)
{
cout << x << " " << y << endl;
return x + y;
}
Conclusion

Basics of functions

Passing and returning values to functions


Next Lecture

Default Arguments

Inline Functions

Function Overloading

Local, Global, and Static Local Variables

You might also like