0% found this document useful (0 votes)
103 views15 pages

C++ Function Basics for Beginners

The document discusses different types of functions in C++, including predefined and user-defined functions. It covers value-returning functions and void functions. Value-returning functions specify a return type and return a value, while void functions do not return a value. The document explains the syntax for defining and calling functions, and how value and reference parameters work. It also discusses function prototypes, return statements, and how to use value-returning functions in expressions and assignments.

Uploaded by

Tarek Srour
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views15 pages

C++ Function Basics for Beginners

The document discusses different types of functions in C++, including predefined and user-defined functions. It covers value-returning functions and void functions. Value-returning functions specify a return type and return a value, while void functions do not return a value. The document explains the syntax for defining and calling functions, and how value and reference parameters work. It also discusses function prototypes, return statements, and how to use value-returning functions in expressions and assignments.

Uploaded by

Tarek Srour
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Value returned type And Void Function

LU_2011 Dr Rima ABDALLAH CHEHADE

Functions
Functions
Called modules Like miniature programs Can be put together to form a larger program Two types: Predefined and User defined function
LU_2011 Dr Rima ABDALLAH CHEHADE 2

Functions (continued)
Properties that form the function definition:
1. Name of the function 2. Number of parameters 3. Data type of each parameter 4. Type of the function 5. Code required to accomplish the task (the body of the function)

For predefined functions, you need to be concerned only with the first four properties For user defined functions, you need to be concerned with the five properties.
LU_2011 Dr Rima ABDALLAH CHEHADE 3

Predefined Functions
Predefined functions are organized into separate libraries Some of the predefined mathematical functions are:
sqrt(x) pow(x,y)

I/O functions are in iostream/ stdio header Math functions are in cmath header

LU_2011 Dr Rima ABDALLAH CHEHADE

The Power Function (pow)


pow(x,y) calculates xy, pow(2,3) = 8.0 p ( y) p ( ) pow returns a value of the type double x and y are called the parameters (or arguments) of the function pow Function pow has two parameters

LU_2011 Dr Rima ABDALLAH CHEHADE

The sqrt Functions


The square root function sqrt(x)
Calculates the non-negative square root of x, for x >= 0.0 sqrt(2.25) is 1.5 Type double (returned type) and has only one parameter

LU_2011 Dr Rima ABDALLAH CHEHADE

User-Defined Functions
Void functions: do not have a data type Value-returning functions: have a data type

LU_2011 Dr Rima ABDALLAH CHEHADE

Value-Returning Functions
Definition of the function : Five properties Heading or function header: first four properties above Formal Parameter: variable declared in the heading Body: the fifth property Ex: double larger (double x, double y) { if (x >= y) return x; else return y; } printf(" The larger is %f ", larger(3,6)); // to call the function in main or others fns
LU_2011 Dr Rima ABDALLAH CHEHADE 8

Syntax of Value-Returning Functions


