5.functions

4
Functions What you’ll learn: o Introduction to functions o Passing values to functions

Upload: hardik-gupta

Post on 20-Jul-2015

40 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 5.functions

Functions

What you’ll learn:

o Introduction to functions

o Passing values to functions

Page 2: 5.functions

What is a function?

A function is a self-contained block of statement(s)/code that performs a unified task of

some kind.

It is a re-usable code that can be used anywhere and anytime in the whole program.

Page 3: 5.functions

Syntax of a function

return_type func_name (list_of_arguments);

return_type func_name (list_of_arguments) {

…block of code…

}

func_name(values_of_arguments);

Prototype Declaration:

Definition:

Calling:

Page 4: 5.functions

Passing Arguments to Functions

int add (int a, int b) {

return a+b;

}

sum = add (i, j);

Formal Arguments

Actual Arguments

• While declaring and defining, formal arguments are used.

• While calling, actual arguments are used.