0% found this document useful (0 votes)
2 views22 pages

6th Week Prgramming-II Using C++

The document is a lecture outline for a Programming-II course using C++, covering topics such as functions, parameters, default parameters, and the return keyword. It explains how to create and call functions, the importance of parameters and arguments, and provides practice exercises for students. The lecturer is Sarullah Zahidi, and the course is scheduled for the Spring semester of the year 1404.

Uploaded by

azimrahimi003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views22 pages

6th Week Prgramming-II Using C++

The document is a lecture outline for a Programming-II course using C++, covering topics such as functions, parameters, default parameters, and the return keyword. It explains how to create and call functions, the importance of parameters and arguments, and provides practice exercises for students. The lecturer is Sarullah Zahidi, and the course is scheduled for the Spring semester of the year 1404.

Uploaded by

azimrahimi003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Programming-II using C++

Lecturer: Sarullah Zahidi


Email: sarullahzahidi@zawul.edu.af
Semester: Two
Year: 1404 Spring
Agenda
• Functions
• Functions Parameters
• Default Parameter
• Multiple Parameter
• Return Key word
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.
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 a function, specify the name of the function, followed by


parentheses ():
Call a Function
• 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 above example, myFunction() is used to print


a text (the action), when it is called:
Call a Function
• A function can be called multiple times:
Call a Function
• 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)
Call a Function
• Note: If a user-defined function, such as myFunction() is declared after the main()
function, an error will occur:
Call a Function
• However, it is possible to separate the declaration and
the definition of the function - for code optimization.

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

• When a parameter is passed to the function, it is


called an argument. So, from the example above:
fname is a parameter, while Liam, Jenny and Anja
are arguments.
C++ Default 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++ Multiple Parameters
• Inside the function, you can add as
many parameters as you want:

• Note that when you are working


with multiple parameters, the
function call must have the same
number of arguments as there are
parameters, and the arguments
must be passed in the same order.
C++ The Return Keyword
• 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++ The Return Keyword (Practice)
C++ The Return Keyword (Practice)
C++ The Return Keyword (Practice)
Practice
1. Write a function addNumbers that takes two integers and returns their sum. Call
this function from main().
2. Create a function greet that takes a string (name) as input and prints "Hello,
[name]!"
3. Write a function square that returns the square of a number. Ask the user for a
number and display the square using the function.
4. Define a function isEven that checks whether a given number is even. Return true
if it is, otherwise false.
5. Write a function maxOfThree that takes three numbers and returns the largest
one.
Practice
6. Create a function factorial that calculates the factorial of a number using a for
loop.
7. Write a program that uses a function swapValues to swap two integers using pass-
by-reference.
8. Create a function isPrime that checks if a number is prime or not. Return true or
false.
9. Write a function sumArray that takes an array and its size, and returns the sum of
all elements.
10. Write a function reverseDigits that takes an integer and returns the number with
its digits reversed (e.g., 123 → 321).
Thank You

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy