Functions C++

Preview:

Citation preview

C++ [ Functions, Arrays, Pointers]

Eng. Ahmed Farag

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.

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.

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

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

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

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);

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

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

Calling a Function Con …

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. 

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

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

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.

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

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

Pointers

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.

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

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.

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

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

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

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

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

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

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

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

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

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

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

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

Recommended