Sunteți pe pagina 1din 5

C++ FUNCTION

An Assignment
Submitted to
DAUD T. POLAO
Faculty, Department of Information Technology
RC- Al Khwarizmi International College Foundation Inc.
Marawi City

In partial fulfillment
Of the Requirements for the Course
IT21 (Fundamentals of Programming and Data Base)

Submitted By

ALMERAH H. BASER

August 2019
FUNCTIONS AND ITS STRUCTURES

 What is function?
It is a group of statements that together performs a task. Every C++ program has at
least one function, which is main(), and all the most trivial programs can define
additional functions.
A function declaration tells the compiler about a function’s name, return type, and
parameters.

 Structure
C++ Program to assign data to members of a structure variable and display it. Here
a structure P erson is declared which has three members name, age and salary.
Inside main() function, a structure variable p1 is defined. You can divide up your
code into separate functions. How you divide up your code among different
functions is up to you, but logically the division usually is so each function performs
a specific task.

 Example
#include <iostream>
using namespace std;

sum(int num1, int num2)


int num3 = num1+num2

Int main()

Cout<< sum(1, 99);

Return 0;

Output
100
Two types of in C++:
 Library functions
 User-defined functions

 What is library function?


These are functions that are already defined and declared in C library, you don’t
need to define or declare them or are inbuilt functions which are grouped together
and placed in a common place we call library. Each library function in C++
performs specific operation. We can make use of these library functions to get the
pre-defined output instead of writing our own code to get those outputs.

 Structure
Library functions include standard input/output (stdio.h), string manipulation
(string.h), math functions (math.h), and date and time functions (time.h). We
learned that the functions for stdio.h include the following: printf() is output to the
screen.

 Examples
Example 1:

#include <iostream>
#include <cmath>

Using namespace std;


Int main()
{
double number, square_root;
cout <<”Enter a number:”;
cin >> number;

square_root = sqrt(number);
cout <<”Square root of “ << number << “ = “ << square_root;

return 0;
}

Output

Enter a number: 26
Square root of 26 = 5.09902
Example 2:
#include <stdio.h>
#include <math.h>

int main()

{
float num, root;
printf (“Enter a number: “);
Spscanf (“%f”, &num);

root = sqrt (num)


printf (“Square root of %.2f = %.2f” , num, root);

return o;

Output
Enter a number: 12
Square root of 12.00 = 3.46

 What is user define function?


User Define Functions (UDF) are those functions which are defined and declared
by the programmer to do some specific task. By using these functions, you can
divide your large program into small pieces according to different task which will
provide you easy access to read and debug. You can aslo re-use your code and you
do not need to rewrite the code, just call the function and use it. And you can
modularize your code.
When the function is invoked from any part of program, it all executes the codes
defined in the body of function.

 Structure
It is a block of code that performs a specific task. In which C allows you to define
functions according to your needs.
 Examples
Example 1:
#include <iostream>
using namespace std;

int add(int, int);

int main()
{
Iint num 1, num 2, sum;
cout<< ”Enter two number to add:”;
cin >> num 1 >> num 2;

sum = add(num 1, num 2);


cout<< “sum = “ << sum;
}

int add(int a, int b)


{
int add;
add = a + b;

return add;
}

Output
Enters two integers: 8 – 4
Sum = 4

Example 2:
#include <stdio.h>
int add_numbers (int a, int b);

int main()
{

int n1, n2, sum;

printf( “Enters two numbers: “);


scanf (“%d %d”, &n1, &n2);

sum = add_numbers(n1, n2);


printf( “sum = %d” , sum);

return 0;
}

int add_numbers(int a, int b)


{
int result;
result = a + b;
return result;
}

S-ar putea să vă placă și