The syntax is: y
functionType functionName(formal parameter list) { statements //declarations and/or executable statements }

functionType: type of the value returned by the function


Also called the data type or the return type

Body of function in between curly braces


LU_2011 Dr Rima ABDALLAH CHEHADE 9

Syntax of Value-Returning Functions


The syntax of the formal p y parameter list is:
dataType identifier, dataType identifier, ...

The syntax for a function call is:


functionName(actual parameter list)

Th syntax for the actual parameter list is: The t f th t l t li t i


expression or variable,expression or variable, ...

LU_2011 Dr Rima ABDALLAH CHEHADE

10

Value-Returning Functions
double larger (double x, double y) { if (x >= y) return x; else return y; } cout<< " The larger is " <<larger(2,3) // call larger in main printf(" The larger is %f ", larger(3,6));
LU_2011 Dr Rima ABDALLAH CHEHADE 11

Value-Returning Functions (continued)


To use a function in your program, y can y p g you call it one or several times. When a function is called, its parameters are called actual parameters. A t l Parameter: variable or expression Actual P t i bl i listed in a call to a function

LU_2011 Dr Rima ABDALLAH CHEHADE

12

Parameters & Memory Allocation


When a function is called
Memory for its formal parameters and variables declared in the body of the function (called local variables) is allocated in the function data area The value of the actual parameter is copied into the memory cell of its corresponding formal parameter (value parameter)

LU_2011 Dr Rima ABDALLAH CHEHADE

13

Function Prototype
In C++ programmers customarily place the function main before all other user-defined functions. It produces compilation errors.

Solution: place the function prototype of a value returning function before any function definition including main. Function Prototype: function heading without the body of the function The syntax is:
functionType functionName (formal parameter list); double larger(double x, d bl y); d bl l (d bl double )

It is not necessary to specify the variable name in the parameter list but you must specify the data type of each parameter
double larger (double, double);
LU_2011 Dr Rima ABDALLAH CHEHADE 14

Flow of Execution (Continued)


Execution always begins at y g
The first statement in the function main no matter where main is placed in the program

Other functions are executed only when they are called

LU_2011 Dr Rima ABDALLAH CHEHADE

15

Flow of Execution (continued)


A value-returning function must returns a value l A function call in a program results in the execution of the body of the called function After executing the function
The value that the function returns replaces the function call statement
LU_2011 Dr Rima ABDALLAH CHEHADE 16

The return Statement


Once the function computes the value, the function returns the value via the return statement The syntax of the return statement is:
return expression, constant value, or variable; *largest.cpp
The data type of the value that expression computes( in the return) must match the function type.

When a return statement executes


All subsequent statements are skipped

Function immediately terminates Control goes back to the caller Return statement returns only one value If the return statement contains more than one expression, only the value of the last expression is returned. When a return statement executes in the function main, the program terminates
*returnStatement.cpp

LU_2011 Dr Rima ABDALLAH CHEHADE

17

Value-Returning Functions
Because the value returned by a valuereturning function is unique, we must: t i f ti i i t
Save the value for further calculation Use the value in some calculation Print the value

A value-returning function is used in an assignment , in an output statement, as a parameter in a function call


LU_2011 Dr Rima ABDALLAH CHEHADE 18

Value-Returning Functions (continued)


A value-returning function is called: In an expression in the main which may be part of an assignment statement or an output statement : y=10; z= 2+y; x= 5+ larger(y,z) ; printf(Larger is %d \n, larger(y,z)); As a parameter in a function call as in compareThree: larger(x larger(y z)) larger(x, larger(y,z)) Save the value for later calculation:x= larger(y,z) Note:To execute a function call, the parameters are evaluated first.
LU_2011 Dr Rima ABDALLAH CHEHADE 19

Value-Returning Functions
To call a value-returning function:
Use its name, with the actual parameters (if any) in parentheses In a function call, you specify only the actual p parameter, not its data type. , yp There is a one-to-one correspondence between actual and formal parameters
LU_2011 Dr Rima ABDALLAH CHEHADE 20

10

void Functions
void functions and value-returning functions g have similar structures
Both have a heading part and a statement part User-defined void functions can be placed either before or after the function main If after main main. main, the function prototype must be placed before the function main

LU_2011 Dr Rima ABDALLAH CHEHADE 21

void Functions (continued)


A void function does not have a data type yp The return statement without any value is typically used to exit the function early Formal parameters are optional A call to a void function is a stand-alone statement

LU_2011 Dr Rima ABDALLAH CHEHADE

22

11

void Functions Without Parameters


Function definition syntax: y
void functionName() { statements }

void is a reserved word Function call syntax:


functionName();

LU_2011 Dr Rima ABDALLAH CHEHADE

23

void Functions With Parameters


Function definition syntax: y
void functionName(formal parameter list) { statements }

Function call syntax:


functionName(actual parameter list);
LU_2011 Dr Rima ABDALLAH CHEHADE 24

12

void Functions With value Parameters (continued)


A formal parameter ( p (value p parameter) ) receives a copy of the content of corresponding actual parameter Reference Parameter - a formal parameter that receives the location (memory address) of the corresponding actual parameter f th di t l t

LU_2011 Dr Rima ABDALLAH CHEHADE

25

Value Parameters
If a formal parameter is a value p p parameter
The value of the corresponding actual parameter is copied into it

The value parameter has its own copy of the data During program execution
The value parameter manipulates the data stored in its own memory space
LU_2011 Dr Rima ABDALLAH CHEHADE 26

13

Reference Variables as Parameters


If a formal parameter is a reference p parameter
It receives the address of the corresponding actual parameter

A reference parameter stores the address of the corresponding actual parameter

LU_2011 Dr Rima ABDALLAH CHEHADE

27

Reference Variables as Parameters (continued)


During program execution to manipulate the data
The address stored in the reference parameter directs it to the memory space of the corresponding actual parameter

During execution, changes made by the formal parameter permanently change the value of the actual parameter
LU_2011 Dr Rima ABDALLAH CHEHADE 28

14

Reference Variables as Parameters (continued)


Reference parameters are useful in three p situations:
Returning more than one value Changing the actual parameter When passing the address would save memory space and time
LU_2011 Dr Rima ABDALLAH CHEHADE 29

Reference parameters and value returning function


You can also use reference parameters in a p value returning function. By definition a value return function returns a single value; this value is returned via the return statement. If a function needs to return more than one value, you should change it to a void function and use the appropriate reference parameters to return the values.
LU_2011 Dr Rima ABDALLAH CHEHADE 30

15

You might also like