Chapter 1 Function (1)
Chapter 1 Function (1)
1. Functions
4.1. What is a function?
♣ Functions are the main part of top-down structured programming. Therefore, a
function is a block of code designed to tackle a specific problem.
♣ A function is a set of statements that take inputs, do some specific
computation, and produce output.
♣ The idea is to put some commonly or repeatedly done tasks together and make
a function so that instead of writing the same code again and again for different
inputs, we can call the function.
♣ In simple terms, a function is a block of code that only runs when it is called.
Advantages of Function
Creating functions in a program is beneficial in:
♣ Parameter names are not important in function declaration only their type is
required, so following is also valid declaration
Example int max(int, int);
Example function declaration cont…
void display(char);
/*function name = display, receives a character as
argument and returns nothing*/
int sum(int,int);
/*function name = sum, receives two integers
as argument and returns an integer*/
function declaration Cont…
The function prototype is the template of the function which tells the details
of the function.
Some examples of acceptable and not acceptable prototypes are shown
below:
♣ int sum(int a, int b); //Acceptable
#include <iostream>
using namespace std;
int g; // Global variable declaration:
int main () {
int a, b; // Local variable declaration:
a = 10; // actual initialization
b = 20;
g = a + b;
cout << g;
return 0;
}
Function Parameters and arguments
♣ The difference between parameters and arguments:
♣ Function parameters are the names listed in the function's definition.
♣ The parameter is referred to as the variables that are defined during a function
declaration or definition.
♣ These variables are used to receive the arguments that are passed during a function
call.
♣ Function arguments are the real values passed to the function.
♣ Parameter is the variable in the declaration of the function.
♣ An Argument is referred to the values that are passed within a function when the function
called.
Function Parameters and arguments cont…
♣ Parameter → placeholder (This means a placeholder belongs to the function
naming and be used in the function body)
♣ Argument → actual value (This means an actual value which is passed by
the function calling)
A parameter is a variable in a function definition. It is a placeholder and
hence does not have a concrete value.
An argument is a value passed during function invocation.
Function Parameters and arguments example
#include <iostream>
using namespace std;
void introduction(string name, int age) // Here name and age are parameters of
the introduction function
{
cout << "Hello! My name is " << name << " and I am " << age <<" years
old."<< endl;
}
int main() {
// Here Jerry and 15 are arguments passed to the introduction function
introduction("Jerry", 15);
return 0;
}
Function Parameters and arguments example
#include <iostream>
using namespace std; Output
// sum: Function definition The summation is 30
int sum(int a, int b)
// returning the addition
return a + b;
}
int main()
{
int num1 = 10, num2 = 20, res;
// sum() is called with
// num1 & num2 as ARGUMENTS.
res = sum(num1, num2);
// Displaying the result
cout << "The summation is " << res;
return 0;
}
Function Parameters and arguments
Passing parameters reading assignment
The all local variables which are defined within the function are