Lec8-C++ Functions PDF
Document Details
Uploaded by Deleted User
Kafrelsheikh University
Dr. Mona Hussein Alnaggar
Tags
Summary
This document contains lecture notes on C++ functions. It covers topics such as function definitions, calling, parameters, return values, overloading, and recursion. The lecture notes are from Kafrelsheikh University.
Full Transcript
Structured Programming Presented by: Dr. Mona Hussein Alnaggar 2023-2024 1st term Lecture 8 - Functions 1 C++ Functions definition C++ Functions A function is a block of code which only r...
Structured Programming Presented by: Dr. Mona Hussein Alnaggar 2023-2024 1st term Lecture 8 - Functions 1 C++ Functions definition C++ Functions A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times. C++ Functions Create a Function C++ provides some pre-defined functions, such as main() which is used to execute code. But you can also create your own functions to perform certain actions. To create (often referred to as declare) a function, specify the name of the function, followed by parentheses (): C++ Functions Example Explained myFunction() is the name of the function void means that the function does not have a return value. inside the function (the body), add code that defines what the function should do. C++ Functions calling Declared functions are not executed immediately. They are "saved for later use", and will be executed later, when they are called. To call a function, write the function's name followed by two parentheses () and a semicolon ; In the following example, myFunction() is used to print a text (the action), when it is called: C++ Functions Calling Call a Function C++ Functions Call a Function A function can be called multiple times: C++ Functions Function Declaration and Definition A C++ function consist of two parts: Declaration: the return type, the name of the function, and parameters (if any) Definition: the body of the function (code to be executed) C++ Functions Function Declaration and Definition Note: If a user-defined function, such as myFunction() is declared after the main() function, an error will occur: C++ Functions Function Declaration and Definition You will often see C++ programs that have function declaration above main() and function definition below main(). This will make the code better organized and easier to read: C++ Function Parameters Parameters and Arguments Information can be passed to functions as a parameter. Parameters act as variables inside the function. Parameters are specified after the function name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma: C++ Function Parameters Parameters and Arguments The following example has a function that takes a string called fname as parameter. When the function is called, we pass along a first name, which is used inside the function to print the full name: C++ Function Parameters Default Parameter Value You can also use a default parameter value, by using the equals sign (=). If we call the function without an argument, it uses the default value ("Norway"): C++ Function Parameters Multiple Parameters Inside the function, you can add as many parameters as you want: C++ Function Parameters Return Values The void keyword, used in the previous examples, indicates that the function should not return a value. If you want the function to return a value, you can use a data type (such as int, string, etc.) instead of void, and use the return keyword inside the function C++ Function Parameters Return Values This example returns the sum of a function with two parameters: C++ Function Parameters Return Values You can also store the result in a variable: C++ Function Parameters Pass Arrays as Function Parameters You can also pass arrays to a function: C++ Function Parameters Pass Arrays as Function Parameters Example Explained The function (myFunction) takes an array as its parameter (int myNumbers), and loops through the array elements with the for loop. When the function is called inside main(), we pass along the myNumbers array, which outputs the array elements. Note: that when you call the function, you only need to use the name of the array when passing it as an argument myFunction(myNumbers). However, the full declaration of the array is needed in the function parameter (int myNumbers). C++ Function Overloading Function Overloading With function overloading, multiple functions can have the same name with different parameters: C++ Function Overloading Function Overloading Consider the following example, which have two functions that add numbers of different type: C++ Function Overloading Function Overloading In the example below, we overload the plusFunc function to work for both int and double: C++ Recursion Recursion Recursion is the technique of making a function call itself. This technique provides a way to break complicated problems down into simple problems which are easier to solve. Recursion may be a bit difficult to understand. The best way to figure out how it works is to experiment with it. C++ Recursion Recursion Example Adding two numbers together is easy to do, but adding a range of numbers is more complicated. In the following example, recursion is used to add a range of numbers together by breaking it down into the simple task of adding two numbers: C++ Recursion Recursion Example Example Explained When the sum() function is called, it adds parameter k to the sum of all numbers smaller than k and returns the result. When k becomes 0, the function just returns 0. When running, the program follows these steps: