Understanding Functions in C++: Library Functions vs. User-Defined Functions, Study Guides, Projects, Research of Computer Science

An overview of functions in C++ programming, explaining the difference between library functions and user-defined functions. It includes examples and code snippets for both types of functions, as well as information on function prototypes and calls. This resource is useful for computer science students and developers who want to deepen their understanding of C++ functions.

Typology: Study Guides, Projects, Research

2019/2020

Uploaded on 05/23/2020

yosif-alasadi
yosif-alasadi 🇮🇶

2 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
University of Technology
Control and systems Eng dep.
A research forwarded from the student:
فسوي دممح ماسويلع سمحن
About Functions in c++
لاــــــــ يف لاودلــغــــ ةســــ++ ي
Branch: Mechatronics عرفلا: يم ورتاكسكن
study: morning ةساردلا: يحابص
stage: first ةلحرملا: ىلولأا
date 4/5/2019
pf3
pf4
pf5
pf8

Partial preview of the text

Download Understanding Functions in C++: Library Functions vs. User-Defined Functions and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

University of Technology

Control and systems Eng dep.

A research forwarded from the student:

About Functions in c++

Branch: Mechatronics نكسكاترومي :الفرع

study: morning صباحي :الدراسة

stage: first األولى:المرحلة

date 4 / 5 /

We often use functions to a arrange the code groups to perform a

specific task.

Depending on whether a function is predefined or created by

programmer; there are two types of function:

  1. Library Function
  2. User-defined Function Library Functions: Library functions are the built-in function in C++ programming. Programmers can use library, and then call the function directly; they don't need to write it themselves. Example 1: Library Function #include <iostream.h> #include <math.h> int main() { double number, squareRoot; cout << "Enter a number: "; cin >> number; // sqrt() تعرف الجذر التربيعي مخزونة في المكتبة مسبقا دالة squareRoot = sqrt(number); cout << "Square root of " << number << " = " << squareRoot; return 0 ; }

It happens like the figure shows Consider the figure above.

When the program is in the main() function, the system starts executing codes

from main() function.

When the program reaches to function_name() inside main(), it moves to void

function_name() and all codes inside void function_name() is executed.

Then, control of the program moves back to the main function where the code after the call to the function_name() is executed as shown in figure above. Example 2: User Defined Function C++ program to add two integers. Make a function add() to add integers and display sum in main() function. #include <iostream.h> using namespace std; // تعريف عناصر الدالة int add(int, int);

int main() { int num1, num2, sum; cout<<"Enters two numbers to add: "; cin >> num1 >> num2; // استدعاء الدالة sum = add(num1, num2); cout << "Sum = " << sum; return 0 ; } // تعريف الدالة int add(int a, int b) { int add; add = a + b; // االرجاع return add; } Output Enters two integers: 8

  • 4 Sum = 4

In the above program, add(num1,num2); inside main() function calls the

user-defined function.

The function returns an integer which is stored in variable add.

Function Definition

The function itself is referred as function definition. Function

definition in the above program is:

// Function definition

int add (int a, int b);

int add;

add = a + b;

return add;

When the function is called, control is transferred to the first

statement of the function body.

Then, other statements in function body are executed sequentially.

When all codes inside function definition is executed, control of

program moves to the calling program.

المصادر: www.programiz.com, "++ ي " موسوعة البر مجة بلغة سc++ book