0% found this document useful (0 votes)
20 views17 pages

Chapter 7 C++

Chapter seven discusses functions in C++, explaining their definition, declaration, and types, including user-defined and built-in functions. It covers how to call functions, the differences between call by value and call by reference, variable scope, return values, function overloading, and recursion. The chapter emphasizes the importance of functions as reusable code blocks that can simplify programming tasks.

Uploaded by

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

Chapter 7 C++

Chapter seven discusses functions in C++, explaining their definition, declaration, and types, including user-defined and built-in functions. It covers how to call functions, the differences between call by value and call by reference, variable scope, return values, function overloading, and recursion. The chapter emphasizes the importance of functions as reusable code blocks that can simplify programming tasks.

Uploaded by

Abera birhanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Chapter seven

Function
FUNction in C++ 1
Introduction to Function
• A function is a group of statements that together perform a task. A function is a
subprogram that can act on data and return a value.
• Each function has its name, and when that name is encountered, the execution of the
program branches to the body of that function.
• When the function returns, execution resumes on the next line of the calling function.
• When a program calls a function, execution switches to the function and then resumes at
the line
after the function call.
• Functions come in two varieties: user-defined and built-in.
FUNction in C++ 2
Function declaration
• A function declaration tells the compiler about a function's name, return
type, and parameters.
• int addition( int x, int y);
• int = return type
• addition = function name
• x and y are parameters of the function having a type of integer

FUNction in C++ 3
Function definition
• The definition of a function consists of the function header and its body. The
header is exactly like the function prototype, except that the parameters must be
named, and there is no terminating semicolon.
• The body of the function is a set of statements enclosed in braces.
• A function definition must agree in the return type and parameter list with its
prototype.
return_type function_name (parameter list)
{
body of the function
…….
……….
}

