0% found this document useful (0 votes)
22 views13 pages

W6L2-User Defined Functions

Copyright
© © All Rights Reserved
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)
22 views13 pages

W6L2-User Defined Functions

Copyright
© © All Rights Reserved
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/ 13

Fundamentals of Computer

Programming
(CS-110)
Course Instructors:
Dr. Momina Moetesum
Ms. Shakeela Bibi

1
Generally, a C/C++ function has three parts:
• Function Prototype
Syntax of Functions • Function Definition
in C/C++ • Function Call

2
Function Prototype (Declaration)

• In a function declaration, we must provide the


• function name,
• its return type, and
• the number and type of its parameters.
• A function declaration tells the compiler that
there is a function with the given name defined
somewhere else in the program.
• Parameter name is optional in most cases
• Function prototype must end with a semicolon

3
• return_type: It is the data type of the value
Function that the function returns. It can be any data
type int, float, void, etc. If the function does not
Prototype return anything, void is used as the return type.
• function_name: It is the identifier of the
function. Use appropriate names for the
functions that specify the purpose of the
function.
• parameter_list: It is the list of parameters that
a function expects in parentheses. A parameter
consists of its data type and name. If we don’t
want to pass any parameter, we can leave the
parentheses empty.

4
Function Return Type

• Function return type tells what type of value is returned after all
function is executed. When we don’t want to return a value, we can
use the void data type.
• Example: int func(parameter_1,parameter_2);
• The above function will return an integer value after running
statements inside the function.
• Note: Only one value can be returned from a C/C++ function. To
return multiple values, we have to use pointers or structures.

5
Function Parameters or Arguments
• Function Arguments (also known as
Function Parameters in some cases) are
the data that is passed to a function.

6
In C/C++ programming language,
functions can be called either with or
without arguments and may or might not
return values.
• Function with no arguments and no
return value
• Function with no arguments and with
return value
• Function with argument and with no
return value
• Function with arguments and with return
value

7
Function Definition
return_type function_name (para1_type para1_name, para2_type para2_name)
{
// body of the function
}

• The function definition


consists of actual statements
which are executed when the
function is called (i.e. when
the program control comes to
the function).
• It consists of the declarator
followed by the function body.

8
Function Call
• A function call is a statement that instructs the
compiler to execute the function. We use the
function name and parameters in the function
call.
• In the below example, the first sum function is
called and 10,30 are passed to the sum
function.
• After the function call sum of a and b is
returned and control is also returned back to
the main function of the program.
• Types:
• Call by Value
• Call by Reference (we will study later)

9
Examples
Examples
Examples
Acknowledgment

• Content of these slides are taken from:


• https://www.geeksforgeeks.org/
• https://www.trytoprogram.com/cplusplus
-programming/functions/
• https://www.tutorialspoint.com/
• https://www.programiz.com/

13

You might also like