Sig Day 3
Sig Day 3
Day - 3
Functions
Function is a block of code which runs when it is
called.
Instead of writing the same code multiple times ,one
can define a function and call it whenever needed.
Syntax
Types
With Arguments:With Return Statement
With Arguments:Without Return
Statement
Without Arguments:With Return
Statement
Without Arguments:Without Return
Statement
Library Functions/Built in Functions
Other:getch(),scanf(),printf()
Calling a function multiple times
Calling a function multiple times
Problem Statements
1)Check whether the given number is
even or odd.Number will be provided
by user.
2)Write a function that takes two
integers as parameters and returns
their multiplication.
Strings
>John
>1234444
>Jo26
Defining String:Method 1
Defining String:Method 2
string name=”John Doe”
2)find(substring):
string str = "Hello World";
cout << str.find("World"); // Output: 6
Functions
3)append():
string str = "Hello";
str.append(" World");
cout << str; // Output: Hello World
4)insert(position, string):
string str = "Hello";
str.insert(5, " World");
cout << str; // Output: Hello World
Functions
5)compare():
string str1 = "abc";
string str2 = "def";
cout << str1.compare(str2); // Output: -1
6)empty():
string str = "";
cout << str.empty(); // Output: 1 (true)
Problem Statements
1)Take a string as user input and calculate its length