Learning C++ - Functions in C++ 3

18
Functions in C++ 1

Transcript of Learning C++ - Functions in C++ 3

Page 1: Learning C++ - Functions  in C++ 3

1

Functions in C++

Page 2: Learning C++ - Functions  in C++ 3

2

Section Outline

• Definition of a function• Calling function• Recursive functions

Page 3: Learning C++ - Functions  in C++ 3

3

Section Outline

• Definition of a function• Calling function• Recursive functions

Page 4: Learning C++ - Functions  in C++ 3

4

Definition of a function

• Every where in C++ environment we can define a function

OutPutType name (argument1, argument2, …){

………………………………………………………………………………

return output}

This two type must have a same type

Page 5: Learning C++ - Functions  in C++ 3

5

Definition of a function(cont.)

• Some note:Functions in C++ works the same as SUBROUTINsThere is no need to use CONTAINS or any similar

keywords to introduce functions to the programThe main() part of our code must know the

presence of our functions and this possible by a predefine or define our function the top of main() (disobey of this rule make a error)

Page 6: Learning C++ - Functions  in C++ 3

6

Definition of a function(cont.)

• e.g. (1) : predefineint MyFuntioin(int);int main() {int t = 12;int a = MyFunction(t);return 0;}int MyFunction(int q){……..……..……..return f;}

• e.g. (2): defineint MyFunction(int q){

……..……..……..

return f;}int main() {

int t = 12;int a = MyFunction(t);

return 0;}

Page 7: Learning C++ - Functions  in C++ 3

7

Definition of a function(cont.)

• Some notes: We can use function`s output like a statement or a variable

• Anywhere in our code, we can define a function such as MyFunction () that it`s output is an integer. after that we write the following command to use the function: int a = MyFunction();

e.g.int sub (int a, int b) { return a-b; }void main() { int a = sub (8,2); return; }

Note: There is no need to use CALL or such key word to call a function, we just need to write function’s name.

Page 8: Learning C++ - Functions  in C++ 3

8

Definition of a function(cont.)

• Some notes(cont.):If you want to write your own functions into the

other files or headers (instead of the main file), you must include them just like libraries with “..”:

e.g:#include “myfunctions.h”

Page 9: Learning C++ - Functions  in C++ 3

9

Definition of a function(cont.)

• e.g.(.h and .cpp)# include “sub.h”void main()

{int a = sub(7,2);cout << a;

}Note : we use “…” in define include when we create

a header ourselves

//sub.hint sub (int , int );

//sub.cpp# include ”sub.h”int sub (int a, int b){ return a-b;}

Page 10: Learning C++ - Functions  in C++ 3

10

Section Outline

• Definition of a function• Calling Function• Recursive functions

Page 11: Learning C++ - Functions  in C++ 3

11

Calling Function

• Passing parameter– e.g. int sub (int a, int b, int c)

{ return b-c; }void main (){ cout <<sub(e,g,f); return 0; }

– Arrays• Because of array`s nature as pointer, we pass an array as a pointer • Upper dimensions than 1D must introduce to the list of argument (we can introduce all dimensions to the function)

Page 12: Learning C++ - Functions  in C++ 3

12

Calling Function(cont.)

• e.g. int array _based _function ( int * first , int second[][5]) {…………………….} int main () {

int a[100], b[100][5];return array _based _function (a, b);

}

int first[] int first[100]

Page 13: Learning C++ - Functions  in C++ 3

13

Calling Function(cont.)

• We have two types of calling function1. Calling by value2. Calling by references

Page 14: Learning C++ - Functions  in C++ 3

14

Calling Function(cont.)

1. Calling by valuee.g. int cubeValue(int n){ return n*n*n;}int main(){int number = 5;number = cubeValue(5);return 0;}

int cubeValue ( int n){

int f = n*n*n;return f;

}

Page 15: Learning C++ - Functions  in C++ 3

15

Calling Function(cont.)

2. Calling by references e.g.

void cubeValue(int * n){ *n = *n * *n * *n;}int main(){int number = 5;number = cubeValue(& number);return 0;}

Page 16: Learning C++ - Functions  in C++ 3

16

Some Important Functions

• List of some computational functions:• Note: You must include the math library in order to use them.

#include <math>

Mathematical def. C++ Fortran

sqrt(x) SQRT(X)

exp(x) exp(x) EXP(X)

ln(x) ln(x) LOG(X)

log10(x) LOG10(X)

sin(x) sin(x) SIN(X)

cos(x) cos(x) COS(X)

tan(x) tan(x) TAN(X)

Residual x % y MOD(X,Y)

x

10logx

Page 17: Learning C++ - Functions  in C++ 3

17

Some Important Functions

#include <math>

Mathematical def. C++ Fortran

sinh(x) sinh(x) SINH(X)

cosh(x) cosh(x) COSH(X)

tanh(x) tanh(x) TANH(X)

sin-1(x) asin(x) ASIN(X)

cos-1(x) acos(x) ACOS(X)

tan-1(x) atan(x) ATAN(X)

|x| labs(x) ABS(X)

[x] floor(x) INT(X)

Page 18: Learning C++ - Functions  in C++ 3

18

In future :

i. Class and related conceptsii. ERRORS