Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC1201: Programming Language 2

Similar presentations


Presentation on theme: "CSC1201: Programming Language 2"— Presentation transcript:

1 CSC1201: Programming Language 2
Functions I

2 – Organize code in program – Code are easier to maintain?
A Function is a group of statements that together perform a task. It can be used anywhere in the program. Why we need function? – Organize code in program – Code are easier to maintain? Function declaration: return_type FuncName(Type arg1, Type arg2,….. Type argN) { function body } A program can contain one or many functions Must always have a function called “main”. The main function is the starting point of all C++ programs The compiler will not compile the code unless it finds a function called “main” within the program.

3 “Hello World” program #include <iostream> using namespace std; int main ()‏ { cout << “Hello World\n”; Return 0; }

4 Function When we need function? – When you need to repeat the same process over and over in a program. – The function can be called many times but appears in the code once.

5 1- Predefined functions
Predefined functions are functions that are built into C++ Language to perform some standard operations. The C++ standard library provides numerous built-in functions that your program can call. For example, function strcat() to concatenate two strings the definitions have been written and it is ready to be used. – User needs to include pre-defined header file (i.e. math.h, time.h)

6 2- User defined functions
Function that been created by the user. – This functions need to be declared and defined by the user

7 functions Value returning functions: Void functions:
functions that have a return type. These functions return a value of a specific data type using the return statement. Void functions: functions that do not have a return type. These functions do not use a return statement to return a value.

8 Value returning functions
The syntax is: FuncType FuncName(formal parameter list )‏ { statements }

9 Void functions The syntax is: Void FuncName ( formal parameter list )‏
{ statements }

10 Examples: Write a Function larger, which returns the larger of the two given integers. Write a Function Square, which returns the square of the given integer. Write a function number_type. The function should output the number and message saying whether the number is positive, negative, or zero.

11 Example: With return value
Double larger ( double x , double y )‏ { double max; if ( x >= y )‏ max = x; else max = y; return max; } Function Call …….. Cout << “The larger of 5 and 6 is “ << larger(5 , 6) << endl; ………. Solution Ex 1

12 Example: With return value
#include<iostream> using std::cin; using std::cout; using std::endl; int square (int x)‏ { return x*x; } int main ( )‏ int number; cout<<"Enter any number to Calculate the square of this number "; cin>>number; cout<<endl; cout<<"the square of "<<number<<" is " <<square(number)<<endl; return 0; Solution Ex 2

13 Example: Without return value
Void number_type ( int x)‏ { if ( x > 0 )‏ cout << x << “ is positive.” << endl; else if ( x < 0 )‏ cout << x << “ is negative.” << endl; else cout<< x << “is a zero.”<<endl; } Function Call …….. Number_type( 5 ); ………. Solution Ex 3


Download ppt "CSC1201: Programming Language 2"

Similar presentations


Ads by Google