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

Kalvi c++ and python assesment

The document explains the concept of functions in C++ and Python, highlighting their purpose as named sections of code that perform specific tasks. It covers the types of functions, including built-in and user-defined functions, and discusses their benefits such as modularity, reusability, and abstraction. Additionally, it provides syntax examples for declaring, defining, and calling user-defined functions in both programming languages.

Uploaded by

Deepak Bhagtani
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)
3 views

Kalvi c++ and python assesment

The document explains the concept of functions in C++ and Python, highlighting their purpose as named sections of code that perform specific tasks. It covers the types of functions, including built-in and user-defined functions, and discusses their benefits such as modularity, reusability, and abstraction. Additionally, it provides syntax examples for declaring, defining, and calling user-defined functions in both programming languages.

Uploaded by

Deepak Bhagtani
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/ 21

Functions in C++ and Python

Here, you will learn about functions and their uses in

C++ and Python programming languages.

What are functions?


In programming, a named section of a program that performs a specific task is known as a
function. In this sense, a function is a type of procedure or routine. Some programming
languages make a distinction between a function, which returns a value, and a procedure,
which performs some operation but does not return a value.

Most programming languages come with a prewritten set of functions that are kept in
a library. You can also write your own functions to perform specialized tasks.

The term function is also used synonymously with operation and command. For example,
you execute the delete function to erase a word.
Functions

- Modularize a program.

- All variables defined inside functions are local variables. (known only in function definition)

- Parameters

 Functions have list of parameters.

 Communicate information between functions.

 Are also local variables to that function.

Benefits of functions

- Divide and Conquer

- Manageable program development

- Software reusability

- Use existing functions as building blocks for new programs

- Abstraction : hide internal details (library functions)

- Avoid code repetition

Types of functions(For both Python and C++)


 Built in functions
 User defined functions

Built in functions(For both C++ and Python)

- <bits/stdc++.h> covers all the libraries in c++


(This table is for C++)

These are the functions which are already built in the libraries of the programming
languages. In this module we will discuss about built in functions in C++ and Python.

They required to include the header file to work.

(This table is for python)

User defined functions(For both C++ and Python)

User defined functions are the functions which are used to perform a specific task or
we can say that an action according to the user. User can modify the function as he
or she wants.

So, these type of functions which made or changed by the user are known as User-
defined functions.

# For C++:

Now , user defined function means we have to make the function by our own by
following the rules of C++ in another words we can say that by using correct syntax.
So, let’s head over to that syntax.

1.Function declaration

The below picture states that how we can declare a user-defined function.

2.Function definition

The below picture states that how can we define a user defined function
3.Function call

The below picture states that how can we call a user defined function in our

programme.
For better understanding let’s understand four types of user
defined functions in C++
1. Function with no return type and no arguments

2.Function with no return type but arguments


3.Function with return type but no arguments
4.Function with return type and arguments
#For Python:

Now , user defined function means we have to make the function by our own by
following the rules of Python in another words we can say that by using correct
syntax.

The syntax is somewhat different from C++ programming language but the working
and use is same in all the programming language whether it is C++ or python.

In python there is no return type even not the data type it is not that return type is not
there you can write return type if you want for your understanding. Return type is
optional here.

In syntax we will learn how to define a user defined function and how can we call it.

So, let’s head over to the syntax.

Def is keyword which means defining the function.

(syntax for defining the user defined function in python)

Syntax for calling user defined function in python:


Some Examples on function calling for python:
Practice question on functions:
1.For python
2.For C++

For below questions predict the output:

1.
2.

You might also like