Functions in Cplusplus
product image
  • What you'll learn
  • ✓What are the 4 types of functions in C++?
    ✓What is a function in C++?
    ✓How many functions are there in C++?
    ✓What are the 4 parts of a function in C++?
    ✓What is the syntax of function?
  • Let’s find the right course for you!
  • Courses focused on building strong foundational skills for career growth
  • A third item
  • A fourth item
  • And a fifth one
  • A function is a block of code that performs some operation. A function can optionally define input parameters that enable callers to pass arguments into the function. A function can optionally return a value as output.
  • There are two types of functions in C programming: 1. Library Functions: are the functions which are declared in the C++ header files such as ceil(x), cos(x), exp(x), etc.
  • A function can take in parameters, run computations, and output a value. A function in C can be created using this syntax: return_type function_name(parameter list) { // function body } The return_type specifies the type of value that the function will return.
  • To call this function from another part of the program, you would use the following syntax: int a = 5, b = 7; int sum = add(a, b); Here, a and b are two integer variables that are passed as arguments to the add function.
  • 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.
  • The main function serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program. Several restrictions apply to the main function that don't apply to any other C functions.
  • When you define a function, you can specify a default value for each of the last parameters. This value will be used if the corresponding argument is left blank when calling to the function. This is done by using the assignment operator and assigning values for the arguments in the function definition. If a value for that parameter is not passed when the function is called, the default given value is used, but if a value is specified, this default value is ignored and the passed value is used instead. Consider the following example − Live Demo #include <iostream> using namespace std; int sum(int a, int b = 20) { int result; result = a + b; return (result); } int main () { // local variable declaration: int a = 100; int b = 200; int result; // calling a function to add the values. result = sum(a, b); cout << "Total value is :" << result << endl; // calling a function again as follows. result = sum(a); cout << "Total value is :" << result << endl; return 0; } When the above code is compiled and executed, it produces the following result − Total value is :300 Total value is :120
  • #include <iostream> using namespace std; int main () { // number definition: short s; int i; long l; float f; double d; // number assignments; s = 10; i = 1000; l = 1000000; f = 230.47; d = 30949.374; // number printing; cout << "short s :" << s << endl; cout << "int i :" << i << endl; cout << "long l :" << l << endl; cout << "float f :" << f << endl; cout << "double d :" << d << endl; return 0; } When the above code is compiled and executed, it produces the following result − short s :10 int i :1000 long l :1000000 float f :230.47 double d :30949.4
  • In addition to the various functions you can create, C++ also includes some useful functions you can use. These functions are available in standard C and C++ libraries and called built-in functions. These are functions that can be included in your program and then use. C++ has a rich set of mathematical operations, which can be performed on various numbers. Following table lists down some useful built-in mathematical functions available in C++. To utilize these functions you need to include the math header file <cmath>. Sr.No Function & Purpose 1 double cos(double); This function takes an angle (as a double) and returns the cosine. 2 double sin(double); This function takes an angle (as a double) and returns the sine. 3 double tan(double); This function takes an angle (as a double) and returns the tangent. 4 double log(double); This function takes a number and returns the natural log of that number. 5 double pow(double, double); The first is a number you wish to raise and the second is the power you wish to raise it t 6 double hypot(double, double); If you pass this function the length of two sides of a right triangle, it will return you the length of the hypotenuse. 7 double sqrt(double); You pass this function a number and it gives you the square root. 8 int abs(int); This function returns the absolute value of an integer that is passed to it. 9 double fabs(double); This function returns the absolute value of any decimal number passed to it. 10 double floor(double); Finds the integer which is less than or equal to the argument passed to it.
  • If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function. The formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit. While calling a function, there are two ways that arguments can be passed to a function − Sr.No Call Type & Description 1 Call by Value This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. 2 Call by Pointer This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument. 3 Call by Reference This method copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument. By default, C++ uses call by value to pass arguments. In general, this means that code within a function cannot alter the arguments used to call the function and above mentioned example while calling max() function used the same method. Default Values for Parameters When you define a function, you can specify a default value for each of the last parameters. This value will be used if the corresponding argument is left blank when calling to the function. This is done by using the assignment operator and assigning values for the arguments in the function definition. If a value for that parameter is not passed when the function is called, the default given value is used, but if a value is specified, this default value is ignored and the passed value is used instead. Consider the following example

Requirement

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.

Are you an absolute beginner looking forward to kickstart your journey in the programming domain. Coding can be hard skill learn to learn for many but no more. Welcome to C++ Programming Essentials, the most fundamental course that every aspiring programmer should take to kickstart their journey in the world of programming. The course teaches you the fundamental building blocks of programming and builds a logical thinking mindset using C++ as our programming language. Many concepts taught in the course are also relevant to other languages like Java, Python, JavaScript etc with few changes in the coding syntax. You will understand the basic concepts and techniques to break down a given problem into smaller parts by drawing flowcharts, write pseudocode, and then diving deep into C++ topics like - variables, datatypes, flow control using branching & loops, functions, arrays, character arrays & strings, recursion, bitmasking & OOPs concepts. Course Features HD Videos Intuitive Explanations Beginner Friendly Teaching Tested Industry vetted curriculum Assignments & Q-A Support Certificate of Completion
  • Related Topics
  • product image
  • Related Topics
  • product image
  • Related Topics
  • product image
  • Related Topics
  • product image

MIS Online computer course

May God Bless us MIS


Description In this course we will go step by step to build a complete custom MVC (Model View Controller)
framework Called TraversyMVC
using object oriented PHP. We will build something similar to Codeigniter but much much lighter.
This framework is completely open source and you are free to change the name, add stuff,
etc and use it as your own. This framework will include...