0% found this document useful (0 votes)
2 views3 pages

Functions

The document provides an overview of function syntax, including return types, function names, and parameters in programming. It explains parameter passing, specifically 'pass by value', where actual parameters are copied to formal parameters without affecting the original values. Examples are included to illustrate how to define and call functions in C++.

Uploaded by

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

Functions

The document provides an overview of function syntax, including return types, function names, and parameters in programming. It explains parameter passing, specifically 'pass by value', where actual parameters are copied to formal parameters without affecting the original values. Examples are included to illustrate how to define and call functions in C++.

Uploaded by

neha.ijaz
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

FUNCTIONS

SYNTAX:

Returntype function name(parameters)

Statement(s);

Return type: data type of value returned by the function

Function name: name of function

Parameters: values given to function when a function is called.

e.g.

1. int add(int a, int b)

2. void line(char c)

3. int subtract(int a, float b, int c)


Example:

Write a program that shows “Programming is easy” on screen using functions.

#include <iostream>

using namespace std;

void show()

cout <<"programming is easy”;

int main()

show();

return 0;

}
Parameter Passing

Parameters are the values that are provided to a function when the function is called. Parameters
are given in the parentheses. If there are many parameters, these are separated by commas. If
there is no parameter, empty parentheses are used. Both variables and constants can be passed to
a function as parameters.

The sequence and types of parameters in function call must be similar to the sequence and types
of parameters in function declaration.

 Parameters in function call are called actual parameters.

 Parameters in function declaration are called formal parameters.

When a function call is executed, the values of actual parameters are copied to formal parameters.

9.4.1 Pass by Value

A parameter passing mechanism in which the value of actual parameter is copied to formal
parameters of called function is known as pass by value. If the function makes any change in
formal parameter, it does not affect the values of actual parameter. It is the default mechanism for
passing parameters to functions.

#include <iostream>

using namespace std;

void show(int num)

cout << "The number is " << num;

int main()

int n;

cout << "Enter number: ";

cin >> n;

show(n);

cout << "End of Program";

return 0;

You might also like