0% found this document useful (0 votes)
7 views

File 2. Functions

The document explains the concept of functions in programming, including their definition, syntax, and examples of different types of functions such as computational, manipulative, and procedural. It also covers function prototypes, signatures, return statements, and the distinction between actual and formal parameters. Additionally, it discusses call by value and call by reference, pure and impure functions, and function overloading.

Uploaded by

chalikwarjai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

File 2. Functions

The document explains the concept of functions in programming, including their definition, syntax, and examples of different types of functions such as computational, manipulative, and procedural. It also covers function prototypes, signatures, return statements, and the distinction between actual and formal parameters. Additionally, it discusses call by value and call by reference, pure and impure functions, and function overloading.

Uploaded by

chalikwarjai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

FUNCTIONS

Definition : One or more statements written in the balanced set of


braces for a specific task is a function(method)(module/code).

Syntax :
[access specifier] [access modifier] <return type>
<function name>(parameter list)
{
Body of the function
}

Example :
public static void main(int a, int b)
{
int c = a+b;
System.out.println(“Sum = “+c);
}

Example of Functions with arguments(parameters), no arguments


(empty parameter list), no return value and a return value.

class C1
{
int x;//data member
//No argument(parameter) No return function
void display()//called fn
{
x=53; //data member accessed in function
System.out.println("My first oop");
System.out.println("Value of x = "+x);
}// return - go back to calling function

//argument, no return
void add(int a, int b)//called fn
{
int c=a+b;
1
System.out.println("Sum="+c);
} // -implicit return

//Argument, return
int product(int a, int b) //called fn
{
int c=a*b;
return c; //return statement is compulsory here
}
}

class C2
{
public static void main() //calling fn
{
C1 ob = new C1();
ob.display();//fn call stmt
int x,y,z;
x=12;
y=10;
ob.add(x,y);//fn call stmt
z=ob.product(x,y);
System.out.println("Product = "+z);
}
}

Function Prototype and Signature :


The first line of a function definition is a function prototype.
Note (reference) : It includes access specifier and modifier optionally
and <return type> <function name> (parameter list) compulsorily.

The function signature refers to parameter list. It is a part of function


prototype.
E.g. int add (int a, int b) – prototype

Signature
2
Practice Question 1 :
Write a prototype for a function check which checks the equality
between two integers received and returns 0 or 1.

Answer : int check (int a, int b)

Practice Question 2 :
Specify the function signature of the above check function.

Answer : int a, int b

return statement / keyword


return is a jump statement of Java and has two purposes:
1. an immediate exit from a function and return to the calling function.
2. returns a value to the calling function.

Syntax : return [value];

Important points (for understanding)


1. If the return type of a function is void and the function does not
have a return statement, an implicit call to return is given.

2. If the return type of a function is other than void then return


statement is compulsory.

3. Only 1 value can be returned by a return statement, that is by a


function.

Keyword void
When a function does not return a value, keyword(data type) void is
used as a return type of a function. It indicates that the function
returns to the calling function without a value.

3
Types of Functions:

1. Computational function : This type of a function calculates the value


and returns the calculated value. Examples:
i. Math.sqrt(16);
ii. int add (int a, int b)
{
return (a+b);
}

2. Manipulative function : This type of a function manipulates (checks)


the information and returns the success or a failure of a code.
Examples: i. indexOf(c);
ii. boolean equals (int a, int b)
{
if(a==b)
return true;
else
return false;
}

3. Procedural function : This type of a function performs an action and


does not return a value. Generally this type of a function is for the
display purpose. Examples :
i. System.out.println();
ii. void add(int a, int b)
{
System.out.println(a+b);
}

Actual and formal parameters :


The parameters that appear in a function call statement are actual
parameters. Example : ob.add(x,y); Here x and y are actual parameters.
They give values to formal parameters. The actual parameters are not
preceded by a data type.
4
The parameters that appear in a function definition are formal
parameters. Example : int add (int a, int b). Here a and b are formal
parameters. They are the copy of actual parameters. They are
preceded by a data type.

Note : This concept can be asked as : Differentiate between actual and


formal parameters. Then write in columns, point wise. 4 points are
given in the above description.

Call/invoke(Pass) by value, Call/invoke(Pass) by reference:


In call/invoke by value method, the values of actual parameters are
copied to formal parameters and after the copy, both, actual and
formal parameters are independent of each other. Any change made
will not reflect on the other. All primitive data types are passed by
value.
Example : ob.add(x,y); here x and y are of int data type.

In call/invoke by reference method, the reference of memory location


(address of memory) is copied from actual parameters to formal
parameters. As a result, both actual and formal parameter point to the
same memory location. Thus any change made will reflect on the other
and are dependent on each other. All non-primitive/reference data
types are passed by reference.
Example : ob.update(arr); here arr is an array variable

Note : This concept can be asked as : Differentiate between call by


value and call by reference. Then write in columns, point wise. 4 points
are given in the above description.

Q. Name the two methods of invoking a function.


A. 1. Call by value 2. Call by reference

5
Pure and impure functions
Functions that does not change the state of the received parameters
are pure functions. They are also called accessor or getter methods.
Generally, call by value functions are pure functions.

Functions that change the state of the received parameters are impure
functions. They are also called mutators or setter methods. Generally,
call by reference functions are impure functions.

Function Overloading :
More than one function in a class having the same name but differ in
signature is function overloading. It implements polymorphism.
Example:
class CalcArea
{
int area(int s)
{
return s*s;
}
double area(int l, int b)
{
return l*b;
}
double area(int b, double h)
{
return 0.5*b*h;
}
double area(double r)
{
return 3.14*r*r;
}
}

You might also like