7
Computer Programming – 13CS Functions in C++ Most computer programs that solve real-world problems are much larger. Experience has shown that the best way to develop and maintain a large program is to construct it from small, simple pieces, or components. This technique is called divide and conquer. Function is a subset of a program that performs a specific task. A function groups a number of program statements into a unit and gives it a name. This unit can then be invoked/executed from other parts of the program. The most important reason to use function is to organize and divide a very large program in to small units. Another reason is to reduce the size of program. Any sequence of instructions that appears in a program more than once is a candidate for being made into a function. The function’s code is stored in only one place in memory, even though the function is executed many times in the course of the program. Functions are sometimes also called as procedures or subroutines. Creating function in C++ In order to create functions in C++ we have to define three things for every function: 1) Function Declaration 2) Function Definition 3) Function Call Function Declaration It tells the details of the function to the compiler by specifying its name, return type and parameter types. Parameters are the inputs to the function and the return type is the output of the function. Let’s consider the function named as add which takes two integer numbers as the input and returns the sum of those numbers as integer. Hence the function has return type of int and has two parameters both of them are int. By Ali Asghar, Lecturer CSE, MUET Parameter Types Return Type

Functions in C++

Embed Size (px)

DESCRIPTION

Functions in C++

Citation preview

Page 1: Functions in C++

Computer Programming – 13CS

Functions in C++

Most computer programs that solve real-world problems are much larger. Experience has shown that the best way to develop and maintain a large program is to construct it from

small, simple pieces, or components. This technique is called divide and conquer. Function is a subset of a program that performs a specific task. A function groups a number of program statements into a unit and gives it a name. This unit can then be

invoked/executed from other parts of the program. The most important reason to use function is to organize and divide a very large program in to small

units. Another reason is to reduce the size of program. Any sequence of instructions that appears in a program

more than once is a candidate for being made into a function. The function’s code is stored in only one place in memory, even though the function is executed many

times in the course of the program. Functions are sometimes also called as procedures or subroutines.

Creating function in C++

In order to create functions in C++ we have to define three things for every function:

1) Function Declaration2) Function Definition3) Function Call

Function Declaration

It tells the details of the function to the compiler by specifying its name, return type and parameter types. Parameters are the inputs to the function and the return type is the output of the function.

Let’s consider the function named as add which takes two integer numbers as the input and returns the sum of those numbers as integer. Hence the function has return type of int and has two parameters both of them are int.

int add(int,int);

Function declaration is always terminated by semicolon at the end.

By Ali Asghar, Lecturer CSE, MUET

Parameter TypesReturn Type

Function Name

Page 2: Functions in C++

Computer Programming – 13CS

Function Definition

Function definition contains the actual code of the function. It defines what the function will actually do. Consider the same add function. The definition of the function is given as under:

int add(int num1, int num2)

{

int num3;

num3 = num1 + num2;

return num3;

}

The definition consists of a line called the declarator, followed by the function body. The function body is composed of the statements that make up the function, delimited by braces. The declarator must agree with the declaration: It must use the same function name, have the same argument types in the same order (if there are arguments), and have the same return type. Notice that the declarator is not terminated by a semicolon.

Function Call

It is where we actually use the function. Function call is also called the invoking or executing the function. This is all we need to call the function: the function name, followed by parentheses. The syntax of the call is very similar to that of the declaration, except that the return type is not used. The call is terminated by a semicolon. Executing the call statement causes the function to execute; that is, control is transferred to the function, the statements in the function definition are executed, and then control returns to the statement following the function call. During the function call we pass the arguments to the function.

add(2,5);

Here the function call will return 7.

By Ali Asghar, Lecturer CSE, MUET

Function Parameters

Function Body

Function Arguments

Page 3: Functions in C++

Computer Programming – 13CS

#include <iostream>#include <conio.h>

using namespace std;

int add(int,int);

int main(){ cout<<add(6,2)<<endl; cout<<add(8,14)<<endl; cout<<add(12,-3);

getch(); return 0;}

int add(int num1, int num2){ int num3; num3 = num1 + num2; return num3;}

Create two functions named as area and circum. The area function should receive the radius of the circle as the argument return the area of the circle. The circum function should also receive the radius of the circle as the argument and returns the circumference of the circle.

#include <iostream>#include <conio.h>#include <cmath>

using namespace std;

float area(float);float circum(float);

int main(){ float radius = 10.5; cout<<"Area of circle is: "<<area(radius)<<endl; cout<<"Circumference of circle is: "<<circum(radius)<<endl;

getch(); return 0;}

float area(float radius){ return 3.14*pow(radius,2);}

float circum(float radius){ return 2*3.14*radius;}

By Ali Asghar, Lecturer CSE, MUET

Page 4: Functions in C++

Computer Programming – 13CS

Create a function that receives two numbers and prints all the odd numbers in between those two numbers.

#include <iostream>#include <conio.h>

using namespace std;

void oddNums(int,int);

int main(){ oddNums(1,20);

getch(); return 0;}

void oddNums(int num1, int num2){ for(int i = num1; i<=num2; i++) { if((i%2) != 0) cout<<i<<" "; }}

Implement the following functions in C++.

1=(a+b)2=a2+2ab+b2

2=(a−b)2=a2−2ab+b2

3=(a+b)3=a3+3a2b+3ab2+b3

4=(a−b)3=a3−3a2b+3 ab2+b3

#include <iostream>#include <conio.h>

using namespace std;

float func1(float,float);float func2(float,float);float func3(float,float);float func4(float,float);

int main(){ float a = 2.0; float b = 3.0;

By Ali Asghar, Lecturer CSE, MUET

Page 5: Functions in C++

Computer Programming – 13CS

cout<<func1(a,b)<<endl; cout<<func2(a,b)<<endl; cout<<func3(a,b)<<endl; cout<<func4(a,b)<<endl;

getch(); return 0;}

float func1(float a, float b){ return (a*a + 2*a*b + b*b);}

float func2(float a, float b){ return (a*a - 2*a*b + b*b);}

float func3(float a, float b){ return (a*a*a + 3*a*a*b + 3*a*b*b + b*b*b);}

float func4(float a, float b){ return (a*a*a - 3*a*a*b + 3*a*b*b + b*b*b);}

Write a function in C++ that receives number of bits as argument and returns the equivalent number of megabytes.

#include <iostream>#include <conio.h>

using namespace std;

float mb(int);

int main(){ cout<<mb(25415587)<<endl; cout<<mb(58485)<<endl;

getch(); return 0;}

float mb(int bits){ return (bits/(8.0*1024*1024));}

By Ali Asghar, Lecturer CSE, MUET

Page 6: Functions in C++

Computer Programming – 13CS

By Ali Asghar, Lecturer CSE, MUET