0% found this document useful (0 votes)
37 views

CS-323 Programming Fundamentals 4 (3-2) CS-323 Programming Fundamentals 4 (3-2)

This document discusses functions in C++ programming. It defines what functions are, how they are declared and defined, and how values are passed to and returned from functions. It provides examples of function prototypes, declarations, definitions, and calls. It also discusses passing arguments to functions by value or reference. The document concludes with a programming assignment to write functions to perform basic math operations like addition, subtraction, etc. and display the results.

Uploaded by

Muhammad Qasim
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

CS-323 Programming Fundamentals 4 (3-2) CS-323 Programming Fundamentals 4 (3-2)

This document discusses functions in C++ programming. It defines what functions are, how they are declared and defined, and how values are passed to and returned from functions. It provides examples of function prototypes, declarations, definitions, and calls. It also discusses passing arguments to functions by value or reference. The document concludes with a programming assignment to write functions to perform basic math operations like addition, subtraction, etc. and display the results.

Uploaded by

Muhammad Qasim
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

CS-323

CS-323 Programming
Programming Fundamentals
Fundamentals
4(3-2)
4(3-2)
Lecture#
Lecture#99&&10
10
Today’s
Today’s Lecture
Lecture
• What are functions?

• How are they defined ?

• How are they declared ?

• What values are passed to functions ?

• What values do functions return ?

Programming Fundamentals 2
Functions
Functions
• A function is, in effect, a subprogram that can act on data
and return a value.
• Every C++ program has at least one function, main().
– When program starts, main() is called automatically.
– main() might call other functions, some of which might call still
others.
• Each function has its own name, and when that name is
encountered, the execution of the program branches to
the body of that function.
– When the function returns, execution resumes on the next line of
the calling function.
• When a program calls a function, execution switches to
the function and then resumes at the line after the function
call.
Programming Fundamentals 3
Functions…
Functions…

Programming Fundamentals 4
Functions…
Functions…
• Well-designed functions perform a specific and easily
understood task.
• Complicated tasks should be broken down into multiple
functions, and then each can be called in turn.
• Functions come in two varieties:
– user-defined and built-in.

– Built-in functions are part of your compiler package - they are


supplied by the manufacturer for your use.

Programming Fundamentals 5
Functions…Example
Functions…Example

Programming Fundamentals 6
Declaring
Declaring and
and Defining
Defining Functions
Functions
• Using functions in your program requires that you first
declare the function and that you then define the function.
• The declaration tells the compiler the name, return type,
and parameters of the function.
• The definition tells the compiler how the function works.

• No function can be called from any other function that


hasn't first been declared.
• The declaration of a function is called its prototype.

Programming Fundamentals 7
Declaring
Declaring the
the Functions
Functions
There are three ways to declare a function:

1.Write your prototype into a file, and then use the #include
directive to include it in your program.

2.Write the prototype into the file in which your function is


used.

3.Define the function before it is called by any other function.


– Doing so, the definition acts as its own declaration.

Programming Fundamentals 8
Declaring
Declaring and
and Defining
Defining Functions
Functions
Two types of functions:

1. Functions that return a value

2. Functions that do not return a value

return-value-type function-name(argument-list)
{
declarations and statements
}

Programming Fundamentals 9
Functions…
Functions…
• Prototype

Return value Assignment List with data type

int functionName ( int , int );

Programming Fundamentals 10
Functions…
Functions…
• Function Declaration

return_value_type function_name (argument_type_list);


main ( )
{
… … … … … … …
… … … … … … …
… … … … … … …
… … … … … … …
… … … … … … …
}

Programming Fundamentals 11
Functions…
Functions…
• Example:

int function_name (int var1, int var2, double var3) ;

void main ( )
{
… … … … … … …
… … … … … … …
… … … … … … …
… … … … … … …
… … … … … … …
}

Programming Fundamentals 12
Functions…
Functions…
• Function Definition

int function_name (int var1, int var2, double var3)


{
… … … … … … …
… … … … … … …
… … … … … … …
… … … … … … …
… … … … … … …
}

Programming Fundamentals 13
Functions…
Functions…
• Examples:

• Declaration:
int square (int n) ;

• Definition:

int square (int i)


{
return (i * i) ;
}

Programming Fundamentals 14
Functions…
Functions…
• Function Call

int x;
int i = 5;
x = square (i) ; // Function Call
cout<< “Square of ” << i << “ = “ << x;

OR

int x ;
int i = 5;
cout<< “Square of ” << i << “=“<< square(i);

Programming Fundamentals 15
Functions…
Functions…
• Example: Function to calculate integer power (Xn)

double raiseToPow (double n, int power)


{
double result;
int i;
result = 1.0 ;
for (i = 1; i <= power; i++) // braces first
{
result *= n ; // result = result * x
}
return (result);
}

Programming Fundamentals 16
Functions…
Functions…
• Code to Call the raisetopow Function

include <iostream>
void main ( )
{
double num ;
int p;
cout << “Please enter the number”;
cin >> num;
cout << “Please enter the integer power that you want this number raised to”;
cin >> p;
cout << num << “raise to power” << p << “is equal to” << raiseToPow (num, p);
}
Output
• Please enter the number
• Please enter the integer power that you want this number raised to
• 5 raise to power 3 is equal to 125

Programming Fundamentals 17
Functions…
Functions…
• Calling a function
– Call By Value

– Call By Reference

Calling function

Called function

Programming Fundamentals 18
Programming
Programming Assignment#
Assignment# 11
• Write functions for Addition, Multiplication, Subtraction,
Division, Mod (%) (remainder) operation of any 2
numbers. Your output should display each operation
result.

Programming Fundamentals 19
THANK
THANK YOU
YOU

Programming Fundamentals 20

You might also like