FUNction in C++ 4
User-defined functions
User-defined functions are the functions that are created by the programmer so that the
programmer can use them many times.
• #include <iostream> cin>>y;
#include <cmath>
cout<<"Sum of these two :"<<sum(x,y);
using namespace std;
return 0;
//Declaring the function sum
}
int sum(int a,int b);
//Defining the function sum
int main(){
int sum(int a, int b) {
int x, y;
int c = a+b;
cout<<"enter first number: ";
return c;
cin>> x;
}
cout<<"enter second number: ";
FUNction in C++ 5
Built-in functions
• Built-in functions are part of your compiler package--they are supplied by the manufacturer
for your use.
• which are declared in the C++ header files such as pow(x,y), cmath, ceil(x), cos(x), exp(x),
etc.
• We need not declare and define these functions as they are already written in the C++
libraries
• #include <iostream>
#include <cmath>
using namespace std;
int main(){
cout<<pow(2,5);
return 0;
FUNction in C++ 6
}
Calling a Function
• While creating a C++ function, you define what the function has to do. To use a function, you will
have to call or invoke that function.
• When a program calls a function, program control is transferred to the called function.
• A called function performs the defined task and when its return statement is executed or when its
function-ending closing brace is reached, it returns program control to the main program.

• int main ()
{
// local variable declaration: • int b = 200;
int a = 100; int ret;
// calling a function to get max value.
ret = max(a, b);
cout << "Max value is : " << ret << endl;
return 0;
}

FUNction in C++ 7
Calling by value
• The call by value method of passing arguments to a function copies the actual value of an
argument into the formal parameter of the function.
• In this case, changes made to the parameter inside the function do not affect the
argument

• Code within a function cannot alter the arguments used to call the function
void swap(int x, int y)
{
int temp; temp = x; /* savethe value of x */
x = y; /* put y into x */ y = temp; /* put x into y */
return;
}
FUNction in C++ 8
# include <iostream> //output of the function
using namespace std; Before swap, the value of a:100
// function declaration Before swap, the value of b:200
void swap(int x, int y); After the swap, the value of a:100
int main () After the swap, the value of b:200
{
• variable declaration:
// local
int a = 100;
int b = 200;
cout << "Before the swap, the value of a:" << a << endl;
cout << "Before the swap, the value of b:" << b << endl;
// calling a function to swap the values.
swap(a, b);
cout << "After the swap, value of a:" << a << endl;
cout << "After the swap, value of b:" << b << endl;
return 0;
}

FUNction in C++ 9
Call by Reference
• The call by reference method of passing arguments to a function copies the reference of an argument into
the formal parameter. Inside the function, the reference is used to access the actual argument used in the
call.
• This means that changes made to the parameter affect the passed argument
• To pass the value by reference, argument reference is passed to the functions just like any other value.
• The function parameters declare as reference types
// function definition to swap the values.
void swap(int &x, int &y)
{
int temp;
temp = x; /* save the value at address x */
x = y; /* put y into x */
y = temp; /* put x into y */
return;
}
FUNction in C++ 10
• <iostream>
#include //output of the function
using namespace std; Before swap, the value of a:100
// function declaration Before swap, the value of b:200
void swap(int &x, int &y); After the swap, the value of a:200
int main () After the swap, the value of b:100
{
// local variable declaration:
int a = 100;
int b = 200;
cout << "Before swap, the value of a:" << a << endl;
cout << "Before swap, the value of b:" << b << endl;
/* calling a function to swap the values using variable
reference.*/
swap(a, b);
cout << "After the swap, the value of a:" << a << endl;
cout << "After the swap, the value of b:" << b << endl;
return 0;
}

FUNction in C++ 11
Scope of variables
• A variable has scope, which determines how long it is available to your program and where it can be accessed. There
are two scopes Local and Global.
• Local variables: are defined as any other variables. The parameters passed into the function are also considered
local variables and can be used exactly as if they had been defined within the body of the function.
• Variables declared within the function are said to have "local scope." That means that they are visible and usable only
within the function in which they are defined.
• Global variables: have global scope and are available anywhere within your program.
• Variables defined outside of any function have global scope and thus are available from any function in the program,
including main().
• If a function has a variable with the same name as a global variable, the name refers to the local variable--not the
global—when used within the function.
Note: The main difference between them is that global variables can be accessed globally in the
entire program, whereas the local variables can be accessed only within the function or block in
which they are declared.
FUNction in C++ 12
#include <iostream.h>
2: void myFunction(); // prototype
3:•int x = 5, y = 7; // global variables utput: x from main: 5
5: int main() y from main: 7
6: { x from myFunction: 5
7: cout << "x from main: " << x << "\n"; y from myFunction: 10

9: cout << "y from main: " << y << "\n\n"; Back from myFunction!
10: myFunction(); x from main: 5
11: cout << "Back from myFunction!\n\n"; y from main: 7
12: cout << "x from main: " << x << "\n";
13: cout << "y from main: " << y << "\n";
14: return 0;
15: }
16: void myFunction()
18: {
19: int y = 10;
20: cout << "x from myFunction: " << x << "\n";
22: cout << "y from myFunction: " << y << "\n\n";
23: } FUNction in C++ 13
Return Values
• Functions return a value or return void.
• A void is a signal to the compiler that no value will be returned.
• To return a value from a function, write the keyword return followed by the value
you want to return.
• return 5;
• return (x > 5);
• return (MyFunction());

FUNction in C++ 14
Function Overloading
• C++ have more than one function with the same name.
• Functions with the same name are called overloaded functions.
C++ requires that each overloaded function differs in its argument
list.
• Overloaded functions enable you to have similar functions that
work on different types of data.

FUNction in C++ 15
Recursion
• A function that calls itself is said to be recursive. Recursion is a general programming
technique applicable to problems that can be defined in terms of themselves.
• A recursive function must have at least one termination condition which can be satisfied.
Otherwise, the function will call itself indefinitely until the runtime stack overflows.
• The three necessary components in a recursive method are:
• A test to stop or continue the recursion
• An end case that terminates the recursion
• A recursive call(s) that continues the recursion

FUNction in C++ 16
Thank you!!!

FUNction in C++ 17

You might also like