26
C++ [ Functions, Arrays, Pointers] Eng. Ahmed Farag

Functions C++

Embed Size (px)

Citation preview

Page 1: Functions C++

C++ [ Functions, Arrays, Pointers]

Eng. Ahmed Farag

Page 2: Functions C++

C++ Functions A function is a group of statements that together perform a

task.

Every C++ program has at least one function, which is main().

A function declaration: tells the compiler about a function's name, return type, and

parameters. A function definition:

provides the actual body of the function.

Page 3: Functions C++

C++ standard library The C++ standard library provides numerous built-in

functions that your program can call.

For example, function strcat() to concatenate two strings.

Page 4: Functions C++

Defining a Function1. Return Type2. Function Name3. Parameters4. Function Body

Example:1. return_type function_name( parameter list )2. {3. body of the function4. }

Page 5: Functions C++

Defining a Function Con…1. // function returning the max between two numbers2. int max(int num1, int num2) 3. {4. // local variable declaration5. int result;6. if (num1 > num2)7. result = num1;8. else9. result = num2;10. return result; 11. }

Page 6: Functions C++

Function Declarations return_type function_name( parameter list ); Example:

1. int max(int num1, int naum2);

Parameter names are not important in function declaration only their type is required, so following is also valid declaration:

1. int max(int, int);

Page 7: Functions C++

Calling a Function1. #include <iostream>2. using namespace std;3. // function declaration4. int max(int num1, int num2);

5. int main ()6. { // local variable declaration:7. int a = 100;8. int b = 200;9. int ret;10. // calling a function to get max value.11. ret = max(a, b);12. cout << "Max value is : " << ret << endl;13. return 0;14. }

Max value is : 200

Page 8: Functions C++

Calling a Function Con… 1. // function returning the max between two numbers2. int max(int num1, int num2) 3. {4. // local variable declaration5. int result;6. if (num1 > num2)7. result = num1;8. else9. result = num2;10. return result; 11. }

Page 9: Functions C++

Calling a Function Con …

Page 10: Functions C++

Function call 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.

By default, C++ uses call by value to pass arguments.

In general, this means that code within a function

cannot alter the arguments used to call the function. 

Page 11: Functions C++

Function call by value : Example

1. // function definition to swap the values.2. void swap(int x, int y)3. {4. int temp;5. temp = x; /* save the value of x */6. x = y; /* put y into x */7. y = temp; /* put x into y */8. return;9. }

Page 12: Functions C++

Function call by value : Example

1. #include <iostream>2. using namespace std;3. // function declaration4. void swap(int x, int y);5. int main ()6. { // local variable declaration:7. int a = 100;8. int b = 200;9. cout << "Before swap, value of a :" << a << endl;10. cout << "Before swap, value of b :" << b << endl;11. // calling a function to swap the values.12. swap(a, b);13. cout << "After swap, value of a :" << a << endl;14. cout << "After swap, value of b :" << b << endl;15. return 0;16. }

Before swap, value of a :100Before swap, value of b :200After swap, value of a :100After swap, value of b :200

Page 13: Functions C++

Function 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.

Page 14: Functions C++

Function call by reference : Example1. // function definition to swap the values.2. void swap(int &x, int &y)3. {4. int temp;5. temp = x; /* save the value at address x */6. x = y; /* put y into x */7. y = temp; /* put x into y */8. 9. return;10. }

Page 15: Functions C++

Function call by reference : Example1. #include <iostream>2. using namespace std;3. // function declaration4. void swap(int &x, int &y);

5. int main ()6. {// local variable declaration:7. int a = 100;8. int b = 200;9. cout << "Before swap, value of a :" << a << endl;10. cout << "Before swap, value of b :" << b << endl;11. /* calling a function to swap the values using variable reference.*/12. swap(a, b);13. cout << "After swap, value of a :" << a << endl;14. cout << "After swap, value of b :" << b << endl;15. return 0;16. }

Before swap, value of a :100Before swap, value of b :200After swap, value of a :200After swap, value of b :100

Page 16: Functions C++

Pointers

Page 17: Functions C++

Function call by pointer The call by pointer method of passing arguments to a

function copies the address of an argument into the formal parameter.

Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument.

Page 18: Functions C++

Function call by pointer : Example

1. // function definition to swap the values.2. void swap(int *x, int *y)3. {4. int temp;5. temp = *x; /* save the value at address x */6. *x = *y; /* put y into x */7. *y = temp; /* put x into y */8. return;9. }

Page 19: Functions C++

Function call by pointer : Example

1. #include <iostream>2. using namespace std;3. // function declaration4. void swap(int *x, int *y);5. int main ()6. { // local variable declaration:7. int a = 100;8. int b = 200;9. cout << "Before swap, value of a :" << a << endl;10. cout << "Before swap, value of b :" << b << endl;11. swap(&a, &b);12. cout << "After swap, value of a :" << a << endl;13. cout << "After swap, value of b :" << b << endl;14. return 0;15. }

Before swap, value of a :100Before swap, value of b :200After swap, value of a :200After swap, value of b :100

NOTE: Calling a function to swap the values.

&a indicates pointer to a ie. address of variable a and

&b indicates pointer to b ie. address of variable b.

Page 20: Functions C++

Default Values for Parameters1. #include <iostream>2. using namespace std;3. 4. int sum(int a, int b=20)5. {6. int result;7. result = a + b;8. return (result);9. }

Page 21: Functions C++

Default Values for Parameters

1. int main ()2. {// local variable declaration:3. int a = 100;4. int b = 200;5. int result;6. // calling a function to add the values.7. result = sum(a, b);8. cout << "Total value is :" << result << endl;9. // calling a function again as follows.10. result = sum(a);11. cout << "Total value is :" << result << endl;12. return 0;13. }

Total value is :300Total value is :120

Page 22: Functions C++

Arrays file:///D:/1-%20Computer%20Science%20Matirial/Offline

%20Sites/Tutorialspoint/C++%20tutorialspoint/cpp_arrays.htm

Page 23: Functions C++

Multi-dimensional Arrays file:///D:/1-%20Computer%20Science%20Matirial/Offline

%20Sites/Tutorialspoint/C++%20tutorialspoint/cpp_multi_dimensional_arrays.htm

Page 24: Functions C++

Pointer to an Array file:///D:/1-%20Computer%20Science%20Matirial/Offline

%20Sites/Tutorialspoint/C++%20tutorialspoint/cpp_pointer_to_an_array.htm

Page 25: Functions C++

Strings file:///D:/1-%20Computer%20Science%20Matirial/Offline

%20Sites/Tutorialspoint/C++%20tutorialspoint/cpp_strings.htm

Page 26: Functions C++

References file:///D:/1-%20Computer%20Science%20Matirial/Offline

%20Sites/Tutorialspoint/C++%20tutorialspoint/cpp_references.